Skip to content

Commit c4e01b3

Browse files
authored
Merge pull request #1160 from daradib/shallowhead
[MRG] Shallow clone HEAD
2 parents 61247be + 56bf038 commit c4e01b3

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

repo2docker/contentproviders/git.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import subprocess
2-
import sys
32

43
from .base import ContentProvider, ContentProviderException
54
from ..utils import execute_cmd, check_ref
@@ -17,12 +16,12 @@ def detect(self, source, ref=None, extra_args=None):
1716

1817
def fetch(self, spec, output_dir, yield_output=False):
1918
repo = spec["repo"]
20-
ref = spec.get("ref", None)
19+
ref = spec.get("ref") or "HEAD"
2120

2221
# make a, possibly shallow, clone of the remote repository
2322
try:
2423
cmd = ["git", "clone"]
25-
if ref is None:
24+
if ref == "HEAD":
2625
# check out of HEAD is performed after the clone is complete
2726
cmd.extend(["--depth", "1"])
2827
else:
@@ -35,13 +34,13 @@ def fetch(self, spec, output_dir, yield_output=False):
3534

3635
except subprocess.CalledProcessError as e:
3736
msg = "Failed to clone repository from {repo}".format(repo=repo)
38-
if ref is not None:
37+
if ref != "HEAD":
3938
msg += " (ref {ref})".format(ref=ref)
4039
msg += "."
4140
raise ContentProviderException(msg) from e
4241

4342
# check out the specific ref given by the user
44-
if ref is not None:
43+
if ref != "HEAD":
4544
hash = check_ref(ref, output_dir)
4645
if hash is None:
4746
self.log.error(

tests/unit/test_clone_depth.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Test that a clone depth of 1 is used and good enough when no refspec is used
2+
Test that a clone depth of 1 is used when HEAD or no refspec is used
33
44
Note: the tests don't actually run the container. Building the
55
container requires a specific repository and commit to be checked out,
@@ -25,7 +25,34 @@ def test_clone_depth():
2525
repo=URL,
2626
dry_run=True,
2727
run=False,
28-
# turn of automatic clean up of the checkout so we can inspect it
28+
# turn off automatic clean up of the checkout so we can inspect it
29+
# we also set the work directory explicitly so we know where to look
30+
cleanup_checkout=False,
31+
git_workdir=d,
32+
)
33+
app.initialize()
34+
app.start()
35+
36+
cmd = ["git", "rev-parse", "HEAD"]
37+
p = subprocess.run(cmd, stdout=subprocess.PIPE, cwd=d)
38+
assert p.stdout.strip() == b"703322e9c6635ba1835d3b92eafbabeca0042c3e"
39+
cmd = ["git", "rev-list", "--count", "HEAD"]
40+
p = subprocess.run(cmd, stdout=subprocess.PIPE, cwd=d)
41+
assert p.stdout.strip() == b"1"
42+
with open(os.path.join(d, "COMMIT")) as fp:
43+
assert fp.read() == "100\n"
44+
45+
46+
def test_clone_depth_head():
47+
"""Test a remote repository, with a refspec of 'HEAD'"""
48+
49+
with TemporaryDirectory() as d:
50+
app = Repo2Docker(
51+
repo=URL,
52+
ref="HEAD",
53+
dry_run=True,
54+
run=False,
55+
# turn off automatic clean up of the checkout so we can inspect it
2956
# we also set the work directory explicitly so we know where to look
3057
cleanup_checkout=False,
3158
git_workdir=d,
@@ -52,7 +79,7 @@ def test_clone_depth_full():
5279
ref="master",
5380
dry_run=True,
5481
run=False,
55-
# turn of automatic clean up of the checkout so we can inspect it
82+
# turn off automatic clean up of the checkout so we can inspect it
5683
# we also set the work directory explicitly so we know where to look
5784
cleanup_checkout=False,
5885
git_workdir=d,
@@ -80,7 +107,7 @@ def test_clone_depth_full2():
80107
ref="703322e",
81108
dry_run=True,
82109
run=False,
83-
# turn of automatic clean up of the checkout so we can inspect it
110+
# turn off automatic clean up of the checkout so we can inspect it
84111
# we also set the work directory explicitly so we know where to look
85112
cleanup_checkout=False,
86113
git_workdir=d,
@@ -108,7 +135,7 @@ def test_clone_depth_mid():
108135
ref="8bc4f21",
109136
dry_run=True,
110137
run=False,
111-
# turn of automatic clean up of the checkout so we can inspect it
138+
# turn off automatic clean up of the checkout so we can inspect it
112139
# we also set the work directory explicitly so we know where to look
113140
cleanup_checkout=False,
114141
git_workdir=d,

0 commit comments

Comments
 (0)