]> source.dussan.org Git - jgit.git/commitdiff
diff: Default arguments to HEAD, working directory 92/1492/2
authorShawn O. Pearce <spearce@spearce.org>
Wed, 1 Sep 2010 19:29:40 +0000 (12:29 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 2 Sep 2010 18:38:39 +0000 (11:38 -0700)
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 <spearce@spearce.org>
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java

index 99878f14c866e24d549c3d742c8e89cb7c4eb7dc..b803604dc15e84e881787c201c38297d1bdce215 100644 (file)
@@ -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
index ec939b37e4bb57d00760bd94f3fddb6d7eaceb15..7a4aed71ea586478fa718c3d8237510dfae08ffb 100644 (file)
 
 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)