SVN - Ramification

L'opération de branche crée une autre ligne de développement. C'est utile lorsque quelqu'un souhaite que le processus de développement se déroule dans deux directions différentes. Supposons que vous ayez publié un produit de la version 1.0, vous voudrez peut-être créer une nouvelle branche afin que le développement de 2.0 puisse être séparé des corrections de bogues 1.0.

Dans cette section, nous verrons comment créer, traverser et fusionner une branche. Jerry n'est pas content à cause du conflit, alors il décide de créer une nouvelle branche privée.

[[email protected] project_repo]$ ls
branches  tags  trunk

[[email protected] project_repo]$ svn copy trunk branches/jerry_branch
A         branches/jerry_branch

[[email protected] project_repo]$ svn status
A  +    branches/jerry_branch

[[email protected] project_repo]$ svn commit -m "Jerry's private branch"
Adding         branches/jerry_branch
Adding         branches/jerry_branch/README

Committed revision 9.
[[email protected] project_repo]$

Maintenant, Jerry travaille dans sa branche privée. Il ajoute une opération de tri pour le tableau. Le code modifié de Jerry ressemble à ceci.

[[email protected] project_repo]$ cd branches/jerry_branch/

[[email protected] jerry_branch]$ cat array.c

La commande ci-dessus produira le résultat suivant.

#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 compile et teste son code et est prêt à valider ses modifications.

[[email protected]S jerry_branch]$ make array
cc     array.c   -o array

[[email protected] jerry_branch]$ ./array

La commande ci-dessus produira le résultat suivant.

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| 

[[email protected] jerry_branch]$ svn status
?       array
M       array.c

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

Pendant ce temps, dans le coffre, Tom décide de mettre en œuvre une opération de recherche. Tom ajoute du code pour l'opération de recherche et son code ressemble à ceci.

[[email protected] trunk]$ svn diff

La commande ci-dessus produira le résultat suivant.

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

Après examen, il valide ses modifications.

[[email protected] trunk]$ svn status
?       array
M       array.c

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

Mais Tom est curieux de savoir ce que Jerry fait dans sa branche privée.

[[email protected] trunk]$ cd ../branches/
[[email protected] branches]$ svn up
A    jerry_branch
A    jerry_branch/array.c
A    jerry_branch/README

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

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

En visualisant le message du journal de Subversion, Tom a découvert que Jerry avait implémenté l'opération de «tri». Tom a implémenté une opération de recherche à l'aide d'un algorithme de recherche binaire, il attend toujours les données dans un ordre trié. Mais que se passe-t-il si l'utilisateur fournit des données dans un ordre non trié? Dans cette situation, l'opération de recherche binaire échouera. Il décide donc de prendre le code de Jerry pour trier les données avant l'opération de recherche. Il demande donc à Subversion de fusionner le code de la branche de Jerry dans le tronc.

[[email protected] trunk]$ pwd
/home/tom/project_repo/trunk

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

Après la fusion, array.c ressemblera à ceci.

[[email protected] trunk]$ cat array.c

La commande ci-dessus produira le résultat suivant.

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

Après compilation et test, Tom valide ses modifications dans le référentiel.

[[email protected] trunk]$ make array
cc     array.c   -o array

[[email protected] 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

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

[[email protected] trunk]$