@@ -127,7 +127,7 @@ public GitVersion build() {
127
127
throw new IllegalArgumentException ("Either the root or project directory must be set" );
128
128
129
129
if (this .root == null )
130
- this .root = GitUtils . findGitRoot (this .project );
130
+ this .root = findGitRoot (this .project );
131
131
132
132
if (this .project == null )
133
133
this .project = this .root ;
@@ -377,6 +377,30 @@ default String getRelativePath(boolean fromRoot, File file) {
377
377
return GitUtils .getRelativePath (fromRoot ? this .getRoot () : this .getProject (), file );
378
378
}
379
379
380
+ /**
381
+ * Attempts to find the git root from the given directory, using {@linkplain File#getAbsoluteFile() absolute files}
382
+ * to walk the filesystem.
383
+ *
384
+ * @param from The file to find the Git root from
385
+ * @return The Git root, or the given file if no Git root was found
386
+ */
387
+ static File findGitRoot (File from ) {
388
+ for (var dir = from .getAbsoluteFile (); dir != null ; dir = dir .getParentFile ())
389
+ if (isGitRoot (dir )) return dir ;
390
+
391
+ return from ;
392
+ }
393
+
394
+ /**
395
+ * Checks if a given file is a Git root.
396
+ *
397
+ * @param dir The directory to check
398
+ * @return {@code true} if the directory is a Git root
399
+ */
400
+ static boolean isGitRoot (File dir ) {
401
+ return new File (dir , ".git" ).exists ();
402
+ }
403
+
380
404
/**
381
405
* Attempts to get the relative path of the given file from the root of its Git repository. This is exposed
382
406
* primarily to allow Gradle plugins to get the relative path without needing to declare the path directory
@@ -390,7 +414,7 @@ default String getRelativePath(boolean fromRoot, File file) {
390
414
*/
391
415
@ Deprecated (forRemoval = true )
392
416
static String findRelativePath (File file ) {
393
- return GitUtils .getRelativePath (GitUtils . findGitRoot (file ), file );
417
+ return GitUtils .getRelativePath (findGitRoot (file ), file );
394
418
}
395
419
396
420
0 commit comments