Skip to content

Commit

Permalink
Add a basic Diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
opticyclic committed Jun 2, 2014
1 parent dc8d3bf commit b245315
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Changelog
=========
### 1.1.1 ###

- Added Diff task
- Bundled with svnkit-1.8.5

### 1.1.0 ###
Expand All @@ -31,6 +32,7 @@ Available commands
- Add
- Checkout
- Commit
- Diff
- Export
- Info
- Log
Expand Down Expand Up @@ -124,6 +126,15 @@ Commands
</target>


# diff
<target name="diff">
<svn username="guest" password="">
<diff workingcopy="/path/to/workingcopy"
outfilename="svn.patch"/>
</svn>
</target>


# mkdir
<svn>
<mkdir
Expand Down
7 changes: 7 additions & 0 deletions src/com/googlecode/svntask/SvnTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.googlecode.svntask.command.Commit;
import com.googlecode.svntask.command.Copy;
import com.googlecode.svntask.command.Delete;
import com.googlecode.svntask.command.Diff;
import com.googlecode.svntask.command.Export;
import com.googlecode.svntask.command.Info;
import com.googlecode.svntask.command.Log;
Expand Down Expand Up @@ -141,6 +142,12 @@ public void addExport(Export export)
this.addCommand(export);
}

/** */
public void addDiff(Diff diff)
{
this.addCommand(diff);
}

/** */
private void addCommand(Command command)
{
Expand Down
88 changes: 88 additions & 0 deletions src/com/googlecode/svntask/command/Diff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.googlecode.svntask.command;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;

import com.googlecode.svntask.Command;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
* svn diff.
* Diff between working copy and head
*
* @author opticyclic
*/
public class Diff extends Command {

private File workingCopy;
private String user;
private String password;
private String outFileName = "svn.patch";

@Override
public void execute() throws Exception {
SVNClientManager cm = getTask().getSvnClient();

if(user != null && password != null) {
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(user, password);
cm.setAuthenticationManager(authManager);
}

// Get the Diff Client
SVNDiffClient diffClient = cm.getDiffClient();

//Save output at root
File workingCopyRoot = SVNWCUtil.getWorkingCopyRoot(workingCopy, false);
File svnDiffFile = new File(workingCopyRoot, outFileName);
svnDiffFile.createNewFile();
OutputStream outputStream = new FileOutputStream(svnDiffFile);

boolean useAncestry = false;
Collection changelist = null;

//Use the pegged version of doDiff() which runs diff on different revisions of the same tree (or file).
//The 2nd parameter (SVNRevision.UNDEFINED) denotes that we do not provide any special peg revision. This will automatically default to the HEAD revision.
//Then we provide SVNRevision.WORKING and SVNRevision.HEAD as we want to diff working tree with the one located in the repository at HEAD revision.
//SVNDepth.INFINITY says that want to diff the entire tree recursively.
//useAncestry flag - controls whether or not items being diffed will be checked for relatedness first.
// Unrelated items are typically transmitted to the editor as a deletion of one thing and the addition of another, but if this flag is true, unrelated items will be diffed as if they were related.
//outputStream - this is the diff output receiver stream.
//diff on the entire tree, not just on members of some changelist (in case we had one) so pass null.
diffClient.doDiff(workingCopy, SVNRevision.UNDEFINED, SVNRevision.WORKING, SVNRevision.HEAD, SVNDepth.INFINITY, useAncestry, outputStream, changelist);

getTask().log("Diff file is at: " + svnDiffFile.getAbsolutePath(), Project.MSG_INFO);
}

@Override
protected void validateAttributes() throws Exception {
//ToDo:Validation!
}

public void setOutFileName(String outFileName) {
this.outFileName = outFileName;
}

public void setWorkingCopy(File workingCopy) {
this.workingCopy = workingCopy;
}

/** */
public void setUsername(String user) {
this.user = user;
}

/** */
public void setPassword(String pass) {
this.password = pass;
}

}

0 comments on commit b245315

Please sign in to comment.