Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/pl/project13/jgit/DescribeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public DescribeCommand always(boolean always) {
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
if (forceLongFormat != null) {
this.forceLongFormat = forceLongFormat;
log("--long = %s", forceLongFormat);
log("--long =", forceLongFormat);
}
return this;
}
Expand Down Expand Up @@ -312,9 +312,9 @@ public DescribeResult call() throws GitAPIException {
// check if dirty
boolean dirty = findDirtyState(repo);

if (hasTags(headCommit, tagObjectIdToName)) {
if (hasTags(headCommit, tagObjectIdToName) && !forceLongFormat) {
String tagName = tagObjectIdToName.get(headCommit).iterator().next();
log("The commit we're on is a Tag ([",tagName,"]), returning.");
log("The commit we're on is a Tag ([",tagName,"]) and forceLongFormat == false, returning.");

return new DescribeResult(tagName, dirty, dirtyOption);
}
Expand Down Expand Up @@ -351,7 +351,7 @@ private DescribeResult createDescribeResult(ObjectReader objectReader, ObjectId
.withCommitIdAbbrev(abbrev);

} else if (howFarFromWhichTag.first > 0 || forceLongFormat) {
return new DescribeResult(objectReader, howFarFromWhichTag.second, howFarFromWhichTag.first, headCommitId, dirty, dirtyOption)
return new DescribeResult(objectReader, howFarFromWhichTag.second, howFarFromWhichTag.first, headCommitId, dirty, dirtyOption, forceLongFormat)
.withCommitIdAbbrev(abbrev); // we're a bit away from a tag

} else if (howFarFromWhichTag.first == 0) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/pl/project13/jgit/DescribeResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class DescribeResult {
private boolean dirty;
private String dirtyMarker;

private boolean forceLongFormat;

private ObjectReader objectReader;

public static final DescribeResult EMPTY = new DescribeResult("");
Expand All @@ -58,7 +60,7 @@ public DescribeResult(@NotNull String tagName) {
}

public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, @Nullable ObjectId commitId) {
this(objectReader, tagName, commitsAwayFromTag, commitId, false, Optional.<String>absent());
this(objectReader, tagName, commitsAwayFromTag, commitId, false, Optional.<String>absent(), false);
}

public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId commitId) {
Expand All @@ -69,13 +71,14 @@ public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId comm
}

public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, String dirtyMarker) {
this(objectReader, tagName, commitsAwayFromTag, commitId, dirty, Optional.of(dirtyMarker));
this(objectReader, tagName, commitsAwayFromTag, commitId, dirty, Optional.of(dirtyMarker), false);
}

public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, Optional<String> dirtyMarker) {
public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, Optional<String> dirtyMarker, boolean forceLongFormat) {
this(objectReader, commitId, dirty, dirtyMarker);
this.tagName = Optional.of(tagName);
this.commitsAwayFromTag = commitsAwayFromTag;
this.forceLongFormat = forceLongFormat;
}

public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId commitId, boolean dirty, @NotNull Optional<String> dirtyMarker) {
Expand Down Expand Up @@ -145,6 +148,9 @@ private boolean abbrevZeroHidesCommitsPartOfDescribe() {

@Nullable
public String commitsAwayFromTag() {
if (forceLongFormat) {
return String.valueOf(commitsAwayFromTag);
}
return commitsAwayFromTag == 0 ? null : String.valueOf(commitsAwayFromTag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.Properties;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.fest.assertions.MapAssert.entry;
import static org.mockito.internal.util.reflection.Whitebox.setInternalState;

public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest {
Expand Down Expand Up @@ -250,6 +250,54 @@ public void shouldNotSkipWithoutFailOnNoGitDirectoryWhenNoGitRepoIsPresent() thr
assertGitPropertiesPresentInProject(targetProject.getProperties());
}

@Test
public void shouldGenerateDescribeWithTagOnlyWhenForceLongFormatIsFalse() throws Exception {
// given
mavenSandbox.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG)
.create(CleanUp.CLEANUP_FIRST);

MavenProject targetProject = mavenSandbox.getChildProject();

setProjectToExecuteMojoIn(targetProject);
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
gitDescribeConfig.setTags(true);
gitDescribeConfig.setForceLongFormat(false);
gitDescribeConfig.setAbbrev(7);
alterMojoSettings("gitDescribe", gitDescribeConfig);

// when
mojo.execute();

// then
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0"));
}

@Test
public void shouldGenerateDescribeWithTagAndZeroAndCommitIdWhenForceLongFormatIsTrue() throws Exception {
// given
mavenSandbox.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG)
.create(CleanUp.CLEANUP_FIRST);

MavenProject targetProject = mavenSandbox.getChildProject();

setProjectToExecuteMojoIn(targetProject);
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
gitDescribeConfig.setTags(true);
gitDescribeConfig.setForceLongFormat(true);
gitDescribeConfig.setAbbrev(7);
alterMojoSettings("gitDescribe", gitDescribeConfig);

// when
mojo.execute();

// then
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0-0-gde4db35"));
}

private void alterMojoSettings(String parameterName, Object parameterValue) {
setInternalState(mojo, parameterName, parameterValue);
}
Expand Down