diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2019-08-01 21:04:31 +0200 |
---|---|---|
committer | Thomas Wolf <thomas.wolf@paranor.ch> | 2019-08-26 22:48:41 +0200 |
commit | bdd6cf74cfe60766500e90bc67e9a15a0c52c89a (patch) | |
tree | 7f785ddf20764d85c18dc91e6758f5612a1bb124 /org.eclipse.jgit.pgm | |
parent | cb208fb3ca130a2241bef3f3a33d54269d3a14ba (diff) | |
download | jgit-bdd6cf74cfe60766500e90bc67e9a15a0c52c89a.tar.gz jgit-bdd6cf74cfe60766500e90bc67e9a15a0c52c89a.zip |
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>
Diffstat (limited to 'org.eclipse.jgit.pgm')
3 files changed, 25 insertions, 2 deletions
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 2a5a31eba8..b2ec57c749 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 e38cb468d9..b67b04c5be 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.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) { 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 7b9401f362..1054944ffd 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 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; |