File tree Expand file tree Collapse file tree 4 files changed +56
-8
lines changed
src/main/java/choonster/testmod3/gradle Expand file tree Collapse file tree 4 files changed +56
-8
lines changed Original file line number Diff line number Diff line change 1313! .gitattributes
1414! ASSETS_LICENSE.txt
1515
16+ ! buildSrc
17+ buildSrc /.gradle
18+ buildSrc /build
19+
1620src /generated /resources /\. cache /
Original file line number Diff line number Diff line change 1+ import choonster.testmod3.gradle.GitVersion ;
2+
13plugins {
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
2015mod_version = " ${ minecraft_version} -${ mod_version} -${ commitId} "
2116
Original file line number Diff line number Diff line change 1+ repositories {
2+ mavenCentral();
3+ }
4+
5+ dependencies {
6+ implementation " org.eclipse.jgit:org.eclipse.jgit:7.4.0.202509020913-r"
7+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments