Skip to content

Commit 7e47698

Browse files
committed
Move some important utilities to GitVersion
The same will probably be done with GitUtils#getRelativePath, moving those to file-utils when it is created.
1 parent 96dd709 commit 7e47698

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/main/java/net/minecraftforge/gitver/api/GitVersion.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public GitVersion build() {
127127
throw new IllegalArgumentException("Either the root or project directory must be set");
128128

129129
if (this.root == null)
130-
this.root = GitUtils.findGitRoot(this.project);
130+
this.root = findGitRoot(this.project);
131131

132132
if (this.project == null)
133133
this.project = this.root;
@@ -377,6 +377,30 @@ default String getRelativePath(boolean fromRoot, File file) {
377377
return GitUtils.getRelativePath(fromRoot ? this.getRoot() : this.getProject(), file);
378378
}
379379

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+
380404
/**
381405
* Attempts to get the relative path of the given file from the root of its Git repository. This is exposed
382406
* 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) {
390414
*/
391415
@Deprecated(forRemoval = true)
392416
static String findRelativePath(File file) {
393-
return GitUtils.getRelativePath(GitUtils.findGitRoot(file), file);
417+
return GitUtils.getRelativePath(findGitRoot(file), file);
394418
}
395419

396420

src/main/java/net/minecraftforge/gitver/internal/GitUtils.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,6 @@ static String getRelativePath(File root, File file) {
5858
return getRelativePath(root.toPath(), file.toPath());
5959
}
6060

61-
/**
62-
* Attempts to find the git root from the given directory.
63-
*
64-
* @param from The file to find the Git root from
65-
* @return The Git root, or the given file if no Git root was found
66-
*/
67-
static File findGitRoot(File from) {
68-
for (var dir = from.getAbsoluteFile(); dir != null; dir = dir.getParentFile())
69-
if (isGitRoot(dir)) return dir;
70-
71-
return from;
72-
}
73-
74-
/**
75-
* Checks if a given file is a Git root.
76-
*
77-
* @param dir The directory to check
78-
* @return {@code true} if the directory is a Git root
79-
*/
80-
static boolean isGitRoot(File dir) {
81-
return new File(dir, ".git").exists();
82-
}
83-
8461
/**
8562
* Counts commits, for the given Git repository, from the given tag to {@linkplain Constants#HEAD HEAD}. If the
8663
* given tag cannot be found, this method will return {@code -1}.

0 commit comments

Comments
 (0)