Skip to content

Commit f770aae

Browse files
authored
remove jgit (#685)
1 parent 99ffcac commit f770aae

File tree

13 files changed

+337
-567
lines changed

13 files changed

+337
-567
lines changed

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ sourceSets {
4141

4242
dependencies {
4343
implementation gradleApi()
44-
implementation 'org.eclipse.jgit:org.eclipse.jgit'
4544
implementation 'com.google.guava:guava'
4645

4746
groovyImplementation localGroovy()

changelog/@unreleased/pr-685.v2.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type: break
2+
break:
3+
description: |-
4+
The PR removes all usage of jgit and use native git. The native git binary is now required for this plugin to work.
5+
There are two major benefits: 1. Only calls git describe once now so the gitVersion() task is much faster. 2. The plugin current supports git worktree.
6+
links:
7+
- https://github.com/palantir/gradle-git-version/pull/685

src/main/java/com/palantir/gradle/gitversion/NativeGitDescribe.java renamed to src/main/java/com/palantir/gradle/gitversion/Git.java

+91-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.gradle.gitversion;
1818

19+
import com.google.common.annotations.VisibleForTesting;
1920
import com.google.common.base.Preconditions;
2021
import com.google.common.base.Splitter;
2122
import java.io.BufferedReader;
@@ -25,33 +26,52 @@
2526
import java.nio.charset.StandardCharsets;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
29+
import java.util.HashMap;
2830
import java.util.HashSet;
2931
import java.util.List;
32+
import java.util.Map;
3033
import java.util.Set;
3134
import org.slf4j.Logger;
3235
import org.slf4j.LoggerFactory;
3336

3437
/**
3538
* Mimics git describe by using rev-list to support versions of git < 1.8.4.
3639
*/
37-
class NativeGitDescribe implements GitDescribe {
38-
private static final Logger log = LoggerFactory.getLogger(NativeGitDescribe.class);
40+
class Git {
41+
private static final Logger log = LoggerFactory.getLogger(Git.class);
3942

4043
private static final Splitter LINE_SPLITTER =
4144
Splitter.on(System.getProperty("line.separator")).omitEmptyStrings();
4245
private static final Splitter WORD_SPLITTER = Splitter.on(" ").omitEmptyStrings();
4346

4447
private final File directory;
4548

46-
NativeGitDescribe(File directory) {
49+
Git(File directory) {
50+
this(directory, false);
51+
}
52+
53+
@VisibleForTesting
54+
Git(File directory, boolean testing) {
55+
if (!gitCommandExists()) {
56+
throw new RuntimeException("Git not found in project");
57+
}
4758
this.directory = directory;
59+
if (testing && !checkIfUserIsSet()) {
60+
setGitUser();
61+
}
4862
}
4963

5064
private String runGitCmd(String... commands) throws IOException, InterruptedException {
65+
return runGitCmd(new HashMap<>(), commands);
66+
}
67+
68+
private String runGitCmd(Map<String, String> envvars, String... commands) throws IOException, InterruptedException {
5169
List<String> cmdInput = new ArrayList<>();
5270
cmdInput.add("git");
5371
cmdInput.addAll(Arrays.asList(commands));
5472
ProcessBuilder pb = new ProcessBuilder(cmdInput);
73+
Map<String, String> environment = pb.environment();
74+
environment.putAll(envvars);
5575
pb.directory(directory);
5676
pb.redirectErrorStream(true);
5777

@@ -74,12 +94,77 @@ private String runGitCmd(String... commands) throws IOException, InterruptedExce
7494
return builder.toString().trim();
7595
}
7696

77-
@Override
78-
public String describe(String prefix) {
79-
if (!gitCommandExists()) {
97+
public String runGitCommand(Map<String, String> envvar, String... command) {
98+
try {
99+
return runGitCmd(envvar, command);
100+
} catch (IOException | InterruptedException | RuntimeException e) {
101+
log.debug("Native git command {} failed.\n", command, e);
102+
return null;
103+
}
104+
}
105+
106+
public String runGitCommand(String... command) {
107+
return runGitCommand(new HashMap<>(), command);
108+
}
109+
110+
private boolean checkIfUserIsSet() {
111+
try {
112+
String userEmail = runGitCmd("config", "user.email");
113+
if (userEmail.isEmpty()) {
114+
return false;
115+
}
116+
return true;
117+
} catch (IOException | InterruptedException | RuntimeException e) {
118+
log.debug("Native git config user.email failed", e);
119+
return false;
120+
}
121+
}
122+
123+
private void setGitUser() {
124+
try {
125+
runGitCommand("config", "--global", "user.email", "[email protected]");
126+
runGitCommand("config", "--global", "user.name", "name");
127+
} catch (RuntimeException e) {
128+
log.debug("Native git set user failed", e);
129+
}
130+
}
131+
132+
public String getCurrentBranch() {
133+
try {
134+
String branch = runGitCmd("branch", "--show-current");
135+
if (branch.isEmpty()) {
136+
return null;
137+
}
138+
return branch;
139+
} catch (IOException | InterruptedException | RuntimeException e) {
140+
log.debug("Native git branch --show-current failed", e);
141+
return null;
142+
}
143+
}
144+
145+
public String getCurrentHeadFullHash() {
146+
try {
147+
return runGitCmd("rev-parse", "HEAD");
148+
} catch (IOException | InterruptedException | RuntimeException e) {
149+
log.debug("Native git rev-parse HEAD failed", e);
150+
return null;
151+
}
152+
}
153+
154+
public Boolean isClean() {
155+
try {
156+
String result = runGitCmd("status", "--porcelain");
157+
if (result.isEmpty()) {
158+
return true;
159+
}
160+
return false;
161+
} catch (IOException | InterruptedException | RuntimeException e) {
162+
log.debug("Native git status --porcelain failed", e);
80163
return null;
81164
}
165+
}
82166

167+
public String describe(String prefix) {
83168
try {
84169
// Get SHAs of all tags, we only need to search for these later on
85170
Set<String> tagRefs = new HashSet<>();

src/main/java/com/palantir/gradle/gitversion/GitDescribe.java

-26
This file was deleted.

src/main/java/com/palantir/gradle/gitversion/GitVersionCacheService.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.palantir.gradle.gitversion;
1717

1818
import java.io.File;
19-
import java.io.IOException;
2019
import java.util.concurrent.ConcurrentHashMap;
2120
import java.util.concurrent.ConcurrentMap;
2221
import org.gradle.api.Project;
@@ -53,12 +52,7 @@ public final VersionDetails getVersionDetails(File project, Object args) {
5352
}
5453

5554
private VersionDetails createVersionDetails(File gitDir, GitVersionArgs args) {
56-
try {
57-
return TimingVersionDetails.wrap(timer, new VersionDetailsImpl(gitDir, args));
58-
} catch (IOException e) {
59-
log.debug("Cannot compute version details", e);
60-
return null;
61-
}
55+
return TimingVersionDetails.wrap(timer, new VersionDetailsImpl(gitDir, args));
6256
}
6357

6458
public final Timer timer() {

src/main/java/com/palantir/gradle/gitversion/JGitDescribe.java

-138
This file was deleted.

src/main/java/com/palantir/gradle/gitversion/RefWithTagName.java

-41
This file was deleted.

0 commit comments

Comments
 (0)