@matheusbsbs / matheus-bagliano / commits / d89f09c828
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC d89f09c8289a764f0a974e02bd5b38cf60d1a7d7
4 files changed +53 -1
Documentation/git-clone.txt
+7 -1
@@ -15,7 +15,7 @@ SYNOPSIS
15 [--dissociate] [--separate-git-dir <git dir>]
16 [--depth <depth>] [--[no-]single-branch] [--no-tags]
17 [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
18 - [--[no-]remote-submodules] [--jobs <n>] [--] <repository>
18 + [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--] <repository>
19 [<directory>]
20
21 DESCRIPTION
@@ -156,6 +156,12 @@ objects from the source repository into a pack in the cloned repository.
156 used, neither remote-tracking branches nor the related
157 configuration variables are created.
158
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 +
165 --mirror::
166 Set up a mirror of the source repository. This implies `--bare`.
167 Compared to `--bare`, `--mirror` not only maps local branches of the
builtin/clone.c
+27
@@ -59,6 +59,7 @@ static const char *real_git_dir;
59 static char *option_upload_pack = "git-upload-pack";
60 static int option_verbosity;
61 static int option_progress = -1;
62 +static int option_sparse_checkout;
63 static enum transport_family family;
64 static struct string_list option_config = STRING_LIST_INIT_NODUP;
65 static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
@@ -146,6 +147,8 @@ static struct option builtin_clone_options[] = {
147 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
148 OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
149 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")),
152 OPT_END()
153 };
154
@@ -733,6 +736,27 @@ static void update_head(const struct ref *our, const struct ref *remote,
736 }
737 }
738
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 +
760 static int checkout(int submodule_progress)
761 {
762 struct object_id oid;
@@ -1106,6 +1130,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1130 if (option_required_reference.nr || option_optional_reference.nr)
1131 setup_reference();
1132
1133 + if (option_sparse_checkout && git_sparse_checkout_init(repo))
1134 + return 1;
1135 +
1136 remote = remote_get(option_origin);
1137
1138 strbuf_addf(&default_refspec, "+%s*:%s*", src_ref_prefix,
builtin/sparse-checkout.c
+6
@@ -102,6 +102,7 @@ static int sparse_checkout_init(int argc, const char **argv)
102 char *sparse_filename;
103 FILE *fp;
104 int res;
105 + struct object_id oid;
106
107 if (set_config(MODE_ALL_PATTERNS))
108 return 1;
@@ -126,6 +127,11 @@ static int sparse_checkout_init(int argc, const char **argv)
127 fprintf(fp, "/*\n!/*/\n");
128 fclose(fp);
129
130 + if (get_oid("HEAD", &oid)) {
131 + /* assume we are in a fresh repo */
132 + return 0;
133 + }
134 +
135 reset_dir:
136 return update_working_directory();
137 }
t/t1091-sparse-checkout-builtin.sh
+13
@@ -82,4 +82,17 @@ test_expect_success 'init with existing sparse-checkout' '
82 test_cmp expect dir
83 '
84
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 +
98 test_done