Skip to content

Commit 99825c7

Browse files
committed
[GR-19220] Add dirty repo status to RUBY_DESCRIPTION, and add jt build --new-hash (#2684)
PullRequest: truffleruby/3427
2 parents 5049e9e + 4fa1e56 commit 99825c7

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

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

Lines changed: 35 additions & 16 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;
@@ -46,8 +47,9 @@ public class BuildInformationProcessor extends TruffleRubyProcessor {
4647
private String buildName;
4748
private String shortRevision;
4849
private String fullRevision;
50+
private boolean isDirty;
4951
private String compileDate;
50-
private String copyrightYear;
52+
private int copyrightYear;
5153
private String kernelMajorVersion;
5254

5355
@Override
@@ -56,10 +58,13 @@ public synchronized void init(ProcessingEnvironment env) {
5658
try {
5759
trufflerubyHome = findHome();
5860
buildName = System.getenv("TRUFFLERUBY_BUILD_NAME");
59-
fullRevision = runCommand("git rev-parse HEAD");
61+
fullRevision = runCommand("git rev-parse HEAD")
62+
.orElseThrow(() -> new Error("git rev-parse command failed"));
6063
shortRevision = fullRevision.substring(0, 8);
61-
compileDate = runCommand("git log -1 --date=short --pretty=format:%cd");
62-
copyrightYear = compileDate.split("\\-")[0];
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"));
67+
copyrightYear = Integer.parseInt(compileDate.split("\\-")[0]);
6368
kernelMajorVersion = findKernelMajorVersion();
6469
} catch (Throwable e) {
6570
throw new Error(e);
@@ -98,23 +103,32 @@ private File findHome() throws URISyntaxException {
98103
}
99104

100105
private String findKernelMajorVersion() throws IOException, InterruptedException {
101-
final String kernelVersion = runCommand("uname -r");
106+
final String kernelVersion = runCommand("uname -r").orElseThrow(() -> new Error("uname -r command failed"));
102107
return kernelVersion.split(Pattern.quote("."))[0];
103108
}
104109

105-
private String runCommand(String command) throws IOException, InterruptedException {
106-
final Process git = new ProcessBuilder(command.split("\\s+")).directory(trufflerubyHome).start();
107-
final String firstLine;
110+
private Optional<String> runCommand(String command) throws IOException, InterruptedException {
111+
final Process process = new ProcessBuilder(command.split("\\s+")).directory(trufflerubyHome).start();
112+
113+
String firstLine;
108114
try (BufferedReader reader = new BufferedReader(
109-
new InputStreamReader(git.getInputStream(), StandardCharsets.UTF_8))) {
115+
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
110116
firstLine = reader.readLine();
117+
118+
while (process.isAlive()) {
119+
reader.readLine();
120+
}
121+
}
122+
123+
if (process.waitFor() != 0) {
124+
return Optional.empty();
111125
}
112126

113-
final int exitCode = git.waitFor();
114-
if (exitCode != 0) {
115-
throw new Error("Command " + command + " failed with exit code " + exitCode);
127+
if (firstLine == null) {
128+
firstLine = "";
116129
}
117-
return firstLine;
130+
131+
return Optional.of(firstLine);
118132
}
119133

120134
@Override
@@ -174,7 +188,7 @@ private void processBuildInformation(TypeElement element) throws Exception {
174188
if (e instanceof ExecutableElement) {
175189
final String name = e.getSimpleName().toString();
176190

177-
final String value;
191+
final Object value;
178192
switch (name) {
179193
case "getBuildName":
180194
value = buildName;
@@ -185,6 +199,9 @@ private void processBuildInformation(TypeElement element) throws Exception {
185199
case "getFullRevision":
186200
value = fullRevision;
187201
break;
202+
case "isDirty":
203+
value = isDirty;
204+
break;
188205
case "getCopyrightYear":
189206
value = copyrightYear;
190207
break;
@@ -199,11 +216,13 @@ private void processBuildInformation(TypeElement element) throws Exception {
199216
}
200217

201218
stream.println(" @Override");
202-
stream.println(" public String " + name + "() {");
219+
stream.println(" public " + ((ExecutableElement) e).getReturnType() + " " + name + "() {");
203220
if (value == null) {
204221
stream.println(" return null;");
205-
} else {
222+
} else if (value instanceof String) {
206223
stream.println(" return \"" + value + "\";");
224+
} else {
225+
stream.println(" return " + value + ";");
207226
}
208227
stream.println(" }");
209228
stream.println();

src/shared/java/org/truffleruby/shared/BuildInformation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public interface BuildInformation {
2020

2121
String getFullRevision();
2222

23-
String getCopyrightYear();
23+
boolean isDirty();
24+
25+
int getCopyrightYear();
2426

2527
String getCompileDate();
2628

src/shared/java/org/truffleruby/shared/TruffleRuby.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public static String getVersionString(String implementationName) {
3434
}
3535

3636
return String.format(
37-
"%s%s %s, like ruby %s, %s %s [%s-%s]",
37+
"%s%s %s%s, like ruby %s, %s %s [%s-%s]",
3838
ENGINE_ID,
3939
nameExtra,
4040
getEngineVersion(),
41+
BuildInformationImpl.INSTANCE.isDirty() ? "*" : "",
4142
LANGUAGE_VERSION,
4243
implementationName,
4344
ImageInfo.inImageCode() ? "Native" : "JVM",
@@ -49,14 +50,16 @@ public static String getEngineVersion() {
4950
// The property cannot be read in a static initializer, it's set later
5051
final String systemVersion = System.getProperty("org.graalvm.version");
5152

53+
final String revisionString = BuildInformationImpl.INSTANCE.getShortRevision();
54+
5255
// No version information, or just "dev" - use 0.0-commit
5356
if (systemVersion == null || systemVersion.equals("dev")) {
54-
return "0.0-" + BuildInformationImpl.INSTANCE.getShortRevision();
57+
return "0.0-" + revisionString;
5558
}
5659

5760
// A "-dev" version number - append the commit as well
5861
if (systemVersion.endsWith("-dev")) {
59-
return systemVersion + "-" + BuildInformationImpl.INSTANCE.getShortRevision();
62+
return systemVersion + "-" + revisionString;
6063
}
6164

6265
return systemVersion;

tool/jt.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ def help
766766
it is also linked in your ruby manager (if found) under the same name,
767767
by default it is the name of the mx env file,
768768
the named build stays until it is rebuilt or deleted manually
769+
--new-hash update the git commit hash in RUBY_DESCRIPTION
769770
mx options: options passed directly to mx
770771
-d start the Java debugger and enables assertions when running truffleruby to configure C extensions
771772
mx build options options passed to the 'build' command of mx
@@ -2375,6 +2376,12 @@ def bootstrap_toolchain
23752376
remove_shared_compile_artifacts
23762377
end
23772378

2379+
if options.delete('--new-hash')
2380+
build_information_path = "#{TRUFFLERUBY_DIR}/src/shared/java/org/truffleruby/shared/BuildInformation.java"
2381+
raise unless File.exist?(build_information_path) # in case the file moves in the future
2382+
FileUtils.touch(build_information_path)
2383+
end
2384+
23782385
env = if (i = options.index('--env') || options.index('-e'))
23792386
options.delete_at i
23802387
options.delete_at i

0 commit comments

Comments
 (0)