Browse Source

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 <thomas.wolf@paranor.ch>
stable-5.5
Thomas Wolf 5 years ago
parent
commit
bdd6cf74cf
  1. 122
      org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java
  2. 2
      org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
  3. 23
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java
  4. 2
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

122
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java

@ -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");
}
}

2
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. noGitRepositoryConfigured=No Git repository configured.
noNamesFound=No names found, cannot describe anything. noNamesFound=No names found, cannot describe anything.
noSuchFile=no such file: {0} noSuchFile=no such file: {0}
noSuchPathInRef=no such path ''{0}'' in {1}
noSuchRef=no such ref: {0}
noSuchRemoteRef=no such remote ref: ''{0}'' noSuchRemoteRef=no such remote ref: ''{0}''
noSystemConsoleAvailable=No System.console available noSystemConsoleAvailable=No System.console available
noTREESectionInIndex=no 'TREE' section in index noTREESectionInIndex=no 'TREE' section in index

23
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java

@ -67,6 +67,7 @@ import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.pgm.internal.CLIText;
@ -178,14 +179,28 @@ class Blame extends TextBuiltin {
} }
generator.reverse(rangeStart, rangeEnd); generator.reverse(rangeStart, rangeEnd);
} else if (revision != null) { } 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 { } 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()) { if (!db.isBare()) {
DirCache dc = db.readDirCache(); DirCache dc = db.readDirCache();
int entry = dc.findEntry(file); int entry = dc.findEntry(file);
if (0 <= entry) { if (0 <= entry) {
generator.push(null, dc.getEntry(entry).getObjectId()); 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); File inTree = new File(db.getWorkTree(), file);
@ -196,6 +211,10 @@ class Blame extends TextBuiltin {
} }
blame = BlameResult.create(generator); blame = BlameResult.create(generator);
if (blame == null) {
throw die(MessageFormat.format(CLIText.get().noSuchPathInRef,
file, revision != null ? revision : Constants.HEAD));
}
begin = 0; begin = 0;
end = blame.getResultContents().size(); end = blame.getResultContents().size();
if (rangeString != null) { if (rangeString != null) {

2
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java

@ -257,6 +257,8 @@ public class CLIText extends TranslationBundle {
/***/ public String noGitRepositoryConfigured; /***/ public String noGitRepositoryConfigured;
/***/ public String noNamesFound; /***/ public String noNamesFound;
/***/ public String noSuchFile; /***/ public String noSuchFile;
/***/ public String noSuchPathInRef;
/***/ public String noSuchRef;
/***/ public String noSuchRemoteRef; /***/ public String noSuchRemoteRef;
/***/ public String noTREESectionInIndex; /***/ public String noTREESectionInIndex;
/***/ public String nonFastForward; /***/ public String nonFastForward;

Loading…
Cancel
Save