Skip to content

Commit 9ec8a31

Browse files
author
Rob Schoening
committed
allow git.branch to return accurate information from Jenkins/Hudson jobs
1 parent 0c06cf3 commit 9ec8a31

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.io.Closeables;
2222
import com.google.common.io.Files;
23+
2324
import org.apache.maven.plugin.AbstractMojo;
2425
import org.apache.maven.plugin.MojoExecutionException;
2526
import org.apache.maven.project.MavenProject;
@@ -30,6 +31,7 @@
3031
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
34+
3335
import pl.project13.jgit.DescribeCommand;
3436
import pl.project13.jgit.DescribeResult;
3537
import pl.project13.maven.git.log.LoggerBridge;
@@ -41,6 +43,7 @@
4143
import java.text.SimpleDateFormat;
4244
import java.util.Date;
4345
import java.util.List;
46+
import java.util.Map;
4447
import java.util.Properties;
4548

4649
/**
@@ -379,7 +382,7 @@ void loadGitData(@NotNull Properties properties) throws IOException, MojoExecuti
379382

380383
try {
381384
// git.branch
382-
String branch = git.getBranch();
385+
String branch = determineBranchName(git);
383386
put(properties, BRANCH, branch);
384387

385388
// git.commit.id.describe
@@ -526,6 +529,21 @@ private boolean directoryExists(@Nullable File fileLocation) {
526529
private boolean directoryDoesNotExits(File fileLocation) {
527530
return !directoryExists(fileLocation);
528531
}
532+
533+
protected String determineBranchName(Repository git) throws IOException {
534+
String branch = git.getBranch();
535+
536+
// Special processing if we're in Jenkins/Hudson
537+
Map<String,String> env = System.getenv();
538+
if (env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL")) {
539+
String branchName = env.get("GIT_BRANCH");
540+
if (branchName!=null && branchName.length()>0) {
541+
branch=branchName;
542+
}
543+
}
544+
545+
return branch;
546+
}
529547

530548
// SETTERS FOR TESTS ----------------------------------------------------
531549

0 commit comments

Comments
 (0)