]> source.dussan.org Git - jgit.git/commitdiff
Bazel: Add workspace status command to stamp final artifact 68/165268/7
authorDavid Ostrovsky <david@ostrovsky.org>
Sun, 21 Jun 2020 14:56:57 +0000 (16:56 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 16 Jul 2020 23:10:15 +0000 (01:10 +0200)
Include implementation version in jgit library. This version is used
by other products that depend on JGit, and built using Bazel and not
consume officially released artifact from Central or Eclipse own Maven
repository.

Most notably, in Gerrit Code Review JGit agent that was previously
reported as "unknown", is now reported as:

  JGit/v5.8.0.202006091008-r-16-g14c43828d

using this change [1].

[1] https://gerrit-review.googlesource.com/c/gerrit/+/272505

Change-Id: Ia50de9ac35b8dbe9e92d8ad7d0d14cd00f057863
Signed-off-by: David Ostrovsky <david@ostrovsky.org>
.bazelrc
org.eclipse.jgit/BUILD
tools/workspace_status.py [new file with mode: 0644]

index 3a61f8cb7adb71913e9b241d26d940d27024423e..e24be88639b36c35511be54605f47121881dc1a2 100644 (file)
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,3 +1,4 @@
+build --workspace_status_command="python ./tools/workspace_status.py"
 build --repository_cache=~/.gerritcodereview/bazel-cache/repository
 build --experimental_strict_action_env
 build --action_env=PATH
index f7970976b0548aa1034c8fa1c4744a8827bb75e0..2083372248f85957f5106771a20c3b0c941b1b57 100644 (file)
@@ -14,7 +14,7 @@ SRCS = glob(
 RESOURCES = glob(["resources/**"])
 
 java_library(
-    name = "jgit",
+    name = "jgit_non_stamped",
     srcs = SRCS,
     resource_strip_prefix = "org.eclipse.jgit/resources",
     resources = RESOURCES,
@@ -25,6 +25,27 @@ java_library(
     ],
 )
 
+genrule(
+    name = "jgit",
+    srcs = [":jgit_non_stamped"],
+    outs = ["jgit.jar"],
+    cmd = " && ".join([
+        "ROOT=$$PWD",
+        "TMP=$$(mktemp -d || mktemp -d -t bazel-tmp)",
+        "TZ=UTC",
+        "export TZ",
+        "GEN_VERSION=$$(cat bazel-out/stable-status.txt | grep -w STABLE_BUILD_JGIT_LABEL | cut -d ' ' -f 2)",
+        "cd $$TMP",
+        "unzip -q $$ROOT/$<",
+        "echo \"Implementation-Version: $$GEN_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF",
+        "find . -exec touch '{}' ';'",
+        "zip -Xqr $$ROOT/$@ .",
+        "rm -rf $$TMP",
+    ]),
+    stamp = 1,
+    visibility = ["//visibility:public"],
+)
+
 java_library(
     name = "insecure_cipher_factory",
     srcs = INSECURE_CIPHER_FACTORY,
diff --git a/tools/workspace_status.py b/tools/workspace_status.py
new file mode 100644 (file)
index 0000000..ca9e0a9
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# Copyright (C) 2020, David Ostrovsky <david@ostrovsky.org> and others
+#
+# This program and the accompanying materials are made available under the
+# terms of the Eclipse Distribution License v. 1.0 which is available at
+# http://www.eclipse.org/org/documents/edl-v10.php.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+# This script will be run by bazel when the build process starts to
+# generate key-value information that represents the status of the
+# workspace. The output should be like
+#
+# KEY1 VALUE1
+# KEY2 VALUE2
+#
+# If the script exits with non-zero code, it's considered as a failure
+# and the output will be discarded.
+
+from __future__ import print_function
+import os
+import subprocess
+import sys
+
+ROOT = os.path.abspath(__file__)
+while not os.path.exists(os.path.join(ROOT, 'WORKSPACE')):
+    ROOT = os.path.dirname(ROOT)
+CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
+
+
+def revision(directory, parent):
+    try:
+        os.chdir(directory)
+        return subprocess.check_output(CMD).strip().decode("utf-8")
+    except OSError as err:
+        print('could not invoke git: %s' % err, file=sys.stderr)
+        sys.exit(1)
+    except subprocess.CalledProcessError as err:
+        # ignore "not a git repository error" to report unknown version
+        return None
+    finally:
+        os.chdir(parent)
+
+
+print("STABLE_BUILD_JGIT_LABEL %s" % revision(ROOT, ROOT))