@@ -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 ;
102- }
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 + ")?" );
92+ evalCommit = getCommitFromRef ();
10893 }
109- evalCommit = revWalk .parseCommit (headObjectId );
11094 revWalk .markStart (evalCommit );
11195 } catch (GitCommitIdExecutionException e ) {
11296 throw e ;
@@ -115,6 +99,61 @@ 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 ().getAbsoluteFile ().toPath ().relativize (moduleBaseDir .getAbsoluteFile ().toPath ()).toString ();
106+ Iterator <RevCommit > iterator ;
107+ if (relativePath .trim ().isEmpty ()) {
108+ // if the relative path is empty, we are in the root of the repository
109+ iterator = gitInstance .log ().call ().iterator ();
110+ } else {
111+ // otherwise, we need to specify the path to get commits for that specific directory
112+ iterator = gitInstance .log ()
113+ .addPath (relativePath ).call ().iterator ();
114+ }
115+ if (!iterator .hasNext ()) {
116+ throw new GitCommitIdExecutionException (
117+ "Could not get commit from folder " + relativePath + " , are you sure you have some " +
118+ "commits in the folder " + moduleBaseDir + "?" );
119+ }
120+
121+ RevCommit revCommit = iterator .next ();
122+ if (revCommit == null ) {
123+ throw new GitCommitIdExecutionException (
124+ "Could not get commit from folder " + relativePath +
125+ " , are you sure you have some commits in the folder " + moduleBaseDir + "?" );
126+ }
127+
128+ return revCommit ;
129+ }
130+ }
131+
132+ private RevCommit getCommitFromRef () throws IOException , GitCommitIdExecutionException {
133+ // more details parsed out below
134+ Ref evaluateOnCommitReference = git .findRef (evaluateOnCommit );
135+ ObjectId evaluateOnCommitResolvedObjectId = git .resolve (evaluateOnCommit );
136+
137+ if ((evaluateOnCommitReference == null ) && (evaluateOnCommitResolvedObjectId == null )) {
138+ throw new GitCommitIdExecutionException (
139+ "Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory " +
140+ "property of this plugin to a valid path (currently set to " + dotGitDirectory + ")?" );
141+ }
142+ ObjectId headObjectId ;
143+ if (evaluateOnCommitReference != null ) {
144+ headObjectId = evaluateOnCommitReference .getObjectId ();
145+ } else {
146+ headObjectId = evaluateOnCommitResolvedObjectId ;
147+ }
148+
149+ if (headObjectId == null ) {
150+ throw new GitCommitIdExecutionException (
151+ "Could not get " + evaluateOnCommit + " Ref, are you sure you have some " +
152+ "commits in the dotGitDirectory (currently set to " + dotGitDirectory + ")?" );
153+ }
154+ return revWalk .parseCommit (headObjectId );
155+ }
156+
118157 @ Override
119158 public String getBranchName () throws GitCommitIdExecutionException {
120159 try {
0 commit comments