Don't fail build if git rev-parse throws exception.
package hudson.plugins.analysis.util;
+import javax.annotation.CheckForNull;
import java.io.IOException;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -20,6 +22,7 @@ import org.jenkinsci.plugins.gitclient.RepositoryCallback;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.TaskListener;
+import hudson.plugins.git.GitException;
import hudson.remoting.VirtualChannel;
/**
@@ -55,10 +58,16 @@ public class GitBlamer extends AbstractBlamer {
private Map<String, BlameResult> loadBlameResultsForFiles(final Map<String, BlameRequest> linesOfConflictingFiles)
throws InterruptedException, IOException {
GitClient git = Git.with(getListener(), getEnvironment()).in(getWorkspace()).using(pathToGit).getClient();
- return git.withRepository(new BlameCallback(this, getHead(git), linesOfConflictingFiles.values()));
+ ObjectId headCommit = getHead(git);
+ if (headCommit == null) {
+ return Collections.emptyMap();
+ }
+ return git.withRepository(new BlameCallback(this, headCommit, linesOfConflictingFiles.values()));
}
+ @CheckForNull
private ObjectId getHead(final GitClient git) throws InterruptedException {
+ try {
String gitCommit = getEnvironment().get("GIT_COMMIT");
ObjectId headCommit;
if (StringUtils.isBlank(gitCommit)) {
@@ -71,6 +80,11 @@ public class GitBlamer extends AbstractBlamer {
}
return headCommit;
}
+ catch (GitException exception) {
+ log("Can't determine head commit using 'git rev-parse'. Skipping blame. %n%s%n", exception.getMessage());
+ return null;
+ }
+ }
private Map<String, BlameRequest> fillBlameResults(final Map<String, BlameRequest> linesOfConflictingFiles,
final Map<String, BlameResult> blameResults) {
