Skip to content

Commit d89f09c

Browse files
derrickstoleegitster
authored andcommitted
clone: add --sparse mode
When someone wants to clone a large repository, but plans to work using a sparse-checkout file, they either need to do a full checkout first and then reduce the patterns they included, or clone with --no-checkout, set up their patterns, and then run a checkout manually. This requires knowing a lot about the repo shape and how sparse-checkout works. Add a new '--sparse' option to 'git clone' that initializes the sparse-checkout file to include the following patterns: /* !/*/ These patterns include every file in the root directory, but no directories. This allows a repo to include files like a README or a bootstrapping script to grow enlistments from that point. During the 'git sparse-checkout init' call, we must first look to see if HEAD is valid, since 'git clone' does not have a valid HEAD at the point where it initializes the sparse-checkout. The following checkout within the clone command will create the HEAD ref and update the working directory correctly. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bab3c35 commit d89f09c

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Documentation/git-clone.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SYNOPSIS
1515
[--dissociate] [--separate-git-dir <git dir>]
1616
[--depth <depth>] [--[no-]single-branch] [--no-tags]
1717
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
18-
[--[no-]remote-submodules] [--jobs <n>] [--] <repository>
18+
[--[no-]remote-submodules] [--jobs <n>] [--sparse] [--] <repository>
1919
[<directory>]
2020

2121
DESCRIPTION
@@ -156,6 +156,12 @@ objects from the source repository into a pack in the cloned repository.
156156
used, neither remote-tracking branches nor the related
157157
configuration variables are created.
158158

159+
--sparse::
160+
Initialize the sparse-checkout file so the working
161+
directory starts with only the files in the root
162+
of the repository. The sparse-checkout file can be
163+
modified to grow the working directory as needed.
164+
159165
--mirror::
160166
Set up a mirror of the source repository. This implies `--bare`.
161167
Compared to `--bare`, `--mirror` not only maps local branches of the

builtin/clone.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static const char *real_git_dir;
5959
static char *option_upload_pack = "git-upload-pack";
6060
static int option_verbosity;
6161
static int option_progress = -1;
62+
static int option_sparse_checkout;
6263
static enum transport_family family;
6364
static struct string_list option_config = STRING_LIST_INIT_NODUP;
6465
static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
@@ -146,6 +147,8 @@ static struct option builtin_clone_options[] = {
146147
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
147148
OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
148149
N_("any cloned submodules will use their remote-tracking branch")),
150+
OPT_BOOL(0, "sparse", &option_sparse_checkout,
151+
N_("initialize sparse-checkout file to include only files at root")),
149152
OPT_END()
150153
};
151154

@@ -733,6 +736,27 @@ static void update_head(const struct ref *our, const struct ref *remote,
733736
}
734737
}
735738

739+
static int git_sparse_checkout_init(const char *repo)
740+
{
741+
struct argv_array argv = ARGV_ARRAY_INIT;
742+
int result = 0;
743+
argv_array_pushl(&argv, "-C", repo, "sparse-checkout", "init", NULL);
744+
745+
/*
746+
* We must apply the setting in the current process
747+
* for the later checkout to use the sparse-checkout file.
748+
*/
749+
core_apply_sparse_checkout = 1;
750+
751+
if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
752+
error(_("failed to initialize sparse-checkout"));
753+
result = 1;
754+
}
755+
756+
argv_array_clear(&argv);
757+
return result;
758+
}
759+
736760
static int checkout(int submodule_progress)
737761
{
738762
struct object_id oid;
@@ -1106,6 +1130,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11061130
if (option_required_reference.nr || option_optional_reference.nr)
11071131
setup_reference();
11081132

1133+
if (option_sparse_checkout && git_sparse_checkout_init(repo))
1134+
return 1;
1135+
11091136
remote = remote_get(option_origin);
11101137

11111138
strbuf_addf(&default_refspec, "+%s*:%s*", src_ref_prefix,

builtin/sparse-checkout.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static int sparse_checkout_init(int argc, const char **argv)
102102
char *sparse_filename;
103103
FILE *fp;
104104
int res;
105+
struct object_id oid;
105106

106107
if (set_config(MODE_ALL_PATTERNS))
107108
return 1;
@@ -126,6 +127,11 @@ static int sparse_checkout_init(int argc, const char **argv)
126127
fprintf(fp, "/*\n!/*/\n");
127128
fclose(fp);
128129

130+
if (get_oid("HEAD", &oid)) {
131+
/* assume we are in a fresh repo */
132+
return 0;
133+
}
134+
129135
reset_dir:
130136
return update_working_directory();
131137
}

t/t1091-sparse-checkout-builtin.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ test_expect_success 'init with existing sparse-checkout' '
8282
test_cmp expect dir
8383
'
8484

85+
test_expect_success 'clone --sparse' '
86+
git clone --sparse repo clone &&
87+
git -C clone sparse-checkout list >actual &&
88+
cat >expect <<-EOF &&
89+
/*
90+
!/*/
91+
EOF
92+
test_cmp expect actual &&
93+
ls clone >dir &&
94+
echo a >expect &&
95+
test_cmp expect dir
96+
'
97+
8598
test_done

0 commit comments

Comments
 (0)