diff --git a/core/src/main/java/pl/project13/core/GitDataProvider.java b/core/src/main/java/pl/project13/core/GitDataProvider.java index bf90b0ac..db4b7d92 100644 --- a/core/src/main/java/pl/project13/core/GitDataProvider.java +++ b/core/src/main/java/pl/project13/core/GitDataProvider.java @@ -60,6 +60,8 @@ public abstract class GitDataProvider implements GitProvider { protected boolean offline; + protected String projectDirectory; + public GitDataProvider(@Nonnull LoggerBridge log) { this.log = log; } @@ -114,6 +116,11 @@ public GitDataProvider setOffline(boolean offline) { return this; } + public GitDataProvider setProjectDirectory(String projectDirectory) { + this.projectDirectory = projectDirectory; + return this; + } + public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties properties) throws GitCommitIdExecutionException { this.evaluateOnCommit = evaluateOnCommit; init(); diff --git a/core/src/main/java/pl/project13/core/JGitProvider.java b/core/src/main/java/pl/project13/core/JGitProvider.java index 616793bf..66a0e384 100644 --- a/core/src/main/java/pl/project13/core/JGitProvider.java +++ b/core/src/main/java/pl/project13/core/JGitProvider.java @@ -82,31 +82,54 @@ public String getBuildAuthorEmail() throws GitCommitIdExecutionException { @Override public void prepareGitToExtractMoreDetailedRepoInformation() throws GitCommitIdExecutionException { try { - // more details parsed out bellow - Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit); - ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit); - - if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) { - throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory property of this plugin to a valid path?"); - } revWalk = new RevWalk(git); - ObjectId headObjectId; - if (evaluateOnCommitReference != null) { - headObjectId = evaluateOnCommitReference.getObjectId(); + if (projectDirectory != null) { + evalCommit = getCommitFromProjectDirectory(projectDirectory); } else { - headObjectId = evaluateOnCommitResolvedObjectId; + evalCommit = getCommitFromRef(); } - - if (headObjectId == null) { - throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have some commits in the dotGitDirectory?"); - } - evalCommit = revWalk.parseCommit(headObjectId); revWalk.markStart(evalCommit); } catch (Exception e) { throw new GitCommitIdExecutionException("Error", e); } } + private RevCommit getCommitFromProjectDirectory(String projectDirectory) throws GitAPIException, GitCommitIdExecutionException { + //retrieve last commit in folder projectDirectory + Iterator iterator = new Git(git).log().addPath(projectDirectory).call().iterator(); + if (!iterator.hasNext()) { + throw new GitCommitIdExecutionException("Could not get commit from folder " + projectDirectory + " , are you sure you have some commits in the folder " + projectDirectory + "?"); + } + + RevCommit revCommit = iterator.next(); + if (revCommit == null) { + throw new GitCommitIdExecutionException("Could not get commit from folder " + projectDirectory + " , are you sure you have some commits in the folder " + projectDirectory + "?"); + } + + return revCommit; + } + + private RevCommit getCommitFromRef() throws IOException, GitCommitIdExecutionException { + // more details parsed out bellow + Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit); + ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit); + + if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) { + throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory property of this plugin to a valid path?"); + } + ObjectId headObjectId; + if (evaluateOnCommitReference != null) { + headObjectId = evaluateOnCommitReference.getObjectId(); + } else { + headObjectId = evaluateOnCommitResolvedObjectId; + } + + if (headObjectId == null) { + throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have some commits in the dotGitDirectory?"); + } + return revWalk.parseCommit(headObjectId); + } + @Override public String getBranchName() throws GitCommitIdExecutionException { try { diff --git a/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index c39b740b..2fb2e8aa 100644 --- a/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -134,6 +134,14 @@ public class GitCommitIdMojo extends AbstractMojo { @Parameter(defaultValue = "${project.basedir}/.git") File dotGitDirectory; + /** + * Get version number / build number based of the last git commit in the module itself, + * and not based on the last git commit of the whole git project. + * Works only in JGit mode. + */ + @Parameter(defaultValue = "false") + boolean enablePerModuleVersions; + /** * Configuration for the {@code 'git-describe'} command. * You can modify the dirty marker, abbrev length and other options here. @@ -695,6 +703,11 @@ private void loadGitDataWithNativeGit(@Nonnull Properties properties) throws Git } private void loadGitDataWithJGit(@Nonnull Properties properties) throws GitCommitIdExecutionException { + String projectDirectory = null; + if (enablePerModuleVersions) { + String projectRootDir = dotGitDirectory.getAbsolutePath().substring(0, dotGitDirectory.getAbsolutePath().indexOf(".git")); + projectDirectory = project.getBasedir().getAbsolutePath().substring(projectRootDir.length()).replace("\\", "/"); + } GitDataProvider jGitProvider = JGitProvider .on(dotGitDirectory, log) .setPrefixDot(prefixDot) @@ -706,7 +719,8 @@ private void loadGitDataWithJGit(@Nonnull Properties properties) throws GitCommi .setUseBranchNameFromBuildEnvironment(useBranchNameFromBuildEnvironment) .setExcludeProperties(excludeProperties) .setIncludeOnlyProperties(includeOnlyProperties) - .setOffline(offline || settings.isOffline()); + .setOffline(offline || settings.isOffline()) + .setProjectDirectory(projectDirectory); jGitProvider.loadGitData(evaluateOnCommit, properties); }