Skip to content

Commit 0c754f9

Browse files
committed
Add support for per-module versions to core
1 parent a7f65ca commit 0c754f9

File tree

5 files changed

+170
-20
lines changed

5 files changed

+170
-20
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ default Map<String, String> getSystemEnv() {
282282
boolean shouldPropertiesEscapeUnicode();
283283

284284
boolean shouldFailOnNoGitDirectory();
285+
286+
/**
287+
* When set to {@code true}, the plugin will consider only commits affecting
288+
* the folder containing this module.
289+
*
290+
* When set to {@code false}, the plugin will consider all commits in the
291+
* repository.
292+
*
293+
* @return Controls whether the plugin only considers commits in the current module's directory.
294+
*/
295+
boolean getPerModuleVersions();
296+
297+
/**
298+
* @return Base directory (folder) of the current module.
299+
*/
300+
File getModuleBaseDir();
285301
}
286302

287303
protected static final Pattern allowedCharactersForEvaluateOnCommit = Pattern.compile("[a-zA-Z0-9\\_\\-\\^\\/\\.]+");
@@ -367,6 +383,9 @@ private static void loadGitDataWithNativeGit(
367383
@Nonnull Callback cb,
368384
@Nonnull File dotGitDirectory,
369385
@Nonnull Properties properties) throws GitCommitIdExecutionException {
386+
if (cb.getPerModuleVersions()) {
387+
throw new GitCommitIdExecutionException("The native git provider does not support per module versions.");
388+
}
370389
GitDataProvider nativeGitProvider = NativeGitProvider
371390
.on(dotGitDirectory, cb.getNativeGitTimeoutInMs(), cb.getLogInterface())
372391
.setPrefixDot(cb.getPrefixDot())
@@ -378,6 +397,7 @@ private static void loadGitDataWithNativeGit(
378397
.setUseBranchNameFromBuildEnvironment(cb.getUseBranchNameFromBuildEnvironment())
379398
.setExcludeProperties(cb.getExcludeProperties())
380399
.setIncludeOnlyProperties(cb.getIncludeOnlyProperties())
400+
.setModuleBaseDir(cb.getModuleBaseDir())
381401
.setOffline(cb.isOffline());
382402

383403
nativeGitProvider.loadGitData(cb.getEvaluateOnCommit(), cb.getSystemEnv(), properties);
@@ -398,6 +418,8 @@ private static void loadGitDataWithJGit(
398418
.setUseBranchNameFromBuildEnvironment(cb.getUseBranchNameFromBuildEnvironment())
399419
.setExcludeProperties(cb.getExcludeProperties())
400420
.setIncludeOnlyProperties(cb.getIncludeOnlyProperties())
421+
.setPerModuleVersions(cb.getPerModuleVersions())
422+
.setModuleBaseDir(cb.getModuleBaseDir())
401423
.setOffline(cb.isOffline());
402424

403425
jGitProvider.loadGitData(cb.getEvaluateOnCommit(), cb.getSystemEnv(), properties);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import pl.project13.core.util.PropertyManager;
2525

2626
import javax.annotation.Nonnull;
27+
28+
import java.io.File;
2729
import java.net.URI;
2830
import java.net.URLEncoder;
2931
import java.nio.charset.StandardCharsets;
@@ -108,6 +110,20 @@ public abstract class GitDataProvider implements GitProvider {
108110
*/
109111
protected List<String> includeOnlyProperties;
110112

113+
/**
114+
* When set to {@code true}, the plugin will consider only commits affecting
115+
* the folder containing this module.
116+
*
117+
* When set to {@code false}, the plugin will consider all commits in the
118+
* repository.
119+
*/
120+
protected boolean perModuleVersions = false;
121+
122+
/**
123+
* The directory containing this project.
124+
*/
125+
protected File moduleBaseDir;
126+
111127
/**
112128
* When set to {@code true}, the plugin will not try to contact any remote repositories.
113129
* Any operations will only use the local state of the repo.
@@ -242,6 +258,31 @@ public GitDataProvider setOffline(boolean offline) {
242258
return this;
243259
}
244260

261+
/**
262+
* When set to {@code true}, the plugin will consider only commits affecting
263+
* the folder containing this module.
264+
*
265+
* When set to {@code false}, the plugin will consider all commits in the
266+
* repository.
267+
* @param perModuleVersions Only consider commits affecting the folder containing this module.
268+
* @return The {@code GitProvider} with the corresponding {@code perModuleVersions} flag set.
269+
*/
270+
public GitDataProvider setPerModuleVersions(boolean perModuleVersions) {
271+
this.perModuleVersions = perModuleVersions;
272+
return this;
273+
}
274+
275+
/**
276+
* Path to the module base directory.
277+
*
278+
* @param moduleBaseDir The path to the directory containing this module.
279+
* @return The {@code GitProvider} with the corresponding {@code moduleBaseDir} set.
280+
*/
281+
public GitDataProvider setModuleBaseDir(File moduleBaseDir) {
282+
this.moduleBaseDir = moduleBaseDir;
283+
return this;
284+
}
285+
245286
/**
246287
* Main function that will attempt to load the desired properties from the git repository.
247288
*

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

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,13 @@ public String getBuildAuthorEmail() throws GitCommitIdExecutionException {
8484
@Override
8585
public void prepareGitToExtractMoreDetailedRepoInformation() throws GitCommitIdExecutionException {
8686
try {
87-
// more details parsed out bellow
88-
Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit);
89-
ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit);
90-
91-
if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) {
92-
throw new GitCommitIdExecutionException(
93-
"Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory " +
94-
"property of this plugin to a valid path (currently set to " + dotGitDirectory + ")?");
95-
}
87+
// more details parsed out below
9688
revWalk = new RevWalk(git);
97-
ObjectId headObjectId;
98-
if (evaluateOnCommitReference != null) {
99-
headObjectId = evaluateOnCommitReference.getObjectId();
89+
if (perModuleVersions && moduleBaseDir != null) {
90+
evalCommit = getCommitFromModuleDirectory(moduleBaseDir);
10091
} else {
101-
headObjectId = evaluateOnCommitResolvedObjectId;
92+
evalCommit = getCommitFromRef();
10293
}
103-
104-
if (headObjectId == null) {
105-
throw new GitCommitIdExecutionException(
106-
"Could not get " + evaluateOnCommit + " Ref, are you sure you have some " +
107-
"commits in the dotGitDirectory (currently set to " + dotGitDirectory + ")?");
108-
}
109-
evalCommit = revWalk.parseCommit(headObjectId);
11094
revWalk.markStart(evalCommit);
11195
} catch (GitCommitIdExecutionException e) {
11296
throw e;
@@ -115,6 +99,54 @@ public void prepareGitToExtractMoreDetailedRepoInformation() throws GitCommitIdE
11599
}
116100
}
117101

102+
private RevCommit getCommitFromModuleDirectory(File moduleBaseDir) throws GitAPIException, GitCommitIdExecutionException {
103+
//retrieve last commit in folder moduleBaseDir
104+
try (Git gitInstance = new Git(git)) {
105+
String relativePath = git.getDirectory().getParentFile().toPath().relativize(moduleBaseDir.getAbsoluteFile().toPath()).toString();
106+
Iterator<RevCommit> iterator = gitInstance.log()
107+
.addPath(relativePath).call().iterator();
108+
if (!iterator.hasNext()) {
109+
throw new GitCommitIdExecutionException(
110+
"Could not get commit from folder " + relativePath + " , are you sure you have some " +
111+
"commits in the folder " + moduleBaseDir + "?");
112+
}
113+
114+
RevCommit revCommit = iterator.next();
115+
if (revCommit == null) {
116+
throw new GitCommitIdExecutionException(
117+
"Could not get commit from folder " + relativePath +
118+
" , are you sure you have some commits in the folder " + moduleBaseDir + "?");
119+
}
120+
121+
return revCommit;
122+
}
123+
}
124+
125+
private RevCommit getCommitFromRef() throws IOException, GitCommitIdExecutionException {
126+
// more details parsed out below
127+
Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit);
128+
ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit);
129+
130+
if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) {
131+
throw new GitCommitIdExecutionException(
132+
"Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory " +
133+
"property of this plugin to a valid path (currently set to " + dotGitDirectory + ")?");
134+
}
135+
ObjectId headObjectId;
136+
if (evaluateOnCommitReference != null) {
137+
headObjectId = evaluateOnCommitReference.getObjectId();
138+
} else {
139+
headObjectId = evaluateOnCommitResolvedObjectId;
140+
}
141+
142+
if (headObjectId == null) {
143+
throw new GitCommitIdExecutionException(
144+
"Could not get " + evaluateOnCommit + " Ref, are you sure you have some " +
145+
"commits in the dotGitDirectory (currently set to " + dotGitDirectory + ")?");
146+
}
147+
return revWalk.parseCommit(headObjectId);
148+
}
149+
118150
@Override
119151
public String getBranchName() throws GitCommitIdExecutionException {
120152
try {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,39 @@ public void verifyAllowedCharactersForEvaluateOnCommit() {
17431743
Assertions.assertFalse(p.matcher("&&cat /etc/passwd").matches());
17441744
}
17451745

1746+
@Test
1747+
public void shouldGiveCommitIdForEachFolderWhenPerModuleVersionsEnabled() throws Exception {
1748+
// given
1749+
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.GIT_COMMIT_ID);
1750+
1751+
GitCommitIdPlugin.Callback cbSrc =
1752+
new GitCommitIdTestCallback()
1753+
.setDotGitDirectory(dotGitDirectory)
1754+
.setUseNativeGit(false)
1755+
.setPerModuleVersions(true)
1756+
.setModuleBaseDir(dotGitDirectory.getParentFile().toPath().resolve("src").toFile())
1757+
.build();
1758+
Properties propertiesSrcFolder = new Properties();
1759+
1760+
GitCommitIdPlugin.Callback cbSrcTest =
1761+
new GitCommitIdTestCallback()
1762+
.setDotGitDirectory(dotGitDirectory)
1763+
.setUseNativeGit(false)
1764+
.setPerModuleVersions(true)
1765+
.setModuleBaseDir(dotGitDirectory.getParentFile().toPath().resolve("src/test").toFile())
1766+
.build();
1767+
Properties propertiesSrcTestFolder = new Properties();
1768+
1769+
// when
1770+
GitCommitIdPlugin.runPlugin(cbSrc, propertiesSrcFolder);
1771+
GitCommitIdPlugin.runPlugin(cbSrcTest, propertiesSrcTestFolder);
1772+
1773+
// then
1774+
assertThat(propertiesSrcFolder).containsKey("git.commit.id");
1775+
assertThat(propertiesSrcTestFolder).containsKey("git.commit.id");
1776+
assertThat(propertiesSrcFolder.getProperty("git.commit.id")).isNotEqualTo(propertiesSrcTestFolder.getProperty("git.commit.id"));
1777+
}
1778+
17461779
private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) {
17471780
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
17481781
gitDescribeConfig.setTags(true);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class GitCommitIdTestCallback {
5959
private Charset propertiesSourceCharset = StandardCharsets.UTF_8;
6060
private boolean shouldPropertiesEscapeUnicode = false;
6161
private boolean shouldFailOnNoGitDirectory = false;
62+
private boolean perModuleVersions = false;
63+
private File moduleBaseDir;
6264

6365
public GitCommitIdTestCallback() {
6466
try {
@@ -200,6 +202,16 @@ public GitCommitIdTestCallback setShouldFailOnNoGitDirectory(boolean shouldFailO
200202
return this;
201203
}
202204

205+
public GitCommitIdTestCallback setPerModuleVersions(boolean perModuleVersions) {
206+
this.perModuleVersions = perModuleVersions;
207+
return this;
208+
}
209+
210+
public GitCommitIdTestCallback setModuleBaseDir(File moduleBaseDir) {
211+
this.moduleBaseDir = moduleBaseDir;
212+
return this;
213+
}
214+
203215
public GitCommitIdPlugin.Callback build() {
204216
return new GitCommitIdPlugin.Callback() {
205217
@Override
@@ -353,6 +365,16 @@ public boolean shouldPropertiesEscapeUnicode() {
353365
public boolean shouldFailOnNoGitDirectory() {
354366
return shouldFailOnNoGitDirectory;
355367
}
368+
369+
@Override
370+
public boolean getPerModuleVersions() {
371+
return perModuleVersions;
372+
}
373+
374+
@Override
375+
public File getModuleBaseDir() {
376+
return moduleBaseDir;
377+
}
356378
};
357379
}
358380

0 commit comments

Comments
 (0)