Differences between revisions 5 and 7 (spanning 2 versions)
Revision 5 as of 2008-09-03 16:15:57
Size: 4591
Editor: 194
Comment:
Revision 7 as of 2008-09-03 17:50:41
Size: 7119
Editor: 194
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
This example demonstrates how you can merge changes to a branch without specifying revisions. This example demonstrates how you can merge changes from trunk to a branch several times without specifying revisions.
What we are going to do here is the following:
 0. Initialize SVNKit to work with file:/// protocol.
 1. Create a repository.
 2. Populate it with some tree.
 3. Make a copy of that tree: /A to /A_copy.
 4. Checkout the entire repository tree to a working copy.
 5. Make some changes to trunk (A). Commit those changes to the repository.
 6. Then merge the changes from trunk (A) to the branch (A_copy).
 7. Then again make some changes to trunk and commit them to the repository.
 8. Again, merge the changes from trunk (A) to the branch (A_copy).
 
This steps are reflected in code comments:
Line 43: Line 55:
        //initialize SVNKit to work through file:/// protocol         //0. initialize SVNKit to work through file:/// protocol
Line 51: Line 63:
            //first create a repository and fill it with data             //1. first create a repository
Line 53: Line 65:
            //2. fill it with data
Line 61: Line 74:
            //copy A to A_copy in repository (url-to-url copy)             //3. copy A to A_copy in the repository (url-to-url copy)
Line 71: Line 84:
            //checkout the entire repository tree             //4. checkout the entire repository tree
Line 75: Line 88:
            //now make some changes to the working copy             //5. now make some changes to the A tree and commit them
Line 85: Line 98:
            commitClient.doCommit(new File[] { wcRoot }, false, "committing changes", null, null, false,
                                  
false, SVNDepth.INFINITY);
            commitClient.doCommit(new File[] { wcRoot }, false, "committing changes", null, null, false, false, SVNDepth.INFINITY);
Line 88: Line 100:
            //now diff the base revision of the working copy against the repository             //6. now merge changes from trunk to the branch.
Line 93: Line 105:
                    new File(wcRoot, "A_copy"), SVNDepth.UNKNOWN, true, false, false, false);                     new File(wcRoot, "A_copy"), SVNDepth.INFINITY, true, false, false, false);
            
            
            //7. now make some changes to the A tree again and commit them
            //change file contents of iota and A/D/gamma
            SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text2 appended to 'iota'", true);
            SamplesUtility.writeToFile(new File(wcRoot, "A/D/gamma"), "New text in 'gamma'", false);
            //remove A/C from version control
            wcClient.doDelete(new File(wcRoot, "A/C"), false, true, false);

            //commit local changes
            commitClient.doCommit(new File[] { wcRoot }, false, "committing changes again", null, null, false, false, SVNDepth.INFINITY);

            /* 8. do the same merge call, merge-tracking feature will merge only those revisions
             * which were not merged yet.
             */
            diffClient.doMerge(A_URL, SVNRevision.HEAD, Collections.singleton(rangeToMerge),
                    new File(wcRoot, "A_copy"), SVNDepth.INFINITY, true, false, false, false);
            
Line 103: Line 133:
}}}
Line 104: Line 135:
Each merge operation in this example is equivalent to the following command line client' command:
{{{
svn merge file:///home/alex/workspace/tmp/exampleRepository/A /home/alex/workspace/exampleWC/A_copy
Line 105: Line 139:

After the first merge, if you run 'svn status' in the working copy you should see this report:

{{{
alex@UFO:~/workspace/tmp/exampleWC$ $JSVN_PATH stat
 M A_copy
 M A_copy/B
M A_copy/mu
}}}

And 'svn pg svn:mergeinfo .' in A_copy gives:

{{{
alex@UFO:~/workspace/tmp/exampleWC/A_copy$ $JSVN_PATH pg svn:mergeinfo .
/A:2-3
}}}

After the second merge the status report will look like this:

{{{
alex@UFO:~/workspace/tmp/exampleWC$ $JSVN_PATH stat
 M A_copy
D A_copy/C
M A_copy/D/gamma
}}}

and propget report like this:
{{{
alex@UFO:~/workspace/tmp/exampleWC/A_copy$ $JSVN_PATH pg svn:mergeinfo .
/A:2-4
}}}

This example demonstrates how you can merge changes from trunk to a branch several times without specifying revisions. What we are going to do here is the following:

  1. Initialize SVNKit to work with file:/// protocol.

  2. Create a repository.
  3. Populate it with some tree.
  4. Make a copy of that tree: /A to /A_copy.
  5. Checkout the entire repository tree to a working copy.
  6. Make some changes to trunk (A). Commit those changes to the repository.
  7. Then merge the changes from trunk (A) to the branch (A_copy).
  8. Then again make some changes to trunk and commit them to the repository.
  9. Again, merge the changes from trunk (A) to the branch (A_copy).

This steps are reflected in code comments:

   1 /*
   2  * ====================================================================
   3  * Copyright (c) 2004-2008 TMate Software Ltd.  All rights reserved.
   4  *
   5  * This software is licensed as described in the file COPYING, which
   6  * you should have received as part of this distribution.  The terms
   7  * are also available at http://svnkit.com/license.html.
   8  * If newer versions of this license are posted there, you may use a
   9  * newer version instead, at your option.
  10  * ====================================================================
  11  */
  12 package org.tmatesoft.svn.examples.wc;
  13 
  14 import java.io.File;
  15 import java.io.IOException;
  16 import java.util.Collections;
  17 
  18 import org.tmatesoft.svn.core.SVNCommitInfo;
  19 import org.tmatesoft.svn.core.SVNDepth;
  20 import org.tmatesoft.svn.core.SVNException;
  21 import org.tmatesoft.svn.core.SVNPropertyValue;
  22 import org.tmatesoft.svn.core.SVNURL;
  23 import org.tmatesoft.svn.core.wc.SVNClientManager;
  24 import org.tmatesoft.svn.core.wc.SVNCommitClient;
  25 import org.tmatesoft.svn.core.wc.SVNCopyClient;
  26 import org.tmatesoft.svn.core.wc.SVNCopySource;
  27 import org.tmatesoft.svn.core.wc.SVNDiffClient;
  28 import org.tmatesoft.svn.core.wc.SVNRevision;
  29 import org.tmatesoft.svn.core.wc.SVNRevisionRange;
  30 import org.tmatesoft.svn.core.wc.SVNWCClient;
  31 
  32 
  33 /**
  34  * @version 1.2.0
  35  * @author  TMate Software Ltd.
  36  */
  37 public class Merge {
  38 
  39     public static void main (String[] args) {
  40         //0. initialize SVNKit to work through file:/// protocol
  41         SamplesUtility.initializeFSFSprotocol();
  42         
  43         File baseDirectory = new File(args[0]);
  44         File reposRoot = new File(baseDirectory, "exampleRepository");
  45         File wcRoot = new File(baseDirectory, "exampleWC");
  46         
  47         try {
  48             //1. first create a repository
  49             SamplesUtility.createRepository(reposRoot);
  50             //2. fill it with data
  51             SVNCommitInfo info = SamplesUtility.createRepositoryTree(reposRoot);
  52             //print out new revision info
  53             System.out.println(info);
  54 
  55             SVNClientManager clientManager = SVNClientManager.newInstance();
  56             
  57             SVNURL reposURL = SVNURL.fromFile(reposRoot);
  58 
  59             //3. copy A to A_copy in the repository (url-to-url copy)
  60             SVNCopyClient copyClient = clientManager.getCopyClient();
  61             SVNURL A_URL = reposURL.appendPath("A", true);
  62             SVNURL copyTargetURL = reposURL.appendPath("A_copy", true);
  63             SVNCopySource copySource = new SVNCopySource(SVNRevision.UNDEFINED, SVNRevision.HEAD, A_URL); 
  64             info = copyClient.doCopy(new SVNCopySource[] { copySource }, copyTargetURL, false, false, true, 
  65                     "copy A to A_copy", null);
  66             //print out new revision info
  67             System.out.println(info);
  68             
  69             //4. checkout the entire repository tree
  70             SamplesUtility.checkOutWorkingCopy(reposURL, wcRoot);
  71 
  72             
  73             //5. now make some changes to the A tree and commit them
  74             SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text appended to 'iota'", true);
  75             SamplesUtility.writeToFile(new File(wcRoot, "A/mu"), "New text in 'mu'", false);
  76             
  77             SVNWCClient wcClient = SVNClientManager.newInstance().getWCClient();
  78             wcClient.doSetProperty(new File(wcRoot, "A/B"), "spam", SVNPropertyValue.create("egg"), false, 
  79                     SVNDepth.EMPTY, null, null);
  80 
  81             //commit local changes
  82             SVNCommitClient commitClient = clientManager.getCommitClient();
  83             commitClient.doCommit(new File[] { wcRoot }, false, "committing changes", null, null, false, false, SVNDepth.INFINITY);
  84             
  85             //6. now merge changes from trunk to the branch.
  86             SVNDiffClient diffClient = clientManager.getDiffClient();
  87             SVNRevisionRange rangeToMerge = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD);
  88             
  89             diffClient.doMerge(A_URL, SVNRevision.HEAD, Collections.singleton(rangeToMerge), 
  90                     new File(wcRoot, "A_copy"), SVNDepth.INFINITY, true, false, false, false);
  91             
  92             
  93             //7. now make some changes to the A tree again and commit them
  94             //change file contents of iota and A/D/gamma
  95             SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text2 appended to 'iota'", true);
  96             SamplesUtility.writeToFile(new File(wcRoot, "A/D/gamma"), "New text in 'gamma'", false);
  97             //remove A/C from version control
  98             wcClient.doDelete(new File(wcRoot, "A/C"), false, true, false);
  99 
 100             //commit local changes
 101             commitClient.doCommit(new File[] { wcRoot }, false, "committing changes again", null, null, false, false, SVNDepth.INFINITY);
 102 
 103             /* 8. do the same merge call, merge-tracking feature will merge only those revisions
 104              * which were not merged yet.
 105              */ 
 106             diffClient.doMerge(A_URL, SVNRevision.HEAD, Collections.singleton(rangeToMerge), 
 107                     new File(wcRoot, "A_copy"), SVNDepth.INFINITY, true, false, false, false);
 108             
 109         } catch (SVNException svne) {
 110             System.out.println(svne.getErrorMessage());
 111             System.exit(1);
 112         } catch (IOException ioe) {
 113             ioe.printStackTrace();
 114             System.exit(1);
 115         }
 116     }
 117 }

Each merge operation in this example is equivalent to the following command line client' command:

svn merge file:///home/alex/workspace/tmp/exampleRepository/A /home/alex/workspace/exampleWC/A_copy

After the first merge, if you run 'svn status' in the working copy you should see this report:

alex@UFO:~/workspace/tmp/exampleWC$ $JSVN_PATH stat
 M     A_copy
 M     A_copy/B
M      A_copy/mu

And 'svn pg svn:mergeinfo .' in A_copy gives:

alex@UFO:~/workspace/tmp/exampleWC/A_copy$ $JSVN_PATH pg svn:mergeinfo .
/A:2-3

After the second merge the status report will look like this:

alex@UFO:~/workspace/tmp/exampleWC$ $JSVN_PATH stat
 M     A_copy
D      A_copy/C
M      A_copy/D/gamma

and propget report like this:

alex@UFO:~/workspace/tmp/exampleWC/A_copy$ $JSVN_PATH pg svn:mergeinfo .
/A:2-4

Merging from trunk to a branch (last edited 2008-09-04 15:57:55 by 194)