Performing 'svn diff -rHEAD' with SVNKit

This is an example of performing diff on an entire working copy against the repository. Below we will enumerate the main steps performed by this example program, and reflect those steps in code comments as well, so that you could keep track of what the program does.

  1. We create a brand-new repository.
  2. Then populate it with a greek tree not using file system directories and files. All this initializing work is done with the help of the utility class called SamplesUtility.

  3. Then checkout a working copy.
  4. Then make text changes in two files and set a property on a directory.
  5. Then we diff our working copy against the repository.

   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 import java.io.File;
  14 import java.io.IOException;
  15 import org.tmatesoft.svn.core.SVNCommitInfo;
  16 import org.tmatesoft.svn.core.SVNDepth;
  17 import org.tmatesoft.svn.core.SVNException;
  18 import org.tmatesoft.svn.core.SVNPropertyValue;
  19 import org.tmatesoft.svn.core.SVNURL;
  20 import org.tmatesoft.svn.core.wc.SVNClientManager;
  21 import org.tmatesoft.svn.core.wc.SVNDiffClient;
  22 import org.tmatesoft.svn.core.wc.SVNRevision;
  23 import org.tmatesoft.svn.core.wc.SVNWCClient;
  24 /**
  25  * This examples demonstrate how you can run WORKING:HEAD diff.
  26  *
  27  * @version 1.2.0
  28  * @author  TMate Software Ltd.
  29  */
  30 public class DiffWCToRepository {
  31 
  32     /**
  33      * Pass the absolute path of the base directory where all example data will be created in
  34      * arg[0]. The sample will create:
  35      *
  36      *  - arg[0]/exampleRepository - repository with some test data
  37      *  - arg[0]/exampleWC         - working copy checked out from exampleRepository
  38      */
  39     public static void main (String[] args) {
  40         //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 and fill it with data
  49             SamplesUtility.createRepository(reposRoot);
  50             //2. populate the repository with the greek tree in a single transaction
  51             SVNCommitInfo info = SamplesUtility.createRepositoryTree(reposRoot);
  52             //pring out the commit information
  53             System.out.println(info);
  54 
  55             //3. checkout the entire repository tree
  56             SVNURL reposURL = SVNURL.fromFile(reposRoot);
  57             SamplesUtility.checkOutWorkingCopy(reposURL, wcRoot);
  58 
  59             //4. now make some changes to the working copy
  60             SamplesUtility.writeToFile(new File(wcRoot, "iota"), "New text appended to 'iota'", true);
  61             SamplesUtility.writeToFile(new File(wcRoot, "A/mu"), "New text in 'mu'", false);
  62 
  63             SVNClientManager clientManager = SVNClientManager.newInstance();
  64             SVNWCClient wcClient = SVNClientManager.newInstance().getWCClient();
  65             //set some property on a working copy directory
  66             wcClient.doSetProperty(new File(wcRoot, "A/B"), "spam", SVNPropertyValue.create("egg"), false,
  67                     SVNDepth.EMPTY, null, null);
  68 
  69             //5. now run diff the working copy against the repository
  70             SVNDiffClient diffClient = clientManager.getDiffClient();
  71             /*
  72              * This corresponds to 'svn diff -rHEAD'.
  73              */
  74             diffClient.doDiff(wcRoot, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD,
  75                     SVNDepth.INFINITY, true, System.out, null);
  76         } catch (SVNException svne) {
  77             System.out.println(svne.getErrorMessage());
  78             System.exit(1);
  79         } catch (IOException ioe) {
  80             ioe.printStackTrace();
  81             System.exit(1);
  82         }
  83     }
  84 
  85 }

What we do with the SVNDiffClient here in this example is equivalent to calling the command line client with the following parameters:

$svn diff -rHEAD

Now here come several comments on the doDiff() method itself.

When you run the program you will see an output like this:

r1 by 'alex' at Wed Sep 03 18:34:52 CEST 2008

Property changes on: /home/alex/workspace/tmp/exampleWC/A/B
___________________________________________________________________
Added: spam
   + egg

Index: /home/alex/workspace/tmp/exampleWC/A/mu
===================================================================
--- /home/alex/workspace/tmp/exampleWC/A/mu     (revision 1)
+++ /home/alex/workspace/tmp/exampleWC/A/mu     (working copy)
@@ -1 +1 @@
-This is the file 'A/mu'.
+New text in 'mu'
\ No newline at end of file
Index: /home/alex/workspace/tmp/exampleWC/iota
===================================================================
--- /home/alex/workspace/tmp/exampleWC/iota     (revision 1)
+++ /home/alex/workspace/tmp/exampleWC/iota     (working copy)
@@ -1 +1,2 @@
 This is the file 'iota'.
+New text appended to 'iota'
\ No newline at end of file

Diffing a working copy against a repository (last edited 2008-09-04 15:55:28 by 194)