diff options
author | Ben Brown <ben.brown@codethink.co.uk> | 2017-09-07 15:05:56 +0100 |
---|---|---|
committer | Ben Brown <ben.brown@codethink.co.uk> | 2017-09-07 16:26:07 +0100 |
commit | 2863270717272b21a01fdf426ec16c9f354bb01b (patch) | |
tree | ade7221e4e898cff4adf752ab0e3e9d5112bdc4f | |
parent | 0c3d787e171e36a25f13d0ac9b9c22b26b8853a2 (diff) | |
download | ybd-benbrown/warn-sha-not-in-ref.tar.gz |
Exit/warn if a component ref does not contain the shabenbrown/warn-sha-not-in-ref
-rw-r--r-- | ybd/cache.py | 3 | ||||
-rw-r--r-- | ybd/repos.py | 30 |
2 files changed, 32 insertions, 1 deletions
diff --git a/ybd/cache.py b/ybd/cache.py index b12d0f3..b4ff08e 100644 --- a/ybd/cache.py +++ b/ybd/cache.py @@ -23,7 +23,7 @@ import shutil from subprocess import call import app -from repos import get_repo_url, get_tree +from repos import ensure_ref_contains_sha, get_repo_url, get_tree import utils import tempfile import yaml @@ -57,6 +57,7 @@ def cache_key(dn): if dn.get('repo'): if not dn.get('tree'): dn['tree'], dn['sha'] = get_tree(dn) + ensure_ref_contains_sha(dn, dn['ref'], dn['sha']) if not dn.get('repourl'): dn['repourl'] = get_repo_url(dn.get('repo')) factors = hash_factors(dn) diff --git a/ybd/repos.py b/ybd/repos.py index 65cdd12..cc9d22e 100644 --- a/ybd/repos.py +++ b/ybd/repos.py @@ -103,6 +103,36 @@ def tracking_branch(dn): return (isinstance(track, list) and dn['path'] in track) or track +def ensure_ref_contains_sha(dn, ref, sha): + check = app.config.get('check-definitions') + if check == 'ignore' or tracking_branch(dn): + return + + gitdir = get_transport_info(dn['repo'])['dir'] + with app.chdir(gitdir), open(os.devnull, "w") as fnull: + ref = dn['ref'] + if ref == sha: + return + + def sha_in_ref(): + return check_output(['git', 'for-each-ref', '--contains', + sha, 'refs/*/' + ref], stderr=fnull, + universal_newlines=True).strip() + + if not sha_in_ref(): + # The sha may currently exist in another branch, + # force an update of the provided ref. + repo_url = get_repo_url(dn['repo']) + call(['git', 'fetch', repo_url, + '+refs/*/{0}:refs/*/{0}'.format(ref)], + stdout=fnull, stderr=fnull) + + if not sha_in_ref(): + exit = check == 'exit' + app.log(dn, 'WARNING: unable to find %s within' % + sha, ref, exit=exit) + + def get_tree(dn): info = get_transport_info(dn['repo']) track = tracking_branch(dn) |