Skip to content

Commit 5f3b21c

Browse files
committed
Merge branch 'sb/clone-shallow-passthru'
"git clone" learned "--shallow-submodules" option. * sb/clone-shallow-passthru: clone: add `--shallow-submodules` flag
2 parents ca158f4 + d22eb04 commit 5f3b21c

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

Documentation/git-clone.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ SYNOPSIS
1414
[-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
1515
[--dissociate] [--separate-git-dir <git dir>]
1616
[--depth <depth>] [--[no-]single-branch]
17-
[--recursive | --recurse-submodules] [--jobs <n>] [--] <repository>
18-
[<directory>]
17+
[--recursive | --recurse-submodules] [--[no-]shallow-submodules]
18+
[--jobs <n>] [--] <repository> [<directory>]
1919

2020
DESCRIPTION
2121
-----------
@@ -191,7 +191,9 @@ objects from the source repository into a pack in the cloned repository.
191191
Create a 'shallow' clone with a history truncated to the
192192
specified number of commits. Implies `--single-branch` unless
193193
`--no-single-branch` is given to fetch the histories near the
194-
tips of all branches.
194+
tips of all branches. This implies `--shallow-submodules`. If
195+
you want to have a shallow superproject clone, but full submodules,
196+
also pass `--no-shallow-submodules`.
195197

196198
--[no-]single-branch::
197199
Clone only the history leading to the tip of a single branch,
@@ -212,6 +214,9 @@ objects from the source repository into a pack in the cloned repository.
212214
repository does not have a worktree/checkout (i.e. if any of
213215
`--no-checkout`/`-n`, `--bare`, or `--mirror` is given)
214216

217+
--[no-]shallow-submodules::
218+
All submodules which are cloned will be shallow with a depth of 1.
219+
215220
--separate-git-dir=<git dir>::
216221
Instead of placing the cloned repository where it is supposed
217222
to be, place the cloned repository at the specified directory,

builtin/clone.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static const char * const builtin_clone_usage[] = {
4040

4141
static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
4242
static int option_local = -1, option_no_hardlinks, option_shared, option_recursive;
43+
static int option_shallow_submodules = -1;
4344
static char *option_template, *option_depth;
4445
static char *option_origin = NULL;
4546
static char *option_branch = NULL;
@@ -92,6 +93,8 @@ static struct option builtin_clone_options[] = {
9293
N_("create a shallow clone of that depth")),
9394
OPT_BOOL(0, "single-branch", &option_single_branch,
9495
N_("clone only one branch, HEAD or --branch")),
96+
OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
97+
N_("any cloned submodules will be shallow")),
9598
OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
9699
N_("separate git dir from working tree")),
97100
OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
@@ -735,6 +738,10 @@ static int checkout(void)
735738
struct argv_array args = ARGV_ARRAY_INIT;
736739
argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL);
737740

741+
if (option_shallow_submodules == 1
742+
|| (option_shallow_submodules == -1 && option_depth))
743+
argv_array_push(&args, "--depth=1");
744+
738745
if (max_jobs != -1)
739746
argv_array_pushf(&args, "--jobs=%d", max_jobs);
740747

t/t5614-clone-submodules.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh
2+
3+
test_description='Test shallow cloning of repos with submodules'
4+
5+
. ./test-lib.sh
6+
7+
pwd=$(pwd)
8+
9+
test_expect_success 'setup' '
10+
git checkout -b master &&
11+
test_commit commit1 &&
12+
test_commit commit2 &&
13+
mkdir sub &&
14+
(
15+
cd sub &&
16+
git init &&
17+
test_commit subcommit1 &&
18+
test_commit subcommit2 &&
19+
test_commit subcommit3
20+
) &&
21+
git submodule add "file://$pwd/sub" sub &&
22+
git commit -m "add submodule"
23+
'
24+
25+
test_expect_success 'nonshallow clone implies nonshallow submodule' '
26+
test_when_finished "rm -rf super_clone" &&
27+
git clone --recurse-submodules "file://$pwd/." super_clone &&
28+
(
29+
cd super_clone &&
30+
git log --oneline >lines &&
31+
test_line_count = 3 lines
32+
) &&
33+
(
34+
cd super_clone/sub &&
35+
git log --oneline >lines &&
36+
test_line_count = 3 lines
37+
)
38+
'
39+
40+
test_expect_success 'shallow clone implies shallow submodule' '
41+
test_when_finished "rm -rf super_clone" &&
42+
git clone --recurse-submodules --depth 2 "file://$pwd/." super_clone &&
43+
(
44+
cd super_clone &&
45+
git log --oneline >lines &&
46+
test_line_count = 2 lines
47+
) &&
48+
(
49+
cd super_clone/sub &&
50+
git log --oneline >lines &&
51+
test_line_count = 1 lines
52+
)
53+
'
54+
55+
test_expect_success 'shallow clone with non shallow submodule' '
56+
test_when_finished "rm -rf super_clone" &&
57+
git clone --recurse-submodules --depth 2 --no-shallow-submodules "file://$pwd/." super_clone &&
58+
(
59+
cd super_clone &&
60+
git log --oneline >lines &&
61+
test_line_count = 2 lines
62+
) &&
63+
(
64+
cd super_clone/sub &&
65+
git log --oneline >lines &&
66+
test_line_count = 3 lines
67+
)
68+
'
69+
70+
test_expect_success 'non shallow clone with shallow submodule' '
71+
test_when_finished "rm -rf super_clone" &&
72+
git clone --recurse-submodules --no-local --shallow-submodules "file://$pwd/." super_clone &&
73+
(
74+
cd super_clone &&
75+
git log --oneline >lines &&
76+
test_line_count = 3 lines
77+
) &&
78+
(
79+
cd super_clone/sub &&
80+
git log --oneline >lines &&
81+
test_line_count = 1 lines
82+
)
83+
'
84+
85+
test_done

0 commit comments

Comments
 (0)