improving ref checker

pull/300/head
Chris Holdgraf 2018-06-27 09:39:15 -07:00
rodzic 728fb9707e
commit 544b1ba039
2 zmienionych plików z 23 dodań i 28 usunięć

Wyświetl plik

@ -175,16 +175,16 @@ class Repo2Docker(Application):
sys.exit(1)
if ref:
ref = check_ref(ref)
try:
for line in execute_cmd(['git', 'reset', '--hard', ref],
cwd=checkout_path,
capture=self.json_logs):
self.log.info(line, extra=dict(phase='fetching'))
except subprocess.CalledProcessError:
hash = check_ref(ref, checkout_path)
if hash is None:
self.log.error('Failed to check out ref %s', ref,
extra=dict(phase='failed'))
sys.exit(1)
# If the hash is resolved above, we should be able to reset to it
for line in execute_cmd(['git', 'reset', '--hard', hash],
cwd=checkout_path,
capture=self.json_logs):
self.log.info(line, extra=dict(phase='fetching'))
def validate_image_name(self, image_name):
"""

Wyświetl plik

@ -272,26 +272,21 @@ class ByteSpecification(Integer):
return int(float(num) * self.UNIT_SUFFIXES[suffix])
def check_ref(ref):
def check_ref(ref, cwd=None):
"""Prepare a ref and ensure it works with git reset --hard."""
ref_checks = {'tag': "refs/tags/{}",
'branch': "refs/heads/{}",
'remote': "refs/remotes/origin/{}",
'commit': "{}^{{commit}}"}
chosen_kind = None
for kind, check in ref_checks:
check = check.split('/')[-1]
parts = ["git", "show-ref", "--verify", check.format(ref)]
success = subprocess.call(parts)
if success:
break
# Try original ref, then trying a remote ref, then removing remote
refs = [ref, # Original ref
'/'.join(["origin", ref]), # In case its a remote branch
ref.split('/')[-1]] # In case partial commit w/ remote
if chosen_kind is None:
raise ValueError("Could not resolve the ref properly, ensure that "
"it is a commit hash / tag / branch name.")
if chosen_kind == "remote" and not check.startswith('origin/'):
# Ensure that the ref has `origin` in front
self.log.info("Ref did not have a remote specified, "
"adding 'origin/' to ref.\n")
check = '/'.join(["origin", check])
return check
hash = None
for i_ref in refs:
call = ["git", "rev-parse", "--quiet", i_ref]
try:
# If success, output will be <hash>
response = subprocess.check_output(call, stderr=subprocess.DEVNULL, cwd=cwd)
hash = response.decode().strip()
except Exception:
# We'll throw an error later if no refs resolve
pass
return hash