From fed508d55b8dcc3b9a3318b289e3eaaadd3ecb29 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 1 Sep 2010 12:29:40 -0700 Subject: [PATCH] diff: Default arguments to HEAD, working directory Similar to C Git, default our difference when no trees are given to us to something that makes a tiny bit of sense to the human. We also now support the --cached flag, and have its meaning work the same way as C Git. Change-Id: I2f19dad4e018404e280ea3e95ebd448a4b667f59 Signed-off-by: Shawn O. Pearce --- .../org/eclipse/jgit/pgm/CLIText.properties | 1 + .../src/org/eclipse/jgit/pgm/Diff.java | 40 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties index 99878f14c8..b803604dc1 100644 --- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties +++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties @@ -140,6 +140,7 @@ usage_alterTheDetailShown=alter the detail shown usage_approveDestructionOfRepository=approve destruction of repository usage_beMoreVerbose=be more verbose usage_beVerbose=be verbose +usage_cached=compare against index usage_cloneRepositoryIntoNewDir=Clone a repository into a new directory usage_configFile=configuration file usage_configureTheServiceInDaemonServicename=configure the service in daemon.servicename diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java index ec939b37e4..7a4aed71ea 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java @@ -45,8 +45,12 @@ package org.eclipse.jgit.pgm; +import static org.eclipse.jgit.lib.Constants.HEAD; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH; + import java.io.BufferedOutputStream; import java.io.PrintWriter; +import java.text.MessageFormat; import java.util.List; import org.eclipse.jgit.diff.DiffEntry; @@ -56,10 +60,14 @@ import org.eclipse.jgit.diff.RawTextIgnoreLeadingWhitespace; import org.eclipse.jgit.diff.RawTextIgnoreTrailingWhitespace; import org.eclipse.jgit.diff.RawTextIgnoreWhitespaceChange; import org.eclipse.jgit.diff.RenameDetector; -import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.dircache.DirCacheIterator; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.TextProgressMonitor; import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler; import org.eclipse.jgit.treewalk.AbstractTreeIterator; +import org.eclipse.jgit.treewalk.CanonicalTreeParser; +import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; @@ -69,12 +77,15 @@ class Diff extends TextBuiltin { private final DiffFormatter diffFmt = new DiffFormatter( // new BufferedOutputStream(System.out)); - @Argument(index = 0, metaVar = "metaVar_treeish", required = true) + @Argument(index = 0, metaVar = "metaVar_treeish") private AbstractTreeIterator oldTree; - @Argument(index = 1, metaVar = "metaVar_treeish", required = true) + @Argument(index = 1, metaVar = "metaVar_treeish") private AbstractTreeIterator newTree; + @Option(name = "--cached", usage = "usage_cached") + private boolean cached; + @Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = PathTreeFilterHandler.class) private TreeFilter pathFilter = TreeFilter.ALL; @@ -128,7 +139,7 @@ class Diff extends TextBuiltin { @Option(name = "--full-index") void abbrev(@SuppressWarnings("unused") boolean on) { - diffFmt.setAbbreviationLength(Constants.OBJECT_ID_STRING_LENGTH); + diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH); } @Option(name = "--src-prefix", usage = "usage_srcPrefix") @@ -153,6 +164,27 @@ class Diff extends TextBuiltin { protected void run() throws Exception { diffFmt.setRepository(db); try { + if (cached) { + if (oldTree == null) { + ObjectId head = db.resolve(HEAD + "^{tree}"); + if (head == null) + die(MessageFormat.format(CLIText.get().notATree, HEAD)); + CanonicalTreeParser p = new CanonicalTreeParser(); + ObjectReader reader = db.newObjectReader(); + try { + p.reset(reader, head); + } finally { + reader.release(); + } + oldTree = p; + } + newTree = new DirCacheIterator(db.readDirCache()); + } else if (oldTree == null) { + oldTree = new DirCacheIterator(db.readDirCache()); + newTree = new FileTreeIterator(db); + } else if (newTree == null) + newTree = new FileTreeIterator(db); + diffFmt.setProgressMonitor(new TextProgressMonitor()); diffFmt.setPathFilter(pathFilter); if (detectRenames != null) -- 2.39.5