Skip to content

Commit da749f3

Browse files
committed
Use git diff --quiet for detecting dirty repository status
1 parent 2858c0e commit da749f3

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/processor/java/org/truffleruby/processor/BuildInformationProcessor.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.security.CodeSource;
2121
import java.util.Calendar;
2222
import java.util.HashSet;
23+
import java.util.Optional;
2324
import java.util.Set;
2425
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
@@ -57,10 +58,12 @@ public synchronized void init(ProcessingEnvironment env) {
5758
try {
5859
trufflerubyHome = findHome();
5960
buildName = System.getenv("TRUFFLERUBY_BUILD_NAME");
60-
fullRevision = runCommand("git rev-parse HEAD");
61+
fullRevision = runCommand("git rev-parse HEAD")
62+
.orElseThrow(() -> new Error("git rev-parse command failed"));
6163
shortRevision = fullRevision.substring(0, 8);
62-
isDirty = runCommand("git status --porcelain") != null;
63-
compileDate = runCommand("git log -1 --date=short --pretty=format:%cd");
64+
isDirty = runCommand("git diff --quiet").isEmpty();
65+
compileDate = runCommand("git log -1 --date=short --pretty=format:%cd")
66+
.orElseThrow(() -> new Error("git log command failed"));
6467
copyrightYear = Integer.parseInt(compileDate.split("\\-")[0]);
6568
kernelMajorVersion = findKernelMajorVersion();
6669
} catch (Throwable e) {
@@ -100,23 +103,32 @@ private File findHome() throws URISyntaxException {
100103
}
101104

102105
private String findKernelMajorVersion() throws IOException, InterruptedException {
103-
final String kernelVersion = runCommand("uname -r");
106+
final String kernelVersion = runCommand("uname -r").orElseThrow(() -> new Error("uname -r command failed"));
104107
return kernelVersion.split(Pattern.quote("."))[0];
105108
}
106109

107-
private String runCommand(String command) throws IOException, InterruptedException {
110+
private Optional<String> runCommand(String command) throws IOException, InterruptedException {
108111
final Process process = new ProcessBuilder(command.split("\\s+")).directory(trufflerubyHome).start();
109-
final String firstLine;
112+
113+
String firstLine;
110114
try (BufferedReader reader = new BufferedReader(
111115
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
112116
firstLine = reader.readLine();
117+
118+
while (process.isAlive()) {
119+
reader.readLine();
120+
}
113121
}
114122

115-
final int exitCode = process.waitFor();
116-
if (exitCode != 0) {
117-
throw new Error("Command " + command + " failed with exit code " + exitCode);
123+
if (process.waitFor() != 0) {
124+
return Optional.empty();
118125
}
119-
return firstLine;
126+
127+
if (firstLine == null) {
128+
firstLine = "";
129+
}
130+
131+
return Optional.of(firstLine);
120132
}
121133

122134
@Override

0 commit comments

Comments
 (0)