Skip to content

Commit

Permalink
fix: ensure that jGit finds the .git directory (#25)
Browse files Browse the repository at this point in the history
* Always pass a basePath when performing a Git operation.

* chore(release): [patch]

* chore: attempt to trigger GH actions

* fix: tests and formatting

closes #24 

---------

Co-authored-by: Olivier Hubaut <olivier@hubaut.info>
Co-authored-by: Manik Magar <manik.magar@gmail.com>
  • Loading branch information
3 people authored Dec 3, 2023
1 parent d4132d6 commit 5b6e886
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import org.eclipse.jgit.lib.Ref;

import java.io.File;

public class GitTag {

private GitTag() {

}

public static String create(final String tagName, final String tagMessage) {
return JGit.executeOperation(git -> {
public static String create(File basePath, final String tagName, final String tagMessage) {
return JGit.executeOperation(basePath, git -> {
Ref tag = git.tag().setName(tagName).setMessage(tagMessage).call();
return String.format("%s@%s", tag.getName(), tag.getObjectId().getName());
});
}

public static boolean exists(final String tagName) {
return JGit.executeOperation(git -> null != git.getRepository().findRef("refs/tags/" + tagName));
public static boolean exists(File basePath, final String tagName) {
return JGit.executeOperation(basePath, git -> null != git.getRepository().findRef("refs/tags/" + tagName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class JGit {
private JGit() {
}

public static <R> R executeOperation(Operation<Git, R> work) throws GitVersionerException {
try (Repository repository = new FileRepositoryBuilder().readEnvironment().findGitDir().build()) {
public static <R> R executeOperation(File basePath, Operation<Git, R> work) throws GitVersionerException {
try (Repository repository = new FileRepositoryBuilder().readEnvironment().findGitDir(basePath).build()) {
try (Git git = new Git(repository)) {
return work.apply(git);
}
Expand All @@ -24,8 +24,6 @@ public static <R> R executeOperation(Operation<Git, R> work) throws GitVersioner

/**
* Find local git dir by scanning upwards to find .git dir
*
* @return
*/
public static String findGitDir(String basePath) {
try (Repository repository = new FileRepositoryBuilder().readEnvironment().findGitDir(new File(basePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class JGitVersioner implements Versioner {

File basePath;
VersionConfig versionConfig;

public JGitVersioner(VersionConfig versionConfig) {
public JGitVersioner(File basePath, VersionConfig versionConfig) {
this.versionConfig = versionConfig;
this.basePath = basePath;
}

public VersionStrategy version() {
return JGit.executeOperation(git -> {
return JGit.executeOperation(basePath, git -> {
var branch = git.getRepository().getBranch();
Ref head = git.getRepository().findRef("HEAD");
var hash = "";
Expand All @@ -39,7 +42,7 @@ public VersionStrategy version() {
versionStrategy.increment(VersionComponentType.MAJOR, hash);
} else if (hasValue(commit.getFullMessage(), versionConfig.getKeywords().getMinorKey())) {
versionStrategy.increment(VersionComponentType.MINOR, hash);
} else if (hasValue(commit.getFullMessage(),versionConfig.getKeywords().getPatchKey())) {
} else if (hasValue(commit.getFullMessage(), versionConfig.getKeywords().getPatchKey())) {
versionStrategy.increment(VersionComponentType.PATCH, hash);
} else {
versionStrategy.increment(VersionComponentType.COMMIT, hash);
Expand All @@ -50,9 +53,9 @@ public VersionStrategy version() {
}

boolean hasValue(String commitMessage, String keyword) {
if(versionConfig.getKeywords().isUseRegex()){
if (versionConfig.getKeywords().isUseRegex()) {
return commitMessage.matches(keyword);
}else {
} else {
return commitMessage.contains(keyword);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@
import com.github.manikmagar.maven.versioner.core.params.VersionConfig;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.assertj.core.util.Files;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JUnitParamsRunner.class)
public class JGitVersionerTest {
private VersionConfig versionConfig = new VersionConfig();;
private JGitVersioner jGitVersioner = new JGitVersioner(versionConfig);

@Test
@Parameters(value = {
"Fix: [minor] corrected typo, .*\\[minor\\].*, true",
"[minor] Fix: corrected typo, ^\\[minor\\].*, true",
"Fix: [minor] corrected typo, ^\\[minor\\].*, false",
"Update: improved performance, [minor], false"})
public void testHasValueWithRegex(String commitMessage, String keyword, boolean expected) {
versionConfig.getKeywords().setUseRegex(true);

assertThat(jGitVersioner.hasValue(commitMessage, keyword)).isEqualTo(expected);
}

@Test
public void testHasValueWithoutRegex() {
versionConfig.getKeywords().setUseRegex(false);

String commitMessage = "Fix: [minor] corrected typo";
assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isTrue();

commitMessage = "Update: improved performance";
assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isFalse();
}
private VersionConfig versionConfig = new VersionConfig();
private JGitVersioner jGitVersioner = new JGitVersioner(Files.currentFolder(), versionConfig);

@Test
@Parameters(value = {"Fix: [minor] corrected typo, .*\\[minor\\].*, true",
"[minor] Fix: corrected typo, ^\\[minor\\].*, true", "Fix: [minor] corrected typo, ^\\[minor\\].*, false",
"Update: improved performance, [minor], false"})
public void testHasValueWithRegex(String commitMessage, String keyword, boolean expected) {
versionConfig.getKeywords().setUseRegex(true);

assertThat(jGitVersioner.hasValue(commitMessage, keyword)).isEqualTo(expected);
}

@Test
public void testHasValueWithoutRegex() {
versionConfig.getKeywords().setUseRegex(false);

String commitMessage = "Fix: [minor] corrected typo";
assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isTrue();

commitMessage = "Update: improved performance";
assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void setKeywords() {
VersionKeywords versionKeywords = new VersionKeywords();
versionKeywords.setMajorKey("[TEST]");
versionConfig.setKeywords(versionKeywords);
assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex").containsExactly("[TEST]",
KEY_MINOR, KEY_PATCH, false);
assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex")
.containsExactly("[TEST]", KEY_MINOR, KEY_PATCH, false);
}

@Test
Expand All @@ -46,7 +46,7 @@ public void setUseRegEx() {
VersionKeywords versionKeywords = new VersionKeywords();
versionKeywords.setUseRegex(true);
versionConfig.setKeywords(versionKeywords);
assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex").containsExactly(KEY_MAJOR,
KEY_MINOR, KEY_PATCH, true);
assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex")
.containsExactly(KEY_MAJOR, KEY_MINOR, KEY_PATCH, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class VersionKeywordsTest {

@Test
public void defaultKeywords() {
assertThat(new VersionKeywords()).extracting("majorKey", "minorKey", "patchKey","useRegex").containsExactly(KEY_MAJOR,
KEY_MINOR, KEY_PATCH, false);
assertThat(new VersionKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex")
.containsExactly(KEY_MAJOR, KEY_MINOR, KEY_PATCH, false);
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions git-versioner-maven-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private Model processModel(Model projectModel, Map<String, ?> options) {
LOGGER.info(MessageUtils.buffer().a("--- ").mojo(extensionGAV).a(" ").strong("[core-extension]").a(" ---")
.toString());
versionConfig = loadConfig();
versionStrategy = new JGitVersioner(versionConfig).version();
versionStrategy = new JGitVersioner(projectModel.getPomFile().getAbsoluteFile(), versionConfig).version();
findRelatedProjects(projectModel);
initialized = true;
}
Expand Down Expand Up @@ -187,8 +187,7 @@ private void addPluginConfiguration(Plugin plugin) {
String config = String.format(
"<configuration> <versionConfig> <keywords> <majorKey>%s</majorKey>"
+ " <minorKey>%s</minorKey> <patchKey>%s</patchKey>"
+ " <useRegex>%s</useRegex>"
+ " </keywords> </versionConfig></configuration>",
+ " <useRegex>%s</useRegex>" + " </keywords> </versionConfig></configuration>",
versionConfig.getKeywords().getMajorKey(), versionConfig.getKeywords().getMinorKey(),
versionConfig.getKeywords().getPatchKey(), versionConfig.getKeywords().isUseRegex());
try {
Expand All @@ -212,7 +211,7 @@ private VersionConfig loadConfig() {
keywords.setMajorKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_MAJOR_KEY));
keywords.setMinorKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_MINOR_KEY));
keywords.setPatchKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_PATCH_KEY));
keywords.setUseRegex(properties.getProperty(VersionKeywords.GV_KEYWORDS_KEY_USEREGEX,"false").equals("true"));
keywords.setUseRegex(properties.getProperty(VersionKeywords.GV_KEYWORDS_KEY_USEREGEX, "false").equals("true"));
coreVersionConfig.setKeywords(keywords);

VersionPattern versionPattern = new VersionPattern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void setMavenProject(MavenProject mavenProject) {
}

protected Versioner getVersioner() {
return new JGitVersioner(getVersionConfig().toVersionConfig());
return new JGitVersioner(mavenProject.getBasedir().getAbsoluteFile(), getVersionConfig().toVersionConfig());
}

protected String replaceTokens(String pattern, VersionStrategy versionStrategy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
var tagMessage = replaceTokens(getTagMessagePattern(), versionStrategy);
getLog().info("Current Version: " + versionStrategy.toVersionString());
getLog().info(String.format("Tag Version '%s' with message '%s'", tagName, tagMessage));
if (GitTag.exists(tagName)) {
if (GitTag.exists(mavenProject.getBasedir().getAbsoluteFile(), tagName)) {
getLog().error(String.format("Tag already exist: %s", tagName));
if (isFailWhenTagExist())
throw new GitVersionerException("Tag already exist: " + tagName);
} else {
String tagId = GitTag.create(tagName, tagMessage);
String tagId = GitTag.create(mavenProject.getBasedir().getAbsoluteFile(), tagName, tagMessage);
getLog().info(String.format("Created tag: '%s'", tagId));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
import com.github.manikmagar.maven.versioner.plugin.mojo.params.VersionConfigParam;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;

import com.github.manikmagar.maven.versioner.core.params.InitialVersion;
import com.github.manikmagar.maven.versioner.core.params.VersionConfig;
import java.io.File;

import static com.github.manikmagar.maven.versioner.core.params.VersionKeywords.*;
import static org.assertj.core.api.Assertions.assertThat;

public class AbstractVersionerMojoTest {

private AbstractVersionerMojo testMojo = new AbstractVersionerMojo() {

{
mavenProject = new MavenProject();
mavenProject.setFile(new File("my/pom.xml"));
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class VersionKeywordsParamTest {

@Test
public void newVersionKeywords() {
assertThat(new VersionKeywords("[big]", "[medium]", "[small]", false)).extracting("majorKey", "minorKey", "patchKey")
.containsExactly("[big]", "[medium]", "[small]");
assertThat(new VersionKeywords("[big]", "[medium]", "[small]", false))
.extracting("majorKey", "minorKey", "patchKey").containsExactly("[big]", "[medium]", "[small]");
}

@Test
Expand Down

0 comments on commit 5b6e886

Please sign in to comment.