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 get_metadata_git_toplevel(path): 32 try: 33 toplevel, _ = bb.process.run('git rev-parse --show-toplevel', cwd=path) 34 except bb.process.ExecutionError: 35 return "" 36 return toplevel.strip() 37 38def get_metadata_git_remotes(path): 39 try: 40 remotes_list, _ = bb.process.run('git remote', cwd=path) 41 remotes = remotes_list.split() 42 except bb.process.ExecutionError: 43 remotes = [] 44 return remotes 45 46def get_metadata_git_remote_url(path, remote): 47 try: 48 uri, _ = bb.process.run('git remote get-url {remote}'.format(remote=remote), cwd=path) 49 except bb.process.ExecutionError: 50 return "" 51 return uri.strip() 52 53def get_metadata_git_describe(path): 54 try: 55 describe, _ = bb.process.run('git describe --tags --dirty', cwd=path) 56 except bb.process.ExecutionError: 57 return "" 58 return describe.strip() 59 60def is_layer_modified(path): 61 try: 62 subprocess.check_output("""cd %s; export PSEUDO_UNLOAD=1; set -e; 63 git diff --quiet --no-ext-diff 64 git diff --quiet --no-ext-diff --cached""" % path, 65 shell=True, 66 stderr=subprocess.STDOUT) 67 return "" 68 except subprocess.CalledProcessError as ex: 69 # Silently treat errors as "modified", without checking for the 70 # (expected) return code 1 in a modified git repo. For example, we get 71 # output and a 129 return code when a layer isn't a git repo at all. 72 return " -- modified" 73 74def get_layer_revisions(d): 75 layers = (d.getVar("BBLAYERS") or "").split() 76 revisions = [] 77 for i in layers: 78 revisions.append((i, os.path.basename(i), get_metadata_git_branch(i).strip(), get_metadata_git_revision(i), is_layer_modified(i))) 79 return revisions 80