Skip to content

Commit 698937a

Browse files
committed
Add per module versions support to native git provider
1 parent d1d52c6 commit 698937a

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/main/java/pl/project13/core/GitCommitIdPlugin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,6 @@ private static void loadGitDataWithNativeGit(
383383
@Nonnull Callback cb,
384384
@Nonnull File dotGitDirectory,
385385
@Nonnull Properties properties) throws GitCommitIdExecutionException {
386-
if (cb.getPerModuleVersions()) {
387-
throw new GitCommitIdExecutionException("The native git provider does not support per module versions.");
388-
}
389386
GitDataProvider nativeGitProvider = NativeGitProvider
390387
.on(dotGitDirectory, cb.getNativeGitTimeoutInMs(), cb.getLogInterface())
391388
.setPrefixDot(cb.getPrefixDot())
@@ -397,6 +394,7 @@ private static void loadGitDataWithNativeGit(
397394
.setUseBranchNameFromBuildEnvironment(cb.getUseBranchNameFromBuildEnvironment())
398395
.setExcludeProperties(cb.getExcludeProperties())
399396
.setIncludeOnlyProperties(cb.getIncludeOnlyProperties())
397+
.setPerModuleVersions(cb.getPerModuleVersions())
400398
.setModuleBaseDir(cb.getModuleBaseDir())
401399
.setOffline(cb.isOffline());
402400

src/main/java/pl/project13/core/NativeGitProvider.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class NativeGitProvider extends GitDataProvider {
4343

4444
final File canonical;
4545

46+
private String moduleRelativePath;
47+
4648
@Nonnull
4749
public static NativeGitProvider on(@Nonnull File dotGitDirectory, long nativeGitTimeoutInMs, @Nonnull LogInterface log) {
4850
return new NativeGitProvider(dotGitDirectory, nativeGitTimeoutInMs, log);
@@ -90,6 +92,19 @@ public String getBuildAuthorEmail() throws GitCommitIdExecutionException {
9092

9193
@Override
9294
public void prepareGitToExtractMoreDetailedRepoInformation() throws GitCommitIdExecutionException {
95+
if (perModuleVersions && moduleBaseDir != null) {
96+
// For per-module versions, we need to determine the relative path of the module
97+
// This will be used in git commands with the -- pathspec limiter
98+
try {
99+
File gitRoot = canonical.getParentFile();
100+
String relativePath = gitRoot.getAbsoluteFile().toPath()
101+
.relativize(moduleBaseDir.getAbsoluteFile().toPath()).toString();
102+
// Store the relative path for use in git commands
103+
this.moduleRelativePath = relativePath.isEmpty() ? null : relativePath;
104+
} catch (Exception e) {
105+
throw new GitCommitIdExecutionException("Unable to compute module relative path", e);
106+
}
107+
}
93108
}
94109

95110
@Override
@@ -196,15 +211,16 @@ private String getArgumentsForGitDescribe(GitDescribeConfig describeConfig) {
196211
@Override
197212
public String getCommitId() throws GitCommitIdExecutionException {
198213
boolean evaluateOnCommitIsSet = evalCommitIsNotHead();
214+
String pathspec = moduleRelativePath != null ? " -- " + moduleRelativePath : "";
199215
if (evaluateOnCommitIsSet) {
200216
// if evaluateOnCommit represents a tag we need to perform the rev-parse on the actual commit reference
201217
// in case evaluateOnCommit is not a reference rev-list will just return the argument given
202218
// and thus it's always safe(r) to unwrap it
203219
// however when evaluateOnCommit is not set we don't want to waste calls to the native binary
204-
String actualCommitId = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list -n 1 " + evaluateOnCommit);
220+
String actualCommitId = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list -n 1 " + evaluateOnCommit + pathspec);
205221
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-parse " + actualCommitId);
206222
} else {
207-
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-parse HEAD");
223+
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-parse HEAD" + pathspec);
208224
}
209225
}
210226

@@ -224,7 +240,8 @@ public String getAbbrevCommitId() throws GitCommitIdExecutionException {
224240

225241
@Override
226242
public boolean isDirty() throws GitCommitIdExecutionException {
227-
return !tryCheckEmptyRunGitCommand(canonical, nativeGitTimeoutInMs, "status -s");
243+
String pathspec = moduleRelativePath != null ? " -- " + moduleRelativePath : "";
244+
return !tryCheckEmptyRunGitCommand(canonical, nativeGitTimeoutInMs, "status -s" + pathspec);
228245
}
229246

230247
@Override
@@ -311,14 +328,16 @@ public String getClosestTagName() throws GitCommitIdExecutionException {
311328
public String getClosestTagCommitCount() throws GitCommitIdExecutionException {
312329
String closestTagName = getClosestTagName();
313330
if (closestTagName != null && !closestTagName.trim().isEmpty()) {
314-
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list " + closestTagName + ".." + evaluateOnCommit + " --count");
331+
String pathspec = moduleRelativePath != null ? " -- " + moduleRelativePath : "";
332+
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list " + closestTagName + ".." + evaluateOnCommit + " --count" + pathspec);
315333
}
316334
return "";
317335
}
318336

319337
@Override
320338
public String getTotalCommitCount() throws GitCommitIdExecutionException {
321-
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list " + evaluateOnCommit + " --count");
339+
String pathspec = moduleRelativePath != null ? " -- " + moduleRelativePath : "";
340+
return runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list " + evaluateOnCommit + " --count" + pathspec);
322341
}
323342

324343
@Override

src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,14 +1766,37 @@ public void shouldGiveCommitIdForEachFolderWhenPerModuleVersionsEnabled() throws
17661766
.build();
17671767
Properties propertiesSrcTestFolder = new Properties();
17681768

1769+
GitCommitIdPlugin.Callback cbSrcNative =
1770+
new GitCommitIdTestCallback()
1771+
.setDotGitDirectory(dotGitDirectory)
1772+
.setUseNativeGit(true)
1773+
.setPerModuleVersions(true)
1774+
.setModuleBaseDir(dotGitDirectory.getParentFile().toPath().resolve("src").toFile())
1775+
.build();
1776+
Properties propertiesSrcFolderNative = new Properties();
1777+
1778+
GitCommitIdPlugin.Callback cbSrcTestNative =
1779+
new GitCommitIdTestCallback()
1780+
.setDotGitDirectory(dotGitDirectory)
1781+
.setUseNativeGit(true)
1782+
.setPerModuleVersions(true)
1783+
.setModuleBaseDir(dotGitDirectory.getParentFile().toPath().resolve("src/test").toFile())
1784+
.build();
1785+
Properties propertiesSrcTestFolderNative = new Properties();
1786+
17691787
// when
17701788
GitCommitIdPlugin.runPlugin(cbSrc, propertiesSrcFolder);
17711789
GitCommitIdPlugin.runPlugin(cbSrcTest, propertiesSrcTestFolder);
1790+
GitCommitIdPlugin.runPlugin(cbSrcNative, propertiesSrcFolderNative);
1791+
GitCommitIdPlugin.runPlugin(cbSrcTestNative, propertiesSrcTestFolderNative);
17721792

17731793
// then
17741794
assertThat(propertiesSrcFolder).containsKey("git.commit.id");
17751795
assertThat(propertiesSrcTestFolder).containsKey("git.commit.id");
17761796
assertThat(propertiesSrcFolder.getProperty("git.commit.id")).isNotEqualTo(propertiesSrcTestFolder.getProperty("git.commit.id"));
1797+
assertThat(propertiesSrcFolderNative).containsKey("git.commit.id");
1798+
assertThat(propertiesSrcTestFolderNative).containsKey("git.commit.id");
1799+
assertThat(propertiesSrcFolderNative.getProperty("git.commit.id")).isNotEqualTo(propertiesSrcTestFolderNative.getProperty("git.commit.id"));
17771800
}
17781801

17791802
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {

0 commit comments

Comments
 (0)