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