xref: /openbmc/openbmc/poky/meta/lib/oe/buildcfg.py (revision 9936f86d)
1
2import subprocess
3import bb.process
4
5def detect_revision(d):
6    path = get_scmbasepath(d)
7    return get_metadata_git_revision(path, d)
8
9def detect_branch(d):
10    path = get_scmbasepath(d)
11    return get_metadata_git_branch(path, d)
12
13def get_scmbasepath(d):
14    return os.path.join(d.getVar('COREBASE'), 'meta')
15
16def get_metadata_git_branch(path, d):
17    try:
18        rev, _ = bb.process.run('git rev-parse --abbrev-ref HEAD', cwd=path)
19    except bb.process.ExecutionError:
20        rev = '<unknown>'
21    return rev.strip()
22
23def get_metadata_git_revision(path, d):
24    try:
25        rev, _ = bb.process.run('git rev-parse HEAD', cwd=path)
26    except bb.process.ExecutionError:
27        rev = '<unknown>'
28    return rev.strip()
29
30def is_layer_modified(path):
31    try:
32        subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e;
33                                git diff --quiet --no-ext-diff
34                                git diff --quiet --no-ext-diff --cached""" % path,
35                                shell=True,
36                                stderr=subprocess.STDOUT)
37        return ""
38    except subprocess.CalledProcessError as ex:
39        # Silently treat errors as "modified", without checking for the
40        # (expected) return code 1 in a modified git repo. For example, we get
41        # output and a 129 return code when a layer isn't a git repo at all.
42        return " -- modified"
43
44def get_layer_revisions(d):
45    layers = (d.getVar("BBLAYERS") or "").split()
46    revisions = []
47    for i in layers:
48        revisions.append((i, os.path.basename(i), get_metadata_git_branch(i, None).strip(), get_metadata_git_revision(i, None), is_layer_modified(i)))
49    return revisions
50