- SVN 教程
- SVN - 首頁
- SVN - 基本概念
- SVN - 環境設定
- SVN - 生命週期
- SVN - 檢出過程
- SVN - 執行更改
- SVN - 檢閱更改
- SVN - 更新過程
- SVN - 修復錯誤
- SVN - 解決衝突
- SVN - 標籤
- SVN - 分支
- SVN 實用資源
- SVN - 快速指南
- SVN - 實用資源
- SVN - 討論
SVN - 分支
分支操作建立另一個開發版本。當有人希望將開發過程一分為二時,此功能很有用。假設你已經發布了 1.0 版本的產品,你可能希望建立一個新分支,以便 2.0 的開發可以與 1.0 的錯誤修復分開。
在本節中,我們將瞭解如何建立、遍歷和合並分支。傑瑞對沖突感到不高興,所以他決定建立一個新私有分支。
[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]$
現在傑瑞正在他的私有分支中工作。他為陣列添加了排序操作。傑瑞修改後的程式碼看起來如下。
[jerry@CentOS project_repo]$ cd branches/jerry_branch/ [jerry@CentOS jerry_branch]$ cat array.c
上面的命令將產生以下結果。
#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@CentOS jerry_branch]$ make array cc array.c -o array [jerry@CentOS jerry_branch]$ ./array
上面的命令將產生以下結果。
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.
與此同時,湯姆決定在主幹中實現搜尋操作。湯姆為搜尋操作添加了程式碼,他的程式碼看起來如下。
[tom@CentOS trunk]$ svn diff
上面的命令將產生以下結果。
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;
}
在審查後,他提交了他的更改。
[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@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 ------------------------------------------------------------------------
透過檢視 Subversion 的日誌訊息,湯姆發現傑瑞實現了“排序”操作。湯姆使用二分查詢演算法實現了搜尋操作,它總是希望資料按排序順序。但是,如果使用者提供的的是未排序順序的資料呢?在這種情況下,二分查詢操作將失敗。因此,他決定使用傑瑞的程式碼在執行搜尋操作之前對資料進行排序。所以他要求 Subversion 將傑瑞分支中的程式碼合併到主幹中。
[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 將如下所示。
[tom@CentOS trunk]$ cat array.c
上面的命令將產生以下結果。
#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;
}
在編譯和測試後,湯姆將他的更改提交到了倉庫。
[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]$
廣告