4 #include "parse-options.h"
6 #include "repository.h"
7 #include "run-command.h"
9 #include "string-list.h"
11 #include "cache-tree.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15 #include "wt-status.h"
17 static const char *empty_base
= "";
19 static char const * const builtin_sparse_checkout_usage
[] = {
20 N_("git sparse-checkout (init|list|set|disable) <options>"),
24 static char *get_sparse_checkout_filename(void)
26 return git_pathdup("info/sparse-checkout");
29 static void write_patterns_to_file(FILE *fp
, struct pattern_list
*pl
)
33 for (i
= 0; i
< pl
->nr
; i
++) {
34 struct path_pattern
*p
= pl
->patterns
[i
];
36 if (p
->flags
& PATTERN_FLAG_NEGATIVE
)
39 fprintf(fp
, "%s", p
->pattern
);
41 if (p
->flags
& PATTERN_FLAG_MUSTBEDIR
)
48 static int sparse_checkout_list(int argc
, const char **argv
)
50 struct pattern_list pl
;
51 char *sparse_filename
;
54 memset(&pl
, 0, sizeof(pl
));
56 sparse_filename
= get_sparse_checkout_filename();
57 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
58 free(sparse_filename
);
61 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
65 write_patterns_to_file(stdout
, &pl
);
66 clear_pattern_list(&pl
);
71 static int update_working_directory(struct pattern_list
*pl
)
74 struct unpack_trees_options o
;
75 struct lock_file lock_file
= LOCK_INIT
;
79 struct repository
*r
= the_repository
;
81 if (repo_read_index_unmerged(r
))
82 die(_("you need to resolve your current index first"));
84 if (get_oid("HEAD", &oid
))
87 tree
= parse_tree_indirect(&oid
);
89 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
91 memset(&o
, 0, sizeof(o
));
92 o
.verbose_update
= isatty(2);
97 o
.src_index
= r
->index
;
98 o
.dst_index
= r
->index
;
99 o
.skip_sparse_checkout
= 0;
101 o
.keep_pattern_list
= !!pl
;
103 resolve_undo_clear_index(r
->index
);
106 cache_tree_free(&r
->index
->cache_tree
);
108 repo_hold_locked_index(r
, &lock_file
, LOCK_DIE_ON_ERROR
);
110 core_apply_sparse_checkout
= 1;
111 result
= unpack_trees(1, &t
, &o
);
114 prime_cache_tree(r
, r
->index
, tree
);
115 write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
);
117 rollback_lock_file(&lock_file
);
122 static void write_cone_to_file(FILE *fp
, struct pattern_list
*pl
)
125 struct pattern_entry
*pe
;
126 struct hashmap_iter iter
;
127 struct string_list sl
= STRING_LIST_INIT_DUP
;
128 struct strbuf parent_pattern
= STRBUF_INIT
;
130 hashmap_for_each_entry(&pl
->parent_hashmap
, &iter
, pe
, ent
) {
131 if (hashmap_get_entry(&pl
->recursive_hashmap
, pe
, ent
, NULL
))
134 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
137 string_list_insert(&sl
, pe
->pattern
);
140 string_list_sort(&sl
);
141 string_list_remove_duplicates(&sl
, 0);
143 fprintf(fp
, "/*\n!/*/\n");
145 for (i
= 0; i
< sl
.nr
; i
++) {
146 char *pattern
= sl
.items
[i
].string
;
149 fprintf(fp
, "%s/\n!%s/*/\n", pattern
, pattern
);
152 string_list_clear(&sl
, 0);
154 hashmap_for_each_entry(&pl
->recursive_hashmap
, &iter
, pe
, ent
) {
155 if (!hashmap_contains_parent(&pl
->recursive_hashmap
,
158 string_list_insert(&sl
, pe
->pattern
);
161 strbuf_release(&parent_pattern
);
163 string_list_sort(&sl
);
164 string_list_remove_duplicates(&sl
, 0);
166 for (i
= 0; i
< sl
.nr
; i
++) {
167 char *pattern
= sl
.items
[i
].string
;
168 fprintf(fp
, "%s/\n", pattern
);
172 static int write_patterns_and_update(struct pattern_list
*pl
)
174 char *sparse_filename
;
177 struct lock_file lk
= LOCK_INIT
;
180 sparse_filename
= get_sparse_checkout_filename();
181 fd
= hold_lock_file_for_update(&lk
, sparse_filename
,
184 result
= update_working_directory(pl
);
186 rollback_lock_file(&lk
);
187 free(sparse_filename
);
188 clear_pattern_list(pl
);
189 update_working_directory(NULL
);
193 fp
= xfdopen(fd
, "w");
195 if (core_sparse_checkout_cone
)
196 write_cone_to_file(fp
, pl
);
198 write_patterns_to_file(fp
, pl
);
201 commit_lock_file(&lk
);
203 free(sparse_filename
);
204 clear_pattern_list(pl
);
209 enum sparse_checkout_mode
{
210 MODE_NO_PATTERNS
= 0,
211 MODE_ALL_PATTERNS
= 1,
212 MODE_CONE_PATTERNS
= 2,
215 static int set_config(enum sparse_checkout_mode mode
)
217 const char *config_path
;
219 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
220 error(_("failed to set extensions.worktreeConfig setting"));
224 config_path
= git_path("config.worktree");
225 git_config_set_in_file_gently(config_path
,
226 "core.sparseCheckout",
227 mode
? "true" : NULL
);
229 git_config_set_in_file_gently(config_path
,
230 "core.sparseCheckoutCone",
231 mode
== MODE_CONE_PATTERNS
? "true" : NULL
);
236 static char const * const builtin_sparse_checkout_init_usage
[] = {
237 N_("git sparse-checkout init [--cone]"),
241 static struct sparse_checkout_init_opts
{
245 static int sparse_checkout_init(int argc
, const char **argv
)
247 struct pattern_list pl
;
248 char *sparse_filename
;
250 struct object_id oid
;
252 struct strbuf pattern
= STRBUF_INIT
;
254 static struct option builtin_sparse_checkout_init_options
[] = {
255 OPT_BOOL(0, "cone", &init_opts
.cone_mode
,
256 N_("initialize the sparse-checkout in cone mode")),
260 repo_read_index(the_repository
);
261 require_clean_work_tree(the_repository
,
262 N_("initialize sparse-checkout"), NULL
, 1, 0);
264 argc
= parse_options(argc
, argv
, NULL
,
265 builtin_sparse_checkout_init_options
,
266 builtin_sparse_checkout_init_usage
, 0);
268 if (init_opts
.cone_mode
) {
269 mode
= MODE_CONE_PATTERNS
;
270 core_sparse_checkout_cone
= 1;
272 mode
= MODE_ALL_PATTERNS
;
274 if (set_config(mode
))
277 memset(&pl
, 0, sizeof(pl
));
279 sparse_filename
= get_sparse_checkout_filename();
280 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
282 /* If we already have a sparse-checkout file, use it. */
284 free(sparse_filename
);
285 core_apply_sparse_checkout
= 1;
286 return update_working_directory(NULL
);
289 if (get_oid("HEAD", &oid
)) {
292 /* assume we are in a fresh repo, but update the sparse-checkout file */
293 fp
= xfopen(sparse_filename
, "w");
295 die(_("failed to open '%s'"), sparse_filename
);
297 free(sparse_filename
);
298 fprintf(fp
, "/*\n!/*/\n");
303 strbuf_addstr(&pattern
, "/*");
304 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
305 strbuf_addstr(&pattern
, "!/*/");
306 add_pattern(strbuf_detach(&pattern
, NULL
), empty_base
, 0, &pl
, 0);
308 return write_patterns_and_update(&pl
);
311 static void insert_recursive_pattern(struct pattern_list
*pl
, struct strbuf
*path
)
313 struct pattern_entry
*e
= xmalloc(sizeof(*e
));
314 e
->patternlen
= path
->len
;
315 e
->pattern
= strbuf_detach(path
, NULL
);
316 hashmap_entry_init(&e
->ent
,
318 strihash(e
->pattern
) :
319 strhash(e
->pattern
));
321 hashmap_add(&pl
->recursive_hashmap
, &e
->ent
);
323 while (e
->patternlen
) {
324 char *slash
= strrchr(e
->pattern
, '/');
325 char *oldpattern
= e
->pattern
;
328 if (slash
== e
->pattern
)
331 newlen
= slash
- e
->pattern
;
332 e
= xmalloc(sizeof(struct pattern_entry
));
333 e
->patternlen
= newlen
;
334 e
->pattern
= xstrndup(oldpattern
, newlen
);
335 hashmap_entry_init(&e
->ent
,
337 strihash(e
->pattern
) :
338 strhash(e
->pattern
));
340 if (!hashmap_get_entry(&pl
->parent_hashmap
, e
, ent
, NULL
))
341 hashmap_add(&pl
->parent_hashmap
, &e
->ent
);
345 static void strbuf_to_cone_pattern(struct strbuf
*line
, struct pattern_list
*pl
)
349 strbuf_trim_trailing_dir_sep(line
);
354 if (line
->buf
[0] != '/')
355 strbuf_insert(line
, 0, "/", 1);
357 insert_recursive_pattern(pl
, line
);
360 static char const * const builtin_sparse_checkout_set_usage
[] = {
361 N_("git sparse-checkout set (--stdin | <patterns>)"),
365 static struct sparse_checkout_set_opts
{
369 static int sparse_checkout_set(int argc
, const char **argv
, const char *prefix
)
372 struct pattern_list pl
;
374 int changed_config
= 0;
376 static struct option builtin_sparse_checkout_set_options
[] = {
377 OPT_BOOL(0, "stdin", &set_opts
.use_stdin
,
378 N_("read patterns from standard in")),
382 repo_read_index(the_repository
);
383 require_clean_work_tree(the_repository
,
384 N_("set sparse-checkout patterns"), NULL
, 1, 0);
386 memset(&pl
, 0, sizeof(pl
));
388 argc
= parse_options(argc
, argv
, prefix
,
389 builtin_sparse_checkout_set_options
,
390 builtin_sparse_checkout_set_usage
,
391 PARSE_OPT_KEEP_UNKNOWN
);
393 if (core_sparse_checkout_cone
) {
394 struct strbuf line
= STRBUF_INIT
;
396 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
397 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
398 pl
.use_cone_patterns
= 1;
400 if (set_opts
.use_stdin
) {
401 while (!strbuf_getline(&line
, stdin
))
402 strbuf_to_cone_pattern(&line
, &pl
);
404 for (i
= 0; i
< argc
; i
++) {
405 strbuf_setlen(&line
, 0);
406 strbuf_addstr(&line
, argv
[i
]);
407 strbuf_to_cone_pattern(&line
, &pl
);
411 if (set_opts
.use_stdin
) {
412 struct strbuf line
= STRBUF_INIT
;
414 while (!strbuf_getline(&line
, stdin
)) {
416 char *buf
= strbuf_detach(&line
, &len
);
417 add_pattern(buf
, empty_base
, 0, &pl
, 0);
420 for (i
= 0; i
< argc
; i
++)
421 add_pattern(argv
[i
], empty_base
, 0, &pl
, 0);
425 if (!core_apply_sparse_checkout
) {
426 set_config(MODE_ALL_PATTERNS
);
427 core_apply_sparse_checkout
= 1;
431 result
= write_patterns_and_update(&pl
);
433 if (result
&& changed_config
)
434 set_config(MODE_NO_PATTERNS
);
436 clear_pattern_list(&pl
);
440 static int sparse_checkout_disable(int argc
, const char **argv
)
442 struct pattern_list pl
;
443 struct strbuf match_all
= STRBUF_INIT
;
445 repo_read_index(the_repository
);
446 require_clean_work_tree(the_repository
,
447 N_("disable sparse-checkout"), NULL
, 1, 0);
449 memset(&pl
, 0, sizeof(pl
));
450 hashmap_init(&pl
.recursive_hashmap
, pl_hashmap_cmp
, NULL
, 0);
451 hashmap_init(&pl
.parent_hashmap
, pl_hashmap_cmp
, NULL
, 0);
452 pl
.use_cone_patterns
= 0;
453 core_apply_sparse_checkout
= 1;
455 strbuf_addstr(&match_all
, "/*");
456 add_pattern(strbuf_detach(&match_all
, NULL
), empty_base
, 0, &pl
, 0);
458 if (update_working_directory(&pl
))
459 die(_("error while refreshing working directory"));
461 clear_pattern_list(&pl
);
462 return set_config(MODE_NO_PATTERNS
);
465 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
467 static struct option builtin_sparse_checkout_options
[] = {
471 if (argc
== 2 && !strcmp(argv
[1], "-h"))
472 usage_with_options(builtin_sparse_checkout_usage
,
473 builtin_sparse_checkout_options
);
475 argc
= parse_options(argc
, argv
, prefix
,
476 builtin_sparse_checkout_options
,
477 builtin_sparse_checkout_usage
,
478 PARSE_OPT_STOP_AT_NON_OPTION
);
480 git_config(git_default_config
, NULL
);
483 if (!strcmp(argv
[0], "list"))
484 return sparse_checkout_list(argc
, argv
);
485 if (!strcmp(argv
[0], "init"))
486 return sparse_checkout_init(argc
, argv
);
487 if (!strcmp(argv
[0], "set"))
488 return sparse_checkout_set(argc
, argv
, prefix
);
489 if (!strcmp(argv
[0], "disable"))
490 return sparse_checkout_disable(argc
, argv
);
493 usage_with_options(builtin_sparse_checkout_usage
,
494 builtin_sparse_checkout_options
);