2021-06-23 17:19:44 +00: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-25 09:19:05 +00:00
index 0000000000000000000000000000000000000000..2b06b17e20826fb95c9ad58101b4102308ea4b29
2021-06-23 17:19:44 +00:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/JarManifests.java
2021-11-25 09:19:05 +00:00
@@ -0,0 +1,34 @@
2021-06-23 17:19:44 +00:00
+package io.papermc.paper.util;
+
+import java.io.IOException;
+import java.io.InputStream;
2021-11-24 23:45:51 +00:00
+import java.util.Collections;
+import java.util.Map;
+import java.util.WeakHashMap;
2021-06-23 17:19:44 +00:00
+import java.util.jar.Manifest;
2021-11-25 09:19:05 +00:00
+
2021-11-24 23:45:51 +00:00
+import org.jetbrains.annotations.ApiStatus;
2021-11-25 09:19:05 +00:00
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
2021-06-23 17:19:44 +00:00
+
2021-11-24 23:45:51 +00:00
+@ApiStatus.Internal
2021-06-23 17:19:44 +00:00
+public final class JarManifests {
2021-11-24 23:45:51 +00:00
+ private JarManifests() {
2021-06-23 17:19:44 +00:00
+ }
+
2021-11-24 23:45:51 +00:00
+ private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>());
2021-06-23 17:19:44 +00:00
+
2021-11-25 09:19:05 +00:00
+ public static @NotNull Manifest manifest(final @NotNull ClassLoader loader) {
2021-11-24 23:45:51 +00:00
+ return MANIFESTS.computeIfAbsent(loader, classLoader -> {
+ final @Nullable InputStream stream = classLoader.getResourceAsStream("META-INF/MANIFEST.MF");
+ if (stream == null) {
+ throw new IllegalArgumentException("Provided ClassLoader does not have a manifest file in the correct location!");
2021-06-23 17:19:44 +00:00
+ }
2021-11-24 23:45:51 +00:00
+ try (stream) {
+ return new Manifest(stream);
+ } catch (final IOException ex) {
+ throw new RuntimeException("Failed to read manifest.");
+ }
+ });
2021-06-23 17:19:44 +00:00
+ }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
2021-11-24 23:45:51 +00:00
index 0521781a48d326c0a4a01b920188e9ce00b51ef0..7868a54eec71f7b31d7fabf3288eb2dc62649e92 100644
2021-06-23 17:19:44 +00:00
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
2021-10-05 03:14:11 +00:00
@@ -51,6 +51,7 @@ import org.bukkit.util.CachedServerIcon;
2021-06-23 17:19:44 +00: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
2021-11-24 23:26:29 +00:00
@@ -100,7 +101,25 @@ public final class Bukkit {
2021-06-23 17:19:44 +00: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-24 23:45:51 +00:00
+ final var manifest = JarManifests.manifest(Bukkit.getServer().getClass().getClassLoader());
+ final String gitBranch = manifest.getMainAttributes().getValue("Git-Branch");
+ final String gitCommit = manifest.getMainAttributes().getValue("Git-Commit");
+ String branchMsg = " on " + gitBranch;
2021-06-23 17:19:44 +00: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
index 4c2ddc722a9dc4011906ad9530b13fa9be1d3ff9..57a21495843f3a144cd73473cdc8781d6129b7ca 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -241,7 +241,7 @@ public class VersionCommand extends BukkitCommand {
private void setVersionMessage(final @NotNull Component msg) {
lastCheck = System.currentTimeMillis();
final Component message = net.kyori.adventure.text.TextComponent.ofChildren(
- Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", net.kyori.adventure.text.format.NamedTextColor.WHITE),
+ Component.text(Bukkit.getVersionMessage(), net.kyori.adventure.text.format.NamedTextColor.WHITE),
Component.newline(),
msg
);