]> source.dussan.org Git - jgit.git/commitdiff
JGit pgm: make Blame more robust against bogus input 30/146930/3
authorThomas Wolf <thomas.wolf@paranor.ch>
Thu, 1 Aug 2019 19:04:31 +0000 (21:04 +0200)
committerThomas Wolf <thomas.wolf@paranor.ch>
Mon, 26 Aug 2019 20:48:41 +0000 (22:48 +0200)
Make the command die with proper messages when the revision
cannot be resolved or the file doesn't exist in the repository.
Previously the command would throw NPEs in these cases.

Bug: 490798
Change-Id: Ia457347aa22cf6bd2c2b6e7b9d705a66b3826307
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java [new file with mode: 0644]
org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java
new file mode 100644 (file)
index 0000000..e806872
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * 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
+ *
+ * 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.
+ */
+package org.eclipse.jgit.pgm;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.CLIRepositoryTestCase;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class BlameTest extends CLIRepositoryTestCase {
+
+       @Rule
+       public ExpectedException thrown = ExpectedException.none();
+
+       @Test
+       public void testBlameNoHead() throws Exception {
+               try (Git git = new Git(db)) {
+                       writeTrashFile("inIndex.txt", "index");
+                       git.add().addFilepattern("inIndex.txt").call();
+               }
+               thrown.expect(Die.class);
+               thrown.expectMessage("no such ref: HEAD");
+               execute("git blame inIndex.txt");
+       }
+
+       @Test
+       public void testBlameCommitted() throws Exception {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+                       writeTrashFile("committed.txt", "committed");
+                       git.add().addFilepattern("committed.txt").call();
+                       git.commit().setMessage("commit").call();
+               }
+               assertStringArrayEquals(
+                               "1ad3399c (GIT_COMMITTER_NAME 2009-08-15 20:12:58 -0330 1) committed",
+                               execute("git blame committed.txt"));
+       }
+
+       @Test
+       public void testBlameStaged() throws Exception {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+                       writeTrashFile("inIndex.txt", "index");
+                       git.add().addFilepattern("inIndex.txt").call();
+               }
+               assertStringArrayEquals(
+                               "         (Not Committed Yet          1) index",
+                               execute("git blame inIndex.txt"));
+       }
+
+       @Test
+       public void testBlameUnstaged() throws Exception {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+               }
+               writeTrashFile("onlyInWorkingTree.txt", "not in repo");
+               thrown.expect(Die.class);
+               thrown.expectMessage("no such path 'onlyInWorkingTree.txt' in HEAD");
+               execute("git blame onlyInWorkingTree.txt");
+       }
+
+       @Test
+       public void testBlameNonExisting() throws Exception {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+               }
+               thrown.expect(Die.class);
+               thrown.expectMessage("no such path 'does_not_exist.txt' in HEAD");
+               execute("git blame does_not_exist.txt");
+       }
+
+       @Test
+       public void testBlameNonExistingInSubdir() throws Exception {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+               }
+               thrown.expect(Die.class);
+               thrown.expectMessage("no such path 'sub/does_not_exist.txt' in HEAD");
+               execute("git blame sub/does_not_exist.txt");
+       }
+}
index 2a5a31eba81f7252dca6bf18eedf10e7337b54bb..b2ec57c7495c0467e81eec882b1955f982e1a7ac 100644 (file)
@@ -155,6 +155,8 @@ needSingleRevision=Needed a single revision
 noGitRepositoryConfigured=No Git repository configured.
 noNamesFound=No names found, cannot describe anything.
 noSuchFile=no such file: {0}
+noSuchPathInRef=no such path ''{0}'' in {1}
+noSuchRef=no such ref: {0}
 noSuchRemoteRef=no such remote ref: ''{0}''
 noSystemConsoleAvailable=No System.console available
 noTREESectionInIndex=no 'TREE' section in index
index e38cb468d942c3f74ab8121eda642f09b38479a3..b67b04c5becf92c7a0ff42addc8dbe4582fd2aab 100644 (file)
@@ -67,6 +67,7 @@ import org.eclipse.jgit.diff.RawTextComparator;
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.errors.NoWorkTreeException;
 import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.pgm.internal.CLIText;
@@ -178,14 +179,28 @@ class Blame extends TextBuiltin {
                                }
                                generator.reverse(rangeStart, rangeEnd);
                        } else if (revision != null) {
-                               generator.push(null, db.resolve(revision + "^{commit}")); //$NON-NLS-1$
+                               ObjectId rev = db.resolve(revision + "^{commit}"); //$NON-NLS-1$
+                               if (rev == null) {
+                                       throw die(MessageFormat.format(CLIText.get().noSuchRef,
+                                                       revision));
+                               }
+                               generator.push(null, rev);
                        } else {
-                               generator.push(null, db.resolve(Constants.HEAD));
+                               ObjectId head = db.resolve(Constants.HEAD);
+                               if (head == null) {
+                                       throw die(MessageFormat.format(CLIText.get().noSuchRef,
+                                                       Constants.HEAD));
+                               }
+                               generator.push(null, head);
                                if (!db.isBare()) {
                                        DirCache dc = db.readDirCache();
                                        int entry = dc.findEntry(file);
                                        if (0 <= entry) {
                                                generator.push(null, dc.getEntry(entry).getObjectId());
+                                       } else {
+                                               throw die(MessageFormat.format(
+                                                               CLIText.get().noSuchPathInRef, file,
+                                                               Constants.HEAD));
                                        }
 
                                        File inTree = new File(db.getWorkTree(), file);
@@ -196,6 +211,10 @@ class Blame extends TextBuiltin {
                        }
 
                        blame = BlameResult.create(generator);
+                       if (blame == null) {
+                               throw die(MessageFormat.format(CLIText.get().noSuchPathInRef,
+                                               file, revision != null ? revision : Constants.HEAD));
+                       }
                        begin = 0;
                        end = blame.getResultContents().size();
                        if (rangeString != null) {
index 7b9401f3621690f868c1a9d8b9d6031e63622270..1054944ffdf7737c2d7f2e82cc4e3bd64760092d 100644 (file)
@@ -257,6 +257,8 @@ public class CLIText extends TranslationBundle {
        /***/ public String noGitRepositoryConfigured;
        /***/ public String noNamesFound;
        /***/ public String noSuchFile;
+       /***/ public String noSuchPathInRef;
+       /***/ public String noSuchRef;
        /***/ public String noSuchRemoteRef;
        /***/ public String noTREESectionInIndex;
        /***/ public String nonFastForward;