make DiffCommand callable
Project: http://git-wip-us.apache.org/repos/asf/maven-scm/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-scm/commit/2fe3d6b2 Tree: http://git-wip-us.apache.org/repos/asf/maven-scm/tree/2fe3d6b2 Diff: http://git-wip-us.apache.org/repos/asf/maven-scm/diff/2fe3d6b2 Branch: refs/heads/master Commit: 2fe3d6b2b8c8fed5afea58bb848de74f71e6c813 Parents: 4dc9673 Author: imod <d...@fortysix.ch> Authored: Fri Jun 21 20:41:32 2013 +0200 Committer: imod <d...@fortysix.ch> Committed: Fri Jun 21 20:41:32 2013 +0200 ---------------------------------------------------------------------- .../git/jgit/command/diff/JGitDiffCommand.java | 51 +++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-scm/blob/2fe3d6b2/src/main/java/org/apache/maven/scm/provider/git/jgit/command/diff/JGitDiffCommand.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/maven/scm/provider/git/jgit/command/diff/JGitDiffCommand.java b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/diff/JGitDiffCommand.java index cd4c5ae..9707ada 100644 --- a/src/main/java/org/apache/maven/scm/provider/git/jgit/command/diff/JGitDiffCommand.java +++ b/src/main/java/org/apache/maven/scm/provider/git/jgit/command/diff/JGitDiffCommand.java @@ -21,6 +21,7 @@ import org.apache.maven.scm.provider.git.jgit.command.JGitUtils; import org.codehaus.plexus.util.StringUtils; import org.eclipse.jgit.api.DiffCommand; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectReader; @@ -37,36 +38,40 @@ public class JGitDiffCommand extends AbstractDiffCommand implements GitCommand { try { Git git = Git.open(fileSet.getBasedir()); - AbstractTreeIterator oldTree = null; - if (startRevision != null && StringUtils.isNotEmpty(startRevision.getName().trim())) { - String startRev = startRevision.getName().trim(); - oldTree = getTreeIterator(git.getRepository(), startRev); - } + return callDiff(git, startRevision, endRevision); + } catch (Exception e) { + throw new ScmException("JGit diff failure!", e); + } + } - AbstractTreeIterator newTree = null; - if (endRevision != null && StringUtils.isNotEmpty(endRevision.getName().trim())) { - String endRev = endRevision.getName().trim(); - newTree = getTreeIterator(git.getRepository(), endRev); - } + public DiffScmResult callDiff(Git git, ScmVersion startRevision, ScmVersion endRevision) throws IOException, GitAPIException, ScmException { + AbstractTreeIterator oldTree = null; + if (startRevision != null && StringUtils.isNotEmpty(startRevision.getName().trim())) { + String startRev = startRevision.getName().trim(); + oldTree = getTreeIterator(git.getRepository(), startRev); + } - OutputStream out = new ByteArrayOutputStream(); + AbstractTreeIterator newTree = null; + if (endRevision != null && StringUtils.isNotEmpty(endRevision.getName().trim())) { + String endRev = endRevision.getName().trim(); + newTree = getTreeIterator(git.getRepository(), endRev); + } - DiffCommand diff = git.diff().setOutputStream(out).setOldTree(oldTree).setNewTree(newTree); - List<DiffEntry> entries = diff.call(); - List<ScmFile> changedFiles = new ArrayList<ScmFile>(); + OutputStream out = new ByteArrayOutputStream(); - // TODO get differences (but actually these are captured in the out - // already...) - Map<String, CharSequence> differences = new HashMap<String, CharSequence>(); + DiffCommand diff = git.diff().setOutputStream(out).setOldTree(oldTree).setNewTree(newTree); + List<DiffEntry> entries = diff.call(); + List<ScmFile> changedFiles = new ArrayList<ScmFile>(); - for (DiffEntry diffEntry : entries) { - changedFiles.add(new ScmFile(diffEntry.getNewPath(), JGitUtils.getScmFileStatus(diffEntry.getChangeType()))); - } + // TODO get differences (but actually these are captured in the out + // already...) + Map<String, CharSequence> differences = new HashMap<String, CharSequence>(); - return new DiffScmResult(changedFiles, differences, out.toString(), new ScmResult("JGit diff", "diff", null, true)); - } catch (Exception e) { - throw new ScmException("JGit diff failure!", e); + for (DiffEntry diffEntry : entries) { + changedFiles.add(new ScmFile(diffEntry.getNewPath(), JGitUtils.getScmFileStatus(diffEntry.getChangeType()))); } + + return new DiffScmResult(changedFiles, differences, out.toString(), new ScmResult("JGit diff", "diff", null, true)); } private AbstractTreeIterator getTreeIterator(Repository repo, String name) throws IOException {