@@ -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
0 commit comments