Stamp final artifacts

Merge Implementation-Version into the final artifacts. This has two
advantages: for one, it can be later tracked down, from what release
this artifact was consumed; for another, in case a release was produced
without touching any source files but only bumping some dependencies,
that are not shaded in the final artifacts and thus do not contribute
to the SHA1 update of the final artifacts, the SHA1 would still change,
because of the changed content of the META-INF/MANIFEST.MF file. For
example building from this commit will produce the following manifest
content:

  $ bazel build java/com/google/gitiles/blame/cache:cache-stamped
  bazel-genfiles/java/com/google/gitiles/blame/cache/cache-stamped.jar

Unzipping the content of the cache-stamped.jar produces the following
implementation version:

  $ cat META-INF/MANIFEST.MF  | grep -i Impl
  Implementation-Version: v0.2-3-7-g9af1a70

Change-Id: Idb6e7c635ae254188e1ee6592e0e0502c7c338a6
diff --git a/java/com/google/gitiles/BUILD b/java/com/google/gitiles/BUILD
index 67e6151..a4361ed 100644
--- a/java/com/google/gitiles/BUILD
+++ b/java/com/google/gitiles/BUILD
@@ -51,3 +51,10 @@
     title = "Gitiles API Documentation",
     visibility = ["//visibility:public"],
 )
+
+load("//tools:stamper.bzl", "stamp")
+
+stamp(
+    name = "servlet",
+    workspace = "gitiles",
+)
diff --git a/java/com/google/gitiles/blame/cache/BUILD b/java/com/google/gitiles/blame/cache/BUILD
index 3f3e39b..33a4797 100644
--- a/java/com/google/gitiles/blame/cache/BUILD
+++ b/java/com/google/gitiles/blame/cache/BUILD
@@ -21,3 +21,10 @@
     title = "Blame Cache API Documentation",
     visibility = ["//visibility:public"],
 )
+
+load("//tools:stamper.bzl", "stamp")
+
+stamp(
+    name = "cache",
+    workspace = "gitiles",
+)
diff --git a/tools/bazel.rc b/tools/bazel.rc
new file mode 100644
index 0000000..2210511
--- /dev/null
+++ b/tools/bazel.rc
@@ -0,0 +1 @@
+build --workspace_status_command=./tools/workspace-status.sh
diff --git a/tools/maven/BUILD b/tools/maven/BUILD
index e6612cb..50a343d 100644
--- a/tools/maven/BUILD
+++ b/tools/maven/BUILD
@@ -12,8 +12,8 @@
     },
     group = "com.google.gitiles",
     jar = {
-        "blame-cache": "//java/com/google/gitiles/blame/cache",
-        "gitiles-servlet": "//java/com/google/gitiles:servlet",
+        "blame-cache": "//java/com/google/gitiles/blame/cache:cache-stamped",
+        "gitiles-servlet": "//java/com/google/gitiles:servlet-stamped",
     },
     repository = "gerrit-maven-repository",
     url = "gs://gerrit-maven",
diff --git a/tools/stamper.bzl b/tools/stamper.bzl
new file mode 100644
index 0000000..ecbf75d
--- /dev/null
+++ b/tools/stamper.bzl
@@ -0,0 +1,20 @@
+# TODO(davido): Consider to move this general bazlets to Bazlets repository
+load("@com_googlesource_gerrit_bazlets//tools:genrule2.bzl", "genrule2")
+
+def stamp(workspace, name):
+  # TODO(davido): Remove manual merge of manifest file when this feature
+  # request is implemented: https://github.com/bazelbuild/bazel/issues/2009
+  genrule2(
+    name = "%s-stamped" % name,
+    stamp = 1,
+    srcs = [":%s" % name],
+    cmd = " && ".join([
+      "GEN_VERSION=$$(cat bazel-out/stable-status.txt | grep -w STABLE_BUILD_%s_LABEL | cut -d ' ' -f 2)" % workspace.upper(),
+      "cd $$TMP",
+      "unzip -q $$ROOT/$<",
+      "echo \"Implementation-Version: $$GEN_VERSION\n$$(cat META-INF/MANIFEST.MF)\" > META-INF/MANIFEST.MF",
+      "zip -qr $$ROOT/$@ ."]),
+    outs = ["%s-stamped.jar" % name],
+    visibility = ["//visibility:public"],
+  )
+
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
new file mode 100755
index 0000000..9cc40e9
--- /dev/null
+++ b/tools/workspace-status.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# 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.
+
+function rev() {
+  cd $1; git describe --always --match "v[0-9].*" --dirty
+}
+
+echo STABLE_BUILD_GITILES_LABEL $(rev .)