鍍金池/ 教程/ Java/ SVN 分支
SVN 檢查更改
SVN 更新過(guò)程
SVN 修復(fù)錯(cuò)誤
什么是版本控制系統(tǒng)(VCS)
SVN 檢出過(guò)程
SVN 執(zhí)行修改
SVN 標(biāo)簽
SVN 分支
SVN 生命周期
SVN 環(huán)境搭建
SVN 解決沖突

SVN 分支

Branch 選項(xiàng)會(huì)給開(kāi)發(fā)者創(chuàng)建出另外一條線路。當(dāng)有人希望開(kāi)發(fā)進(jìn)程分開(kāi)成兩條不同的線路時(shí),這個(gè)選項(xiàng)會(huì)非常有用。我們先假設(shè)你已經(jīng)發(fā)布了一個(gè)產(chǎn)品的 1.0 版本,你可能想創(chuàng)建一個(gè)新的分支,這樣就可以不干擾到 1.0 版本的bug修復(fù)的同時(shí),又可以開(kāi)發(fā)2.0版本。

在這一節(jié),我們將看到如何創(chuàng)建,穿過(guò)和合并分支。Jerry 因?yàn)榇a沖突的事情不開(kāi)心,所以他決定創(chuàng)建一個(gè)新的私有分支。

[jerry@CentOS project_repo]$ ls
branches  tags  trunk

[jerry@CentOS project_repo]$ svn copy trunk branches/jerry_branch
A         branches/jerry_branch

[jerry@CentOS project_repo]$ svn status
A  +    branches/jerry_branch

[jerry@CentOS project_repo]$ svn commit -m "Jerry's private branch"
Adding         branches/jerry_branch
Adding         branches/jerry_branch/README

Committed revision 9.
[jerry@CentOS project_repo]$ 

現(xiàn)在 Jerry 在自己的分支下開(kāi)始工作。他給序列添加了 sort 選項(xiàng)。Jerry 修改后的代碼如下:

[jerry@CentOS project_repo]$ cd branches/jerry_branch/

[jerry@CentOS jerry_branch]$ cat array.c 

上面的代碼將會(huì)產(chǎn)生下面的結(jié)果:

#include <stdio.h>
#define MAX 16

void bubble_sort(int *arr, int n)
{
   int i, j, temp, flag = 1;
   for (i = 1; i < n && flag == 1; ++i) {
      flag = 0;
      for (j = 0; j < n - i; ++j) {
         if (arr[j] > arr[j + 1]) {
            flag = 1;
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
         }
      }
   }
}

void accept_input(int *arr, int n)
{
   int i;

   for (i = 0; i < n; ++i) 
   scanf("%d", &arr[i]);
}

void display(int *arr, int n)
{
   int i;

   for (i = 0; i < n; ++i)
   printf("|%d| ", arr[i]);

   printf("\n");
}

int main(void)
{
   int i, n, key, ret, arr[MAX];

   printf("Enter the total number of elements: ");
   scanf("%d", &n);

   /* Error handling for array overflow */
   if (n >MAX) {
      fprintf(stderr, "Number of elements must be less than %d\n", MAX);
      return 1;
   }

   printf("Enter the elements\n");
   accept_input(arr, n);

   printf("Array has following elements\n");
   display(arr, n);

   printf("Sorted data is\n");
   bubble_sort(arr, n);
   display(arr, n);

   return 0;
}

Jerry 編譯并且測(cè)試了他的代碼,準(zhǔn)備提交他的更改。

[jerry@CentOS jerry_branch]$ make array
cc     array.c   -o array

[jerry@CentOS jerry_branch]$ ./array 

上面的命令將會(huì)產(chǎn)生如下的結(jié)果:

Enter the total number of elements: 5
Enter the elements
10
-4
2
7 
9
Array has following elements
|10| |-4| |2| |7| |9| 
Sorted data is
|-4| |2| |7| |9| |10| 

[jerry@CentOS jerry_branch]$ svn status
?       array
M       array.c

[jerry@CentOS jerry_branch]$ svn commit -m "Added sort operation"
Sending        jerry_branch/array.c
Transmitting file data .
Committed revision 10.

同時(shí),越過(guò)主干,Tom 決定實(shí)現(xiàn) search 選項(xiàng)。Tom 添加了 search 選項(xiàng)而添加代碼,他的代碼如下:

[tom@CentOS trunk]$ svn diff

上面的命令將會(huì)產(chǎn)生下面的結(jié)果:

Index: array.c
===================================================================
--- array.c   (revision 10)
+++ array.c   (working copy)
@@ -2,6 +2,27 @@

 #define MAX 16

+int bin_search(int *arr, int n, int key)
+{
+   int low, high, mid;
+
+   low   = 0;
+   high   = n - 1;
+   mid   = low + (high - low) / 2;
+
+   while (low <= high) {
+      if (arr[mid] == key)
+         return mid;
+      if (arr[mid] > key)
+         high = mid - 1;
+      else
+         low = mid + 1;
+      mid = low + (high - low) / 2;
+   }
+
+   return -1;
+}
+
 void accept_input(int *arr, int n)
 {
    int i;
@@ -22,7 +43,7 @@

 int main(void)
 {
-   int i, n, arr[MAX];
+   int i, n, ret, key, arr[MAX];

    printf("Enter the total number of elements: ");
    scanf("%d", &n);
@@ -39,5 +60,16 @@
    printf("Array has following elements\n");
    display(arr, n);

+   printf("Enter the element to be searched: ");
+   scanf("%d", &key);
+
+   ret = bin_search(arr, n, key);
+   if (ret < 0) {
+      fprintf(stderr, "%d element not present in array\n", key);
+      return 1;
+   }
+
+   printf("%d element found at location %d\n", key, ret + 1);
+
    return 0;
 }
After reviewing, he commits his changes.

[tom@CentOS trunk]$ svn status
?       array
M       array.c

[tom@CentOS trunk]$ svn commit -m "Added search operation"
Sending        trunk/array.c
Transmitting file data .
Committed revision 11.

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

[tom@CentOS trunk]$ cd ../branches/
[tom@CentOS branches]$ svn up
A    jerry_branch
A    jerry_branch/array.c
A    jerry_branch/README

[tom@CentOS branches]$ svn log
------------------------------------------------------------------------
r9 | jerry | 2013-08-27 21:56:51 +0530 (Tue, 27 Aug 2013) | 1 line

Added sort operation
------------------------------------------------------------------------

通過(guò)查看 Subversion 的 log 信息,Tom 發(fā)現(xiàn) Jerry 依賴(lài) ‘sort’ 選項(xiàng)。Tom 決定增添用折半查找,期望數(shù)據(jù)總是根據(jù)種類(lèi)進(jìn)行分類(lèi)。但是如果用戶提供的數(shù)據(jù)是沒(méi)有進(jìn)行分類(lèi)呢?在那種情況下,折半查找將會(huì)失效。所以他決定接著 Jerry 的代碼,在搜索選項(xiàng)前先進(jìn)性分類(lèi)。所以他告訴 Subversion 合并 Jerry 的分支到主干中去。

[tom@CentOS trunk]$ pwd
/home/tom/project_repo/trunk

[tom@CentOS trunk]$ svn merge ../branches/jerry_branch/
--- Merging r9 through r11 into '.':
U    array.c

在融合后,array.c 會(huì)看上去是這個(gè)樣子:

[tom@CentOS trunk]$ cat array.c

上面的代碼將會(huì)產(chǎn)生下面的結(jié)果:

#include <stdio.h>
#define MAX 16

void bubble_sort(int *arr, int n)
{
   int i, j, temp, flag = 1;

   for (i = 1; i < n && flag == 1; ++i) {
      flag = 0;
      for (j = 0; j < n - i; ++j) {
         if (arr[j] > arr[j + 1]) {
            flag        = 1;
            temp        = arr[j];
            arr[j]      = arr[j + 1];
            arr[j + 1]  = temp;
         }
      }
   }
}

int bin_search(int *arr, int n, int key)
{
   int low, high, mid;

   low   = 0;
   high  = n - 1;
   mid   = low + (high - low) / 2;

   while (low <= high) {
      if (arr[mid] == key)
         return mid;
      if (arr[mid] > key)
         high = mid - 1;
      else
         low = mid + 1;
      mid = low + (high - low) / 2;
   }
   return -1;
}

void accept_input(int *arr, int n)
{
   int i;

   for (i = 0; i < n; ++i)
      scanf("%d", &arr[i]);
}

void display(int *arr, int n)
{
   int i;
   for (i = 0; i < n; ++i)
      printf("|%d| ", arr[i]);
   printf("\n");
}

int main(void)
{
   int i, n, ret, key, arr[MAX];

   printf("Enter the total number of elements: ");
   scanf("%d", &n);

   /* Error handling for array overflow */
   if (n > MAX) {
      fprintf(stderr, "Number of elements must be less than %d\n", MAX);
      return 1;
   }

   printf("Enter the elements\n");
   accept_input(arr, n);

   printf("Array has following elements\n");
   display(arr, n);

   printf("Sorted data is\n");
   bubble_sort(arr, n);
   display(arr, n);

   printf("Enter the element to be searched: ");
   scanf("%d", &key);

   ret = bin_search(arr, n, key);
   if (ret < 0) {
      fprintf(stderr, "%d element not present in array\n", key);
      return 1;
   }

   printf("%d element found at location %d\n", key, ret +   1);

   return 0;
}

經(jīng)過(guò)編譯和測(cè)試后,Tom 提交了他的更改到倉(cāng)庫(kù)。

[tom@CentOS trunk]$ make array
cc     array.c   -o array

[tom@CentOS trunk]$ ./array 
Enter the total number of elements: 5
Enter the elements
10
-2
8
15
3
Array has following elements
|10| |-2| |8| |15| |3| 
Sorted data is
|-2| |3| |8| |10| |15| 
Enter the element to be searched: -2
-2 element found at location 1

[tom@CentOS trunk]$ svn commit -m "Merge changes from Jerry's code"
Sending        trunk
Sending        trunk/array.c
Transmitting file data .
Committed revision 12.

[tom@CentOS trunk]$