blob: b3fe510309c21fa3096d15793a3e171bd2b035a3 [file] [log] [blame]
Andrew Geissler78b72792022-06-14 06:47:25 -05001
Patrick Williamsac13d5f2023-11-24 18:59:46 -06002import os
Andrew Geissler78b72792022-06-14 06:47:25 -05003import subprocess
4import bb.process
5
6def detect_revision(d):
7 path = get_scmbasepath(d)
Patrick Williamsac13d5f2023-11-24 18:59:46 -06008 return get_metadata_git_revision(path)
Andrew Geissler78b72792022-06-14 06:47:25 -05009
10def detect_branch(d):
11 path = get_scmbasepath(d)
Patrick Williamsac13d5f2023-11-24 18:59:46 -060012 return get_metadata_git_branch(path)
Andrew Geissler78b72792022-06-14 06:47:25 -050013
14def get_scmbasepath(d):
15 return os.path.join(d.getVar('COREBASE'), 'meta')
16
Patrick Williamsac13d5f2023-11-24 18:59:46 -060017def get_metadata_git_branch(path):
Andrew Geissler78b72792022-06-14 06:47:25 -050018 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
Patrick Williamsac13d5f2023-11-24 18:59:46 -060024def get_metadata_git_revision(path):
Andrew Geissler78b72792022-06-14 06:47:25 -050025 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:
Patrick Williamsac13d5f2023-11-24 18:59:46 -060049 revisions.append((i, os.path.basename(i), get_metadata_git_branch(i).strip(), get_metadata_git_revision(i), is_layer_modified(i)))
Andrew Geissler78b72792022-06-14 06:47:25 -050050 return revisions