2021-06-23 19:19:44 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Professor Bloodstone <git@bloodstone.dev>
Date: Sun, 20 Jun 2021 01:48:31 +0200
Subject: [PATCH] Add Git information to version command/on startup
diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java
new file mode 100644
2021-11-27 09:58:03 +01:00
index 0000000000000000000000000000000000000000..909617079db61b675cc7b60b44ef96b306076343
2021-06-23 19:19:44 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/JarManifests.java
2021-11-27 09:58:03 +01:00
@@ -0,0 +1,37 @@
2021-06-23 19:19:44 +02:00
+package io.papermc.paper.util;
+
+import java.io.IOException;
+import java.io.InputStream;
2021-11-27 02:03:47 +01:00
+import java.net.URL;
2021-11-25 00:45:51 +01:00
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
2021-06-23 19:19:44 +02:00
+import java.util.jar.Manifest;
2021-11-25 00:45:51 +01:00
+import org.jetbrains.annotations.ApiStatus;
2021-11-25 10:19:05 +01:00
+import org.jetbrains.annotations.NotNull;
2021-11-27 09:58:03 +01:00
+import org.jetbrains.annotations.Nullable;
2021-06-23 19:19:44 +02:00
+
2021-11-25 00:45:51 +01:00
+@ApiStatus.Internal
2021-06-23 19:19:44 +02:00
+public final class JarManifests {
2021-11-25 00:45:51 +01:00
+ private JarManifests() {
2021-06-23 19:19:44 +02:00
+ }
+
2021-11-25 00:45:51 +01:00
+ private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>());
2021-06-23 19:19:44 +02:00
+
2021-11-27 09:58:03 +01:00
+ public static @Nullable Manifest manifest(final @NotNull Class<?> clazz) {
2021-11-27 02:03:47 +01:00
+ return MANIFESTS.computeIfAbsent(clazz.getClassLoader(), classLoader -> {
+ final String classLocation = "/" + clazz.getName().replace(".", "/") + ".class";
+ final URL resource = clazz.getResource(classLocation);
+ if (resource == null) {
2021-11-27 09:58:03 +01:00
+ return null;
2021-06-23 19:19:44 +02:00
+ }
2021-11-27 02:03:47 +01:00
+ final String classFilePath = resource.toString().replace("\\", "/");
+ final String archivePath = classFilePath.substring(0, classFilePath.length() - classLocation.length());
+ try (final InputStream stream = new URL(archivePath + "/META-INF/MANIFEST.MF").openStream()) {
2021-11-25 00:45:51 +01:00
+ return new Manifest(stream);
+ } catch (final IOException ex) {
2021-11-27 09:58:03 +01:00
+ return null;
2021-11-25 00:45:51 +01:00
+ }
+ });
2021-06-23 19:19:44 +02:00
+ }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
2023-12-05 18:20:55 +01:00
index 9f52c092d906622e2eade3bc9d8baac6816f9f3a..869d57b429fbe2694d1444db56c56619abfae920 100644
2021-06-23 19:19:44 +02:00
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
2023-11-11 21:25:45 +01:00
@@ -57,6 +57,7 @@ import org.bukkit.util.CachedServerIcon;
2021-06-23 19:19:44 +02:00
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import io.papermc.paper.util.JarManifests; // Paper
/**
* Represents the Bukkit core, for version and Server singleton handling
2023-11-11 21:25:45 +01:00
@@ -106,7 +107,25 @@ public final class Bukkit {
2021-06-23 19:19:44 +02:00
}
Bukkit.server = server;
- server.getLogger().info("This server is running " + getName() + " version " + getVersion() + " (Implementing API version " + getBukkitVersion() + ")");
+ // Paper start - add git information
+ server.getLogger().info(getVersionMessage());
+ }
+ /**
+ * Gets message describing the version server is running.
+ *
+ * @return message describing the version server is running
+ */
+ @NotNull
+ public static String getVersionMessage() {
2021-11-27 09:58:03 +01:00
+ final var manifest = JarManifests.manifest(Bukkit.getServer().getClass());
+ final String gitBranch = manifest == null ? null : manifest.getMainAttributes().getValue("Git-Branch");
+ final String gitCommit = manifest == null ? null : manifest.getMainAttributes().getValue("Git-Commit");
2021-11-25 00:45:51 +01:00
+ String branchMsg = " on " + gitBranch;
2021-06-23 19:19:44 +02:00
+ if ("master".equals(gitBranch) || "main".equals(gitBranch)) {
+ branchMsg = ""; // Don't show branch on main/master
+ }
+ return "This server is running " + getName() + " version " + getVersion() + " (Implementing API version " + getBukkitVersion() + ") (Git: " + gitCommit + branchMsg + ")";
+ // Paper end
}
/**
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
2023-08-06 03:26:13 +02:00
index b437cf212a63aa96a9492db8d01d5d37061aee23..fd5d9881abfd930bb883120f018f76dc78b62b14 100644
2021-06-23 19:19:44 +02:00
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
2023-08-06 03:26:13 +02:00
@@ -256,7 +256,7 @@ public class VersionCommand extends BukkitCommand {
private void setVersionMessage(final @NotNull Component msg) {
2021-06-23 19:19:44 +02:00
lastCheck = System.currentTimeMillis();
2023-08-06 03:26:13 +02:00
final Component message = Component.textOfChildren(
- Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", NamedTextColor.WHITE),
+ Component.text(Bukkit.getVersionMessage(), NamedTextColor.WHITE),
Component.newline(),
2021-06-23 19:19:44 +02:00
msg
);