Trang chủ > Khác > Học SVN > SVN - Tạo nhánh (Branching) (bài tập Học SVN)

SVN - Tạo nhánh (Branching) (bài tập Học SVN)

Hoạt động branch trong SVN tạo tuyến của sự phát triển. Nó rất hữu ích khi bạn muốn tiến trình phát triển phân loại theo 2 hướng khác nhau. Chúng tôi giả sử rằng bạn đã công bố một sản phẩm có phiên bản 1.0, bạn có nhu cầu muốn tạo ra một nhánh mới để phát triển phiên bản 2.0 mà có nó vẫn giữ riêng rẽ với phiên bản 1.0 để sửa các bug của nó.

Trong tình huống này, chúng ta cần theo dõi cách để tạo ra hoặc là sáp nhập nhánh. Jerry sẽ không cảm thấy vui mừng khi có sự xuất hiện xung đột (conflict) trong phiên bản trước, chính vì thế anh ta đã quyết định tạo ra một nhánh riêng của mình.

 [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]$ 

Bây giờ Jerry đang làm các công việc trên nhánh riêng của mình. Anh ta đã tạo thêm hoạt động sort cho mảng (array). Khi đó, code đã được chỉnh sửa của Jerry nhìn giống như dưới đây:

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

Lệnh trên sẽ tạo ra kết quả như dưới đây:

#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 đã biên dịch và kiểm tra code của anh ta và sẵn sàng để commit các thay đổi của chính anh ta.

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

Lệnh trên sẽ tạo ra kết quả như dưới đây:

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. 

Trong khi đó, trên thân (trunk), Tom đã quyết định thực hiện hoạt động search. Tom thêm code cho hoạt động search và bây giờ code của anh ta trông như sau:

 [tom@CentOS trunk]$ svn diff

Lệnh trên sẽ tạo ra kết quả dưới đây:

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; 
}

Sau khi duyệt, anh ta đã commit các thay đổi của mình.

 [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. 

Nhưng Tom tò mò về những việc Jerry đã thực hiện trong nhánh riêng của cậu ta:

 [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
------------------------------------------------------------------------

Bằng cách quan sát trên thông báo log, Tom đã tìm thấy rằng Jerry đã thực hiện hoạt động sort. Tom thực hiện hoạt động search dựa trên việc dùng thuật toán tìm kiếm nhị phân, nó luôn luôn chắc chắn rằng các dữ liệu sẽ được sắp xếp theo thứ tự. Nhưng nếu người dùng cung cấp dữ liệu chưa được sắp xếp. Trong tình huống này, hoạt động search nhị phân sẽ không thành công. Chính vì thế anh ta đã quyết định sắp xếp các dữ liệu của Jerry trước khi thực hiện hoạt động search. Do đó anh ta đã yêu cầu nhập code từ nhánh của Jerry vào trong thân (trunk).

 [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

Sau khi sáp nhập, array. c sẽ trông giống như dưới đây:

 [tom@CentOS trunk]$ cat array. c

Lệnh trên sẽ tạo ra kết quả như dưới đây:

#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; 
}

Sau khi biên dịch và kiểm tra lại, Tom commit các thay đổi của anh ta đến repository.