Skip to content
Browse files
JENKINS-10055: NPE when commit to build is first commit in repository
When the commit to build is the first commit in the repository, it
does not have a parent commit.  In this case use the revision of
the commit itself as parent.

Change-Id: I167c02f10e2b657433da63323e04cc7f77733d65
  • Loading branch information
dpursehouse committed Jul 5, 2012
1 parent a6d458b commit ba602dfede0cd21e58d77a328e0c0b747fbf9dbc
Showing with 12 additions and 2 deletions.
  1. +12 −2 .../java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/
@@ -94,17 +94,27 @@ public Build prevBuildForChangelog(String singleBranch, BuildData data, IGitAPI
return newLastBuild;

//CS IGNORE RedundantThrows FOR NEXT 20 LINES. REASON: Informative, and could happen.
//CS IGNORE RedundantThrows FOR NEXT 30 LINES. REASON: Informative, and could happen.
* Gets the top parent of the given revision.
* @param revName Revision
* @param git GitAPI object
* @return object id for parent
* @return object id of Revision's parent, or of Revision itself if there is no parent
* @throws GitException In case of error in git call
private ObjectId getFirstParent(String revName, IGitAPI git) throws GitException {
String result = ((GitAPI)git).launchCommand("log", "-1", "--pretty=format:%P", revName);
// If this is the first commit in the git, there is no parent and the
// git log command returns an empty string, which will cause NPE later.
if (result.isEmpty()) {
// Get the revision of this commit instead. If git log still returns
// an empty string, raise an exception.
result = ((GitAPI)git).launchCommand("log", "-1", "--pretty=format:%H");
if (result.isEmpty()) {
throw new GitException("git log returned an empty string");
String parents = firstLine(result).trim();
String firstParent = parents.split(" ")[0];
return ObjectId.fromString(firstParent);

0 comments on commit ba602df

Please sign in to comment.