Skip to content

Commit

Permalink
git-commit-id#365 set ahead and behind properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jonevn committed Nov 3, 2018
1 parent 0f0a6be commit eec3570
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/main/java/pl/project13/maven/git/AheadBehind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pl.project13.maven.git;

import java.util.Objects;

public class AheadBehind {

public static final AheadBehind NO_REMOTE = AheadBehind.of("NO_REMOTE", "NO_REMOTE");

private final String ahead;

private final String behind;

private AheadBehind(String ahead, String behind) {
this.ahead = ahead;
this.behind = behind;
}

public static AheadBehind of(int ahead, int behind) {
return new AheadBehind(String.valueOf(ahead), String.valueOf(behind));
}

public static AheadBehind of(String ahead, String behind) {
return new AheadBehind(ahead, behind);
}

public String ahead() {
return ahead;
}

public String behind() {
return behind;
}

@Override
public int hashCode() {
return Objects.hash(ahead, behind);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AheadBehind other = (AheadBehind) obj;

if (!Objects.equals(ahead, other.ahead)) {
return false;
}
if (!Objects.equals(behind, other.behind)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class GitCommitPropertyConstant {
// these properties will be exposed to maven
public static final String BRANCH = "branch";
public static final String BRANCH_AHEAD = "branch.ahead";
public static final String BRANCH_BEHIND = "branch.behind";
public static final String DIRTY = "dirty";
// only one of the following two will be exposed, depending on the commitIdGenerationMode
public static final String COMMIT_ID_FLAT = "commit.id";
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/pl/project13/maven/git/GitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties pr
put(properties,GitCommitPropertyConstant.CLOSEST_TAG_COMMIT_COUNT, getClosestTagCommitCount());

put(properties,GitCommitPropertyConstant.TOTAL_COMMIT_COUNT, getTotalCommitCount());
try {
AheadBehind aheadBehind = getAheadBehind();
put(properties, GitCommitPropertyConstant.BRANCH_AHEAD, aheadBehind.ahead());
put(properties, GitCommitPropertyConstant.BRANCH_BEHIND, aheadBehind.behind());
} catch (Exception e) {
log.error("Failed to read ahead behind", e);
}
} finally {
finalCleanUp();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/pl/project13/maven/git/GitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ public interface GitProvider {
String getTotalCommitCount() throws GitCommitIdExecutionException;

void finalCleanUp() throws GitCommitIdExecutionException;

AheadBehind getAheadBehind() throws GitCommitIdExecutionException;

}
24 changes: 24 additions & 0 deletions src/main/java/pl/project13/maven/git/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

import com.google.common.annotations.VisibleForTesting;

import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.BranchTrackingStatus;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
Expand Down Expand Up @@ -286,6 +289,27 @@ private Repository getGitRepository() throws GitCommitIdExecutionException {

return repository;
}

@Override
public AheadBehind getAheadBehind() throws GitCommitIdExecutionException {
try {
fetch();
Optional<BranchTrackingStatus> branchTrackingStatus = Optional.ofNullable(BranchTrackingStatus.of(git, getBranchName()));
return branchTrackingStatus.map(bts -> AheadBehind.of(bts.getAheadCount(), bts.getBehindCount()))
.orElse(AheadBehind.NO_REMOTE);
} catch (Exception e) {
throw new GitCommitIdExecutionException("Failed to read ahead behind count: " + e.getMessage(), e);
}
}

private void fetch() {
FetchCommand fetchCommand = Git.wrap(git).fetch();
try {
fetchCommand.setThin(true).call();
} catch (Exception e) {
log.error("Failed to perform fetch", e);
}
}

// SETTERS FOR TESTS ----------------------------------------------------

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/pl/project13/maven/git/NativeGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Optional;
import java.util.concurrent.TimeUnit;


Expand Down Expand Up @@ -465,5 +466,39 @@ public boolean runEmpty(File directory, long nativeGitTimeoutInMs, String comman
return empty; // was non-empty
}
}

@Override
public AheadBehind getAheadBehind() throws GitCommitIdExecutionException {
try {
Optional<String> remoteBranch = remoteBranch();
if (!remoteBranch.isPresent()) {
return AheadBehind.NO_REMOTE;
}
fetch(remoteBranch.get());
String ahead = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list --right-only --count " + remoteBranch.get() + "..." + getBranchName());
String behind = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list --left-only --count " + remoteBranch.get() + "..." + getBranchName());
return AheadBehind.of(ahead, behind);
} catch (Exception e) {
throw new GitCommitIdExecutionException("Failed to read ahead behind count: " + e.getMessage(), e);
}
}

private Optional<String> remoteBranch() {
try {
String remoteRef = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "symbolic-ref -q HEAD");
String remoteBranch = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "for-each-ref --format=%(upstream:short) " + remoteRef);
return Optional.ofNullable(remoteBranch.isEmpty() ? null : remoteBranch);
} catch (Exception e) {
return Optional.empty();
}
}

private void fetch(String remoteBranch) {
try {
runQuietGitCommand(canonical, nativeGitTimeoutInMs, "fetch " + remoteBranch.replaceFirst("/", " "));
} catch (Exception e) {
log.error("Failed to execute fetch", e);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class NativeAndJGitProviderTest extends GitIntegrationTest {
"git.commit.message.short",
"git.commit.time",
"git.total.commit.count",
"git.remote.origin.url"
"git.remote.origin.url",
"git.branch.ahead",
"git.branch.behind"
};

public static final String DEFAULT_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssZ";
Expand Down Expand Up @@ -87,7 +89,7 @@ public void testCompareSubrepoInChild() throws Exception {
}

@Test
public void testCompareISO8601Time() throws Exception {
public void testCompareIso8601Time() throws Exception {
// Test on all available basic repos to ensure that the output is identical.
for (AvailableGitTestRepo testRepo : AvailableGitTestRepo.values()) {
if (testRepo != AvailableGitTestRepo.GIT_COMMIT_ID) {
Expand Down

0 comments on commit eec3570

Please sign in to comment.