From 49d47b855e02cd80775276936c620f30c75c704b Mon Sep 17 00:00:00 2001 From: Dara Adib Date: Thu, 2 Jun 2022 18:52:08 -0400 Subject: [PATCH 1/3] Shallow clone HEAD Previously repo2docker only performed shallow clones when refspec was not provided. We now also perform shallow clones if refspec is explicitly set to HEAD. Binder always specifies a refspec, often HEAD, so this should improve some build times. --- repo2docker/contentproviders/git.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/repo2docker/contentproviders/git.py b/repo2docker/contentproviders/git.py index 5c914860..43333d70 100644 --- a/repo2docker/contentproviders/git.py +++ b/repo2docker/contentproviders/git.py @@ -17,12 +17,12 @@ class Git(ContentProvider): def fetch(self, spec, output_dir, yield_output=False): repo = spec["repo"] - ref = spec.get("ref", None) + ref = spec.get("ref") or "HEAD" # make a, possibly shallow, clone of the remote repository try: cmd = ["git", "clone"] - if ref is None: + if ref == "HEAD": # check out of HEAD is performed after the clone is complete cmd.extend(["--depth", "1"]) else: @@ -35,13 +35,13 @@ class Git(ContentProvider): except subprocess.CalledProcessError as e: msg = "Failed to clone repository from {repo}".format(repo=repo) - if ref is not None: + if ref != "HEAD": msg += " (ref {ref})".format(ref=ref) msg += "." raise ContentProviderException(msg) from e # check out the specific ref given by the user - if ref is not None: + if ref != "HEAD": hash = check_ref(ref, output_dir) if hash is None: self.log.error( From 991c249141d2271b3425e96961c615644ef0a035 Mon Sep 17 00:00:00 2001 From: Dara Adib Date: Thu, 2 Jun 2022 18:51:26 -0400 Subject: [PATCH 2/3] Remove unused sys import --- repo2docker/contentproviders/git.py | 1 - 1 file changed, 1 deletion(-) diff --git a/repo2docker/contentproviders/git.py b/repo2docker/contentproviders/git.py index 43333d70..7b624a87 100644 --- a/repo2docker/contentproviders/git.py +++ b/repo2docker/contentproviders/git.py @@ -1,5 +1,4 @@ import subprocess -import sys from .base import ContentProvider, ContentProviderException from ..utils import execute_cmd, check_ref From 56bf03811e16a2dda96266046a32668c74a14349 Mon Sep 17 00:00:00 2001 From: Dara Adib Date: Thu, 2 Jun 2022 20:42:29 -0400 Subject: [PATCH 3/3] Add test for shallow clone HEAD --- tests/unit/test_clone_depth.py | 37 +++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_clone_depth.py b/tests/unit/test_clone_depth.py index f482d3d0..7989b117 100644 --- a/tests/unit/test_clone_depth.py +++ b/tests/unit/test_clone_depth.py @@ -1,5 +1,5 @@ """ -Test that a clone depth of 1 is used and good enough when no refspec is used +Test that a clone depth of 1 is used when HEAD or no refspec is used Note: the tests don't actually run the container. Building the container requires a specific repository and commit to be checked out, @@ -25,7 +25,34 @@ def test_clone_depth(): repo=URL, dry_run=True, run=False, - # turn of automatic clean up of the checkout so we can inspect it + # turn off automatic clean up of the checkout so we can inspect it + # we also set the work directory explicitly so we know where to look + cleanup_checkout=False, + git_workdir=d, + ) + app.initialize() + app.start() + + cmd = ["git", "rev-parse", "HEAD"] + p = subprocess.run(cmd, stdout=subprocess.PIPE, cwd=d) + assert p.stdout.strip() == b"703322e9c6635ba1835d3b92eafbabeca0042c3e" + cmd = ["git", "rev-list", "--count", "HEAD"] + p = subprocess.run(cmd, stdout=subprocess.PIPE, cwd=d) + assert p.stdout.strip() == b"1" + with open(os.path.join(d, "COMMIT")) as fp: + assert fp.read() == "100\n" + + +def test_clone_depth_head(): + """Test a remote repository, with a refspec of 'HEAD'""" + + with TemporaryDirectory() as d: + app = Repo2Docker( + repo=URL, + ref="HEAD", + dry_run=True, + run=False, + # turn off automatic clean up of the checkout so we can inspect it # we also set the work directory explicitly so we know where to look cleanup_checkout=False, git_workdir=d, @@ -52,7 +79,7 @@ def test_clone_depth_full(): ref="master", dry_run=True, run=False, - # turn of automatic clean up of the checkout so we can inspect it + # turn off automatic clean up of the checkout so we can inspect it # we also set the work directory explicitly so we know where to look cleanup_checkout=False, git_workdir=d, @@ -80,7 +107,7 @@ def test_clone_depth_full2(): ref="703322e", dry_run=True, run=False, - # turn of automatic clean up of the checkout so we can inspect it + # turn off automatic clean up of the checkout so we can inspect it # we also set the work directory explicitly so we know where to look cleanup_checkout=False, git_workdir=d, @@ -108,7 +135,7 @@ def test_clone_depth_mid(): ref="8bc4f21", dry_run=True, run=False, - # turn of automatic clean up of the checkout so we can inspect it + # turn off automatic clean up of the checkout so we can inspect it # we also set the work directory explicitly so we know where to look cleanup_checkout=False, git_workdir=d,