课程表

SVN课程

工具箱
速查手册

SVN 分支

当前位置:免费教程 » 软件/图像 » SVN

Branch 选项会给开发者创建出另外一条线路。当有人希望开发进程分开成两条不同的线路时,这个选项会非常有用。我们先假设你已经发布了一个产品的 1.0 版本,你可能想创建一个新的分支,这样就可以不干扰到 1.0 版本的bug修复的同时,又可以开发2.0版本。

在这一节,我们将看到如何创建,穿过和合并分支。Jerry 因为代码冲突的事情不开心,所以他决定创建一个新的私有分支。

  1. [jerry@CentOS project_repo]$ ls
  2. branches tags trunk
  3. [jerry@CentOS project_repo]$ svn copy trunk branches/jerry_branch
  4. A branches/jerry_branch
  5. [jerry@CentOS project_repo]$ svn status
  6. A + branches/jerry_branch
  7. [jerry@CentOS project_repo]$ svn commit -m "Jerry's private branch"
  8. Adding branches/jerry_branch
  9. Adding branches/jerry_branch/README
  10. Committed revision 9.
  11. [jerry@CentOS project_repo]$

现在 Jerry 在自己的分支下开始工作。他给序列添加了 sort 选项。Jerry 修改后的代码如下:

  1. [jerry@CentOS project_repo]$ cd branches/jerry_branch/
  2. [jerry@CentOS jerry_branch]$ cat array.c

上面的代码将会产生下面的结果:

  1. #include <stdio.h>
  2. #define MAX 16
  3. void bubble_sort(int *arr, int n)
  4. {
  5. int i, j, temp, flag = 1;
  6. for (i = 1; i < n && flag == 1; ++i) {
  7. flag = 0;
  8. for (j = 0; j < n - i; ++j) {
  9. if (arr[j] > arr[j + 1]) {
  10. flag = 1;
  11. temp = arr[j];
  12. arr[j] = arr[j + 1];
  13. arr[j + 1] = temp;
  14. }
  15. }
  16. }
  17. }
  18. void accept_input(int *arr, int n)
  19. {
  20. int i;
  21. for (i = 0; i < n; ++i)
  22. scanf("%d", &arr[i]);
  23. }
  24. void display(int *arr, int n)
  25. {
  26. int i;
  27. for (i = 0; i < n; ++i)
  28. printf("|%d| ", arr[i]);
  29. printf("\n");
  30. }
  31. int main(void)
  32. {
  33. int i, n, key, ret, arr[MAX];
  34. printf("Enter the total number of elements: ");
  35. scanf("%d", &n);
  36. /* Error handling for array overflow */
  37. if (n >MAX) {
  38. fprintf(stderr, "Number of elements must be less than %d\n", MAX);
  39. return 1;
  40. }
  41. printf("Enter the elements\n");
  42. accept_input(arr, n);
  43. printf("Array has following elements\n");
  44. display(arr, n);
  45. printf("Sorted data is\n");
  46. bubble_sort(arr, n);
  47. display(arr, n);
  48. return 0;
  49. }

Jerry 编译并且测试了他的代码,准备提交他的更改。

  1. [jerry@CentOS jerry_branch]$ make array
  2. cc array.c -o array
  3. [jerry@CentOS jerry_branch]$ ./array

上面的命令将会产生如下的结果:

  1. Enter the total number of elements: 5
  2. Enter the elements
  3. 10
  4. -4
  5. 2
  6. 7
  7. 9
  8. Array has following elements
  9. |10| |-4| |2| |7| |9|
  10. Sorted data is
  11. |-4| |2| |7| |9| |10|
  12. [jerry@CentOS jerry_branch]$ svn status
  13. ? array
  14. M array.c
  15. [jerry@CentOS jerry_branch]$ svn commit -m "Added sort operation"
  16. Sending jerry_branch/array.c
  17. Transmitting file data .
  18. Committed revision 10.

同时,越过主干,Tom 决定实现 search 选项。Tom 添加了 search 选项而添加代码,他的代码如下:

  1. [tom@CentOS trunk]$ svn diff

上面的命令将会产生下面的结果:

  1. Index: array.c
  2. ===================================================================
  3. --- array.c (revision 10)
  4. +++ array.c (working copy)
  5. @@ -2,6 +2,27 @@
  6. #define MAX 16
  7. +int bin_search(int *arr, int n, int key)
  8. +{
  9. + int low, high, mid;
  10. +
  11. + low = 0;
  12. + high = n - 1;
  13. + mid = low + (high - low) / 2;
  14. +
  15. + while (low <= high) {
  16. + if (arr[mid] == key)
  17. + return mid;
  18. + if (arr[mid] > key)
  19. + high = mid - 1;
  20. + else
  21. + low = mid + 1;
  22. + mid = low + (high - low) / 2;
  23. + }
  24. +
  25. + return -1;
  26. +}
  27. +
  28. void accept_input(int *arr, int n)
  29. {
  30. int i;
  31. @@ -22,7 +43,7 @@
  32. int main(void)
  33. {
  34. - int i, n, arr[MAX];
  35. + int i, n, ret, key, arr[MAX];
  36. printf("Enter the total number of elements: ");
  37. scanf("%d", &n);
  38. @@ -39,5 +60,16 @@
  39. printf("Array has following elements\n");
  40. display(arr, n);
  41. + printf("Enter the element to be searched: ");
  42. + scanf("%d", &key);
  43. +
  44. + ret = bin_search(arr, n, key);
  45. + if (ret < 0) {
  46. + fprintf(stderr, "%d element not present in array\n", key);
  47. + return 1;
  48. + }
  49. +
  50. + printf("%d element found at location %d\n", key, ret + 1);
  51. +
  52. return 0;
  53. }
  54. After reviewing, he commits his changes.
  55. [tom@CentOS trunk]$ svn status
  56. ? array
  57. M array.c
  58. [tom@CentOS trunk]$ svn commit -m "Added search operation"
  59. Sending trunk/array.c
  60. Transmitting file data .
  61. Committed revision 11.

但是 Tom 好奇 Jerry 在他自己的私有分支中干了什么:

  1. [tom@CentOS trunk]$ cd ../branches/
  2. [tom@CentOS branches]$ svn up
  3. A jerry_branch
  4. A jerry_branch/array.c
  5. A jerry_branch/README
  6. [tom@CentOS branches]$ svn log
  7. ------------------------------------------------------------------------
  8. r9 | jerry | 2013-08-27 21:56:51 +0530 (Tue, 27 Aug 2013) | 1 line
  9. Added sort operation
  10. ------------------------------------------------------------------------

通过查看 Subversion 的 log 信息,Tom 发现 Jerry 依赖 ‘sort’ 选项。Tom 决定增添用折半查找,期望数据总是根据种类进行分类。但是如果用户提供的数据是没有进行分类呢?在那种情况下,折半查找将会失效。所以他决定接着 Jerry 的代码,在搜索选项前先进性分类。所以他告诉 Subversion 合并 Jerry 的分支到主干中去。

  1. [tom@CentOS trunk]$ pwd
  2. /home/tom/project_repo/trunk
  3. [tom@CentOS trunk]$ svn merge ../branches/jerry_branch/
  4. --- Merging r9 through r11 into '.':
  5. U array.c

在融合后,array.c 会看上去是这个样子:

  1. [tom@CentOS trunk]$ cat array.c

上面的代码将会产生下面的结果:

  1. #include <stdio.h>
  2. #define MAX 16
  3. void bubble_sort(int *arr, int n)
  4. {
  5. int i, j, temp, flag = 1;
  6. for (i = 1; i < n && flag == 1; ++i) {
  7. flag = 0;
  8. for (j = 0; j < n - i; ++j) {
  9. if (arr[j] > arr[j + 1]) {
  10. flag = 1;
  11. temp = arr[j];
  12. arr[j] = arr[j + 1];
  13. arr[j + 1] = temp;
  14. }
  15. }
  16. }
  17. }
  18. int bin_search(int *arr, int n, int key)
  19. {
  20. int low, high, mid;
  21. low = 0;
  22. high = n - 1;
  23. mid = low + (high - low) / 2;
  24. while (low <= high) {
  25. if (arr[mid] == key)
  26. return mid;
  27. if (arr[mid] > key)
  28. high = mid - 1;
  29. else
  30. low = mid + 1;
  31. mid = low + (high - low) / 2;
  32. }
  33. return -1;
  34. }
  35. void accept_input(int *arr, int n)
  36. {
  37. int i;
  38. for (i = 0; i < n; ++i)
  39. scanf("%d", &arr[i]);
  40. }
  41. void display(int *arr, int n)
  42. {
  43. int i;
  44. for (i = 0; i < n; ++i)
  45. printf("|%d| ", arr[i]);
  46. printf("\n");
  47. }
  48. int main(void)
  49. {
  50. int i, n, ret, key, arr[MAX];
  51. printf("Enter the total number of elements: ");
  52. scanf("%d", &n);
  53. /* Error handling for array overflow */
  54. if (n > MAX) {
  55. fprintf(stderr, "Number of elements must be less than %d\n", MAX);
  56. return 1;
  57. }
  58. printf("Enter the elements\n");
  59. accept_input(arr, n);
  60. printf("Array has following elements\n");
  61. display(arr, n);
  62. printf("Sorted data is\n");
  63. bubble_sort(arr, n);
  64. display(arr, n);
  65. printf("Enter the element to be searched: ");
  66. scanf("%d", &key);
  67. ret = bin_search(arr, n, key);
  68. if (ret < 0) {
  69. fprintf(stderr, "%d element not present in array\n", key);
  70. return 1;
  71. }
  72. printf("%d element found at location %d\n", key, ret + 1);
  73. return 0;
  74. }

经过编译和测试后,Tom 提交了他的更改到仓库。

  1. [tom@CentOS trunk]$ make array
  2. cc array.c -o array
  3. [tom@CentOS trunk]$ ./array
  4. Enter the total number of elements: 5
  5. Enter the elements
  6. 10
  7. -2
  8. 8
  9. 15
  10. 3
  11. Array has following elements
  12. |10| |-2| |8| |15| |3|
  13. Sorted data is
  14. |-2| |3| |8| |10| |15|
  15. Enter the element to be searched: -2
  16. -2 element found at location 1
  17. [tom@CentOS trunk]$ svn commit -m "Merge changes from Jerry's code"
  18. Sending trunk
  19. Sending trunk/array.c
  20. Transmitting file data .
  21. Committed revision 12.
  22. [tom@CentOS trunk]$
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号