improving ref checker
MODIFY
repo2docker/app.py
repo2docker/app.py
@@ -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):
"""

MODIFY
repo2docker/utils.py
repo2docker/utils.py
@@ -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
-
- 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
+ # 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
+
+ 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

improving ref checker