Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 1 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 2 | import os |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 3 | import subprocess |
| 4 | import bb.process |
| 5 | |
| 6 | def detect_revision(d): |
| 7 | path = get_scmbasepath(d) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 8 | return get_metadata_git_revision(path) |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 9 | |
| 10 | def detect_branch(d): |
| 11 | path = get_scmbasepath(d) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 12 | return get_metadata_git_branch(path) |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 13 | |
| 14 | def get_scmbasepath(d): |
| 15 | return os.path.join(d.getVar('COREBASE'), 'meta') |
| 16 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 17 | def get_metadata_git_branch(path): |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 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 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 24 | def get_metadata_git_revision(path): |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 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 | |
| 31 | def 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 | |
| 45 | def get_layer_revisions(d): |
| 46 | layers = (d.getVar("BBLAYERS") or "").split() |
| 47 | revisions = [] |
| 48 | for i in layers: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 49 | revisions.append((i, os.path.basename(i), get_metadata_git_branch(i).strip(), get_metadata_git_revision(i), is_layer_modified(i))) |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 50 | return revisions |