xref: /JGit/tools/workspace_status.py (revision d35f0ffb7c33a11f8192ec64da5a736827ab472d)
1#!/usr/bin/env python
2# Copyright (C) 2020, David Ostrovsky <david@ostrovsky.org> and others
3#
4# This program and the accompanying materials are made available under the
5# terms of the Eclipse Distribution License v. 1.0 which is available at
6# http://www.eclipse.org/org/documents/edl-v10.php.
7#
8# SPDX-License-Identifier: BSD-3-Clause
9
10# This script will be run by bazel when the build process starts to
11# generate key-value information that represents the status of the
12# workspace. The output should be like
13#
14# KEY1 VALUE1
15# KEY2 VALUE2
16#
17# If the script exits with non-zero code, it's considered as a failure
18# and the output will be discarded.
19
20from __future__ import print_function
21import os
22import subprocess
23import sys
24
25ROOT = os.path.abspath(__file__)
26while not os.path.exists(os.path.join(ROOT, 'WORKSPACE')):
27    ROOT = os.path.dirname(ROOT)
28CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
29
30
31def revision(directory, parent):
32    try:
33        os.chdir(directory)
34        return subprocess.check_output(CMD).strip().decode("utf-8")
35    except OSError as err:
36        print('could not invoke git: %s' % err, file=sys.stderr)
37        sys.exit(1)
38    except subprocess.CalledProcessError as err:
39        # ignore "not a git repository error" to report unknown version
40        return None
41    finally:
42        os.chdir(parent)
43
44
45print("STABLE_BUILD_JGIT_LABEL %s" % revision(ROOT, ROOT))
46