From bdd6cf74cfe60766500e90bc67e9a15a0c52c89a Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Thu, 1 Aug 2019 21:04:31 +0200 Subject: [PATCH] JGit pgm: make Blame more robust against bogus input 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 --- .../tst/org/eclipse/jgit/pgm/BlameTest.java | 122 ++++++++++++++++++ .../jgit/pgm/internal/CLIText.properties | 2 + .../src/org/eclipse/jgit/pgm/Blame.java | 23 +++- .../eclipse/jgit/pgm/internal/CLIText.java | 2 + 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.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 index 000000000..e806872c1 --- /dev/null +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2019 Thomas Wolf + * 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"); + } +} diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties index 2a5a31eba..b2ec57c74 100644 --- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties +++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties @@ -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 diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java index e38cb468d..b67b04c5b 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java @@ -67,6 +67,7 @@ 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 @@ protected void run() { } 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 @@ protected void run() { } 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) { diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java index 7b9401f36..1054944ff 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java @@ -257,6 +257,8 @@ public static String fatalError(String message) { /***/ public String noGitRepositoryConfigured; /***/ public String noNamesFound; /***/ public String noSuchFile; + /***/ public String noSuchPathInRef; + /***/ public String noSuchRef; /***/ public String noSuchRemoteRef; /***/ public String noTREESectionInIndex; /***/ public String nonFastForward;