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 <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-09-01 12:29:40 -07:00
parent 59a262d5d2
commit fed508d55b
2 changed files with 37 additions and 4 deletions

View 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

View File

@ -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.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 @@ void abbrev(int lines) {
@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 @@ void noPrefix(@SuppressWarnings("unused") boolean on) {
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)