Skip to content

Commit e2c64b5

Browse files
committed
Replace grgit with static method in buildSrc using JGit
1 parent c2908eb commit e2c64b5

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
!.gitattributes
1414
!ASSETS_LICENSE.txt
1515

16+
!buildSrc
17+
buildSrc/.gradle
18+
buildSrc/build
19+
1620
src/generated/resources/\.cache/

build.gradle

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import choonster.testmod3.gradle.GitVersion;
2+
13
plugins {
24
id 'java'
35
id 'idea'
@@ -6,16 +8,9 @@ plugins {
68
id 'net.minecraftforge.accesstransformers' version '5.0.1'
79
id 'net.minecraftforge.gradle' version '7.0.0-beta.51'
810
id 'net.minecraftforge.jarjar' version '0.2.3'
9-
id 'org.ajoberstar.grgit' version '5.2.0'
1011
}
1112

12-
final commitId
13-
14-
if (hasProperty("grgit")) { // If there's a valid Git repository, get the latest commit ID
15-
commitId = "${grgit.head().abbreviatedId}"
16-
} else { // Else fall back to NOGIT
17-
commitId = "NOGIT"
18-
}
13+
final def commitId = GitVersion.getGitCommit(layout.projectDirectory.asFile).orElse("NOGIT");
1914

2015
mod_version = "${minecraft_version}-${mod_version}-${commitId}"
2116

buildSrc/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repositories {
2+
mavenCentral();
3+
}
4+
5+
dependencies {
6+
implementation "org.eclipse.jgit:org.eclipse.jgit:7.4.0.202509020913-r"
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package choonster.testmod3.gradle;
2+
3+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.Optional;
8+
9+
public class GitVersion {
10+
static Optional<String> getGitCommit(final File projectDir) {
11+
final var gitDir = new File(projectDir, ".git");
12+
13+
if (!gitDir.exists()) {
14+
return Optional.empty();
15+
}
16+
17+
final var builder = new FileRepositoryBuilder();
18+
builder.readEnvironment();
19+
20+
builder.findGitDir(projectDir);
21+
22+
if (builder.getGitDir() == null) {
23+
throw new IllegalStateException("No .git directory found!");
24+
}
25+
26+
try (
27+
final var repository = builder.build();
28+
final var reader = repository.newObjectReader()
29+
) {
30+
final var head = repository.resolve("HEAD");
31+
32+
if (head == null) {
33+
return Optional.empty();
34+
}
35+
36+
final var abbreviated = reader.abbreviate(head);
37+
return Optional.of(abbreviated.name());
38+
} catch (final IOException e) {
39+
throw new IllegalStateException("Failed to get commit from Git repository", e);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)