aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java')
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Diff.java126
1 files changed, 72 insertions, 54 deletions
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 61a385df83..52665a1c71 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
@@ -1,51 +1,17 @@
/*
* Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
* Copyright (C) 2009, Johannes E. Schindelin
- * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de>
- * and other copyright owners as documented in the project's IP log.
+ * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de> and others
*
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
*
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.pgm;
-import static java.lang.Integer.valueOf;
import static org.eclipse.jgit.lib.Constants.HEAD;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
@@ -62,6 +28,7 @@ import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.dircache.DirCacheIterator;
+import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
@@ -80,16 +47,20 @@ import org.kohsuke.args4j.Option;
class Diff extends TextBuiltin {
private DiffFormatter diffFmt;
+ private boolean showNameOnly = false;
+
+ private boolean showNameAndStatusOnly = false;
+
@Argument(index = 0, metaVar = "metaVar_treeish")
private AbstractTreeIterator oldTree;
@Argument(index = 1, metaVar = "metaVar_treeish")
private AbstractTreeIterator newTree;
- @Option(name = "--cached", usage = "usage_cached")
+ @Option(name = "--cached", aliases = { "--staged" }, usage = "usage_cached")
private boolean cached;
- @Option(name = "--", metaVar = "metaVar_paths", multiValued = true, handler = PathTreeFilterHandler.class)
+ @Option(name = "--", metaVar = "metaVar_paths", handler = PathTreeFilterHandler.class)
private TreeFilter pathFilter = TreeFilter.ALL;
// BEGIN -- Options shared with Log
@@ -113,7 +84,22 @@ class Diff extends TextBuiltin {
private Integer renameLimit;
@Option(name = "--name-status", usage = "usage_nameStatus")
- private boolean showNameAndStatusOnly;
+ void nameAndStatusOnly(boolean on) {
+ if (showNameOnly) {
+ throw new IllegalArgumentException(
+ CLIText.get().cannotUseNameStatusOnlyAndNameOnly);
+ }
+ showNameAndStatusOnly = on;
+ }
+
+ @Option(name = "--name-only", usage = "usage_nameOnly")
+ void nameOnly(boolean on) {
+ if (showNameAndStatusOnly) {
+ throw new IllegalArgumentException(
+ CLIText.get().cannotUseNameStatusOnlyAndNameOnly);
+ }
+ showNameOnly = on;
+ }
@Option(name = "--ignore-space-at-eol")
void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
@@ -150,12 +136,12 @@ class Diff extends TextBuiltin {
diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH);
}
- @Option(name = "--src-prefix", usage = "usage_srcPrefix")
+ @Option(name = "--src-prefix", metaVar = "metaVar_prefix", usage = "usage_srcPrefix")
void sourcePrefix(String path) {
diffFmt.setOldPrefix(path);
}
- @Option(name = "--dst-prefix", usage = "usage_dstPrefix")
+ @Option(name = "--dst-prefix", metaVar = "metaVar_prefix", usage = "usage_dstPrefix")
void dstPrefix(String path) {
diffFmt.setNewPrefix(path);
}
@@ -169,20 +155,21 @@ class Diff extends TextBuiltin {
// END -- Options shared with Log
@Override
- protected void init(final Repository repository, final String gitDir) {
+ protected void init(Repository repository, String gitDir) {
super.init(repository, gitDir);
diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
}
@Override
- protected void run() throws Exception {
+ protected void run() {
diffFmt.setRepository(db);
try {
if (cached) {
if (oldTree == null) {
ObjectId head = db.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
- if (head == null)
+ if (head == null) {
die(MessageFormat.format(CLIText.get().notATree, HEAD));
+ }
CanonicalTreeParser p = new CanonicalTreeParser();
try (ObjectReader reader = db.newObjectReader()) {
p.reset(reader, head);
@@ -193,15 +180,17 @@ class Diff extends TextBuiltin {
} else if (oldTree == null) {
oldTree = new DirCacheIterator(db.readDirCache());
newTree = new FileTreeIterator(db);
- } else if (newTree == null)
+ } else if (newTree == null) {
newTree = new FileTreeIterator(db);
+ }
TextProgressMonitor pm = new TextProgressMonitor(errw);
pm.setDelayStart(2, TimeUnit.SECONDS);
diffFmt.setProgressMonitor(pm);
diffFmt.setPathFilter(pathFilter);
- if (detectRenames != null)
+ if (detectRenames != null) {
diffFmt.setDetectRenames(detectRenames.booleanValue());
+ }
if (renameLimit != null && diffFmt.isDetectRenames()) {
RenameDetector rd = diffFmt.getRenameDetector();
rd.setRenameLimit(renameLimit.intValue());
@@ -210,11 +199,15 @@ class Diff extends TextBuiltin {
if (showNameAndStatusOnly) {
nameStatus(outw, diffFmt.scan(oldTree, newTree));
outw.flush();
-
+ } else if(showNameOnly) {
+ nameOnly(outw, diffFmt.scan(oldTree, newTree));
+ outw.flush();
} else {
diffFmt.format(oldTree, newTree);
diffFmt.flush();
}
+ } catch (RevisionSyntaxException | IOException e) {
+ throw die(e.getMessage(), e);
} finally {
diffFmt.close();
}
@@ -234,16 +227,41 @@ class Diff extends TextBuiltin {
out.println("M\t" + ent.getNewPath()); //$NON-NLS-1$
break;
case COPY:
- out.format("C%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
- ent.getOldPath(), ent.getNewPath());
+ out.format("C%1$03d\t%2$s\t%3$s", //$NON-NLS-1$
+ Integer.valueOf(ent.getScore()), ent.getOldPath(),
+ ent.getNewPath());
out.println();
break;
case RENAME:
- out.format("R%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
- ent.getOldPath(), ent.getNewPath());
+ out.format("R%1$03d\t%2$s\t%3$s", //$NON-NLS-1$
+ Integer.valueOf(ent.getScore()), ent.getOldPath(),
+ ent.getNewPath());
out.println();
break;
}
}
}
+
+ static void nameOnly(ThrowingPrintWriter out, List<DiffEntry> files)
+ throws IOException {
+ for (DiffEntry ent : files) {
+ switch (ent.getChangeType()) {
+ case ADD:
+ out.println(ent.getNewPath());
+ break;
+ case DELETE:
+ out.println(ent.getOldPath());
+ break;
+ case MODIFY:
+ out.println(ent.getNewPath());
+ break;
+ case COPY:
+ out.println(ent.getNewPath());
+ break;
+ case RENAME:
+ out.println(ent.getNewPath());
+ break;
+ }
+ }
+ }
}