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 18:08:44 +0100 |
commit | 6e6b16afefc3cefa891a8f3af2483f07aaedf9af (patch) | |
tree | 9adeaf7c2acdbfc06db81831b3c3d4f5f882d595 | |
parent | 0c3d787e171e36a25f13d0ac9b9c22b26b8853a2 (diff) | |
download | ybd-6e6b16afefc3cefa891a8f3af2483f07aaedf9af.tar.gz |
Exit/warn if a component ref does not contain the sha
-rw-r--r-- | ybd/repos.py | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/ybd/repos.py b/ybd/repos.py index 65cdd12..75685fb 100644 --- a/ybd/repos.py +++ b/ybd/repos.py @@ -103,6 +103,38 @@ def tracking_branch(dn): return (isinstance(track, list) and dn['path'] in track) or track +def ensure_ref_contains_sha(dn, gitdir, ref, sha): + check = app.config.get('check-definitions') + if check == 'ignore' or tracking_branch(dn): + return + + with app.chdir(gitdir), open(os.devnull, "w") as fnull: + ref = dn['ref'] + if ref == sha: + return + + def sha_in_ref(): + try: + return bool(check_output(['git', 'for-each-ref', '--contains', + sha, 'refs/*/' + ref], stderr=fnull, + universal_newlines=True).strip()) + except: + return False + + 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) @@ -134,9 +166,10 @@ def get_tree(dn): try: # Returns a (tree, sha) tuple. - return check_output(['git', 'rev-parse', ref + '^{tree}', ref], - universal_newlines=True).split() - + treesha = check_output(['git', 'rev-parse', ref + '^{tree}', ref], + universal_newlines=True).split() + ensure_ref_contains_sha(dn, gitdir, dn['ref'], treesha[1]) + return treesha except: # either we don't have a git dir, or ref is not unique # or ref does not exist |