sparse-checkout: update working directory in-process for 'init'
[git] / builtin / sparse-checkout.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "pathspec.h"
6 #include "repository.h"
7 #include "run-command.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "cache.h"
11 #include "cache-tree.h"
12 #include "lockfile.h"
13 #include "resolve-undo.h"
14 #include "unpack-trees.h"
15
16 static const char *empty_base = "";
17
18 static char const * const builtin_sparse_checkout_usage[] = {
19         N_("git sparse-checkout (init|list|set|disable) <options>"),
20         NULL
21 };
22
23 static char *get_sparse_checkout_filename(void)
24 {
25         return git_pathdup("info/sparse-checkout");
26 }
27
28 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
29 {
30         int i;
31
32         for (i = 0; i < pl->nr; i++) {
33                 struct path_pattern *p = pl->patterns[i];
34
35                 if (p->flags & PATTERN_FLAG_NEGATIVE)
36                         fprintf(fp, "!");
37
38                 fprintf(fp, "%s", p->pattern);
39
40                 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
41                         fprintf(fp, "/");
42
43                 fprintf(fp, "\n");
44         }
45 }
46
47 static int sparse_checkout_list(int argc, const char **argv)
48 {
49         struct pattern_list pl;
50         char *sparse_filename;
51         int res;
52
53         memset(&pl, 0, sizeof(pl));
54
55         sparse_filename = get_sparse_checkout_filename();
56         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
57         free(sparse_filename);
58
59         if (res < 0) {
60                 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
61                 return 0;
62         }
63
64         write_patterns_to_file(stdout, &pl);
65         clear_pattern_list(&pl);
66
67         return 0;
68 }
69
70 static int update_working_directory(struct pattern_list *pl)
71 {
72         int result = 0;
73         struct unpack_trees_options o;
74         struct lock_file lock_file = LOCK_INIT;
75         struct object_id oid;
76         struct tree *tree;
77         struct tree_desc t;
78         struct repository *r = the_repository;
79
80         if (repo_read_index_unmerged(r))
81                 die(_("you need to resolve your current index first"));
82
83         if (get_oid("HEAD", &oid))
84                 return 0;
85
86         tree = parse_tree_indirect(&oid);
87         parse_tree(tree);
88         init_tree_desc(&t, tree->buffer, tree->size);
89
90         memset(&o, 0, sizeof(o));
91         o.verbose_update = isatty(2);
92         o.merge = 1;
93         o.update = 1;
94         o.fn = oneway_merge;
95         o.head_idx = -1;
96         o.src_index = r->index;
97         o.dst_index = r->index;
98         o.skip_sparse_checkout = 0;
99         o.pl = pl;
100         o.keep_pattern_list = !!pl;
101
102         resolve_undo_clear_index(r->index);
103         setup_work_tree();
104
105         cache_tree_free(&r->index->cache_tree);
106
107         repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
108
109         core_apply_sparse_checkout = 1;
110         result = unpack_trees(1, &t, &o);
111
112         if (!result) {
113                 prime_cache_tree(r, r->index, tree);
114                 write_locked_index(r->index, &lock_file, COMMIT_LOCK);
115         } else
116                 rollback_lock_file(&lock_file);
117
118         return result;
119 }
120
121 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
122 {
123         int i;
124         struct pattern_entry *pe;
125         struct hashmap_iter iter;
126         struct string_list sl = STRING_LIST_INIT_DUP;
127         struct strbuf parent_pattern = STRBUF_INIT;
128
129         hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
130                 if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
131                         continue;
132
133                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
134                                              pe->pattern,
135                                              &parent_pattern))
136                         string_list_insert(&sl, pe->pattern);
137         }
138
139         string_list_sort(&sl);
140         string_list_remove_duplicates(&sl, 0);
141
142         fprintf(fp, "/*\n!/*/\n");
143
144         for (i = 0; i < sl.nr; i++) {
145                 char *pattern = sl.items[i].string;
146
147                 if (strlen(pattern))
148                         fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
149         }
150
151         string_list_clear(&sl, 0);
152
153         hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
154                 if (!hashmap_contains_parent(&pl->recursive_hashmap,
155                                              pe->pattern,
156                                              &parent_pattern))
157                         string_list_insert(&sl, pe->pattern);
158         }
159
160         strbuf_release(&parent_pattern);
161
162         string_list_sort(&sl);
163         string_list_remove_duplicates(&sl, 0);
164
165         for (i = 0; i < sl.nr; i++) {
166                 char *pattern = sl.items[i].string;
167                 fprintf(fp, "%s/\n", pattern);
168         }
169 }
170
171 static int write_patterns_and_update(struct pattern_list *pl)
172 {
173         char *sparse_filename;
174         FILE *fp;
175         int fd;
176         struct lock_file lk = LOCK_INIT;
177         int result;
178
179         sparse_filename = get_sparse_checkout_filename();
180         fd = hold_lock_file_for_update(&lk, sparse_filename,
181                                       LOCK_DIE_ON_ERROR);
182
183         result = update_working_directory(pl);
184         if (result) {
185                 rollback_lock_file(&lk);
186                 free(sparse_filename);
187                 clear_pattern_list(pl);
188                 update_working_directory(NULL);
189                 return result;
190         }
191
192         fp = xfdopen(fd, "w");
193
194         if (core_sparse_checkout_cone)
195                 write_cone_to_file(fp, pl);
196         else
197                 write_patterns_to_file(fp, pl);
198
199         fflush(fp);
200         commit_lock_file(&lk);
201
202         free(sparse_filename);
203         clear_pattern_list(pl);
204
205         return 0;
206 }
207
208 enum sparse_checkout_mode {
209         MODE_NO_PATTERNS = 0,
210         MODE_ALL_PATTERNS = 1,
211         MODE_CONE_PATTERNS = 2,
212 };
213
214 static int set_config(enum sparse_checkout_mode mode)
215 {
216         const char *config_path;
217
218         if (git_config_set_gently("extensions.worktreeConfig", "true")) {
219                 error(_("failed to set extensions.worktreeConfig setting"));
220                 return 1;
221         }
222
223         config_path = git_path("config.worktree");
224         git_config_set_in_file_gently(config_path,
225                                       "core.sparseCheckout",
226                                       mode ? "true" : NULL);
227
228         git_config_set_in_file_gently(config_path,
229                                       "core.sparseCheckoutCone",
230                                       mode == MODE_CONE_PATTERNS ? "true" : NULL);
231
232         return 0;
233 }
234
235 static char const * const builtin_sparse_checkout_init_usage[] = {
236         N_("git sparse-checkout init [--cone]"),
237         NULL
238 };
239
240 static struct sparse_checkout_init_opts {
241         int cone_mode;
242 } init_opts;
243
244 static int sparse_checkout_init(int argc, const char **argv)
245 {
246         struct pattern_list pl;
247         char *sparse_filename;
248         int res;
249         struct object_id oid;
250         int mode;
251         struct strbuf pattern = STRBUF_INIT;
252
253         static struct option builtin_sparse_checkout_init_options[] = {
254                 OPT_BOOL(0, "cone", &init_opts.cone_mode,
255                          N_("initialize the sparse-checkout in cone mode")),
256                 OPT_END(),
257         };
258
259         argc = parse_options(argc, argv, NULL,
260                              builtin_sparse_checkout_init_options,
261                              builtin_sparse_checkout_init_usage, 0);
262
263         if (init_opts.cone_mode) {
264                 mode = MODE_CONE_PATTERNS;
265                 core_sparse_checkout_cone = 1;
266         } else
267                 mode = MODE_ALL_PATTERNS;
268
269         if (set_config(mode))
270                 return 1;
271
272         memset(&pl, 0, sizeof(pl));
273
274         sparse_filename = get_sparse_checkout_filename();
275         res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
276
277         /* If we already have a sparse-checkout file, use it. */
278         if (res >= 0) {
279                 free(sparse_filename);
280                 core_apply_sparse_checkout = 1;
281                 return update_working_directory(NULL);
282         }
283
284         if (get_oid("HEAD", &oid)) {
285                 FILE *fp;
286
287                 /* assume we are in a fresh repo, but update the sparse-checkout file */
288                 fp = xfopen(sparse_filename, "w");
289                 if (!fp)
290                         die(_("failed to open '%s'"), sparse_filename);
291
292                 free(sparse_filename);
293                 fprintf(fp, "/*\n!/*/\n");
294                 fclose(fp);
295                 return 0;
296         }
297
298         strbuf_addstr(&pattern, "/*");
299         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
300         strbuf_addstr(&pattern, "!/*/");
301         add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
302
303         return write_patterns_and_update(&pl);
304 }
305
306 static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
307 {
308         struct pattern_entry *e = xmalloc(sizeof(*e));
309         e->patternlen = path->len;
310         e->pattern = strbuf_detach(path, NULL);
311         hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
312
313         hashmap_add(&pl->recursive_hashmap, &e->ent);
314
315         while (e->patternlen) {
316                 char *slash = strrchr(e->pattern, '/');
317                 char *oldpattern = e->pattern;
318                 size_t newlen;
319
320                 if (slash == e->pattern)
321                         break;
322
323                 newlen = slash - e->pattern;
324                 e = xmalloc(sizeof(struct pattern_entry));
325                 e->patternlen = newlen;
326                 e->pattern = xstrndup(oldpattern, newlen);
327                 hashmap_entry_init(&e->ent, memhash(e->pattern, e->patternlen));
328
329                 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
330                         hashmap_add(&pl->parent_hashmap, &e->ent);
331         }
332 }
333
334 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
335 {
336         strbuf_trim(line);
337
338         strbuf_trim_trailing_dir_sep(line);
339
340         if (!line->len)
341                 return;
342
343         if (line->buf[0] != '/')
344                 strbuf_insert(line, 0, "/", 1);
345
346         insert_recursive_pattern(pl, line);
347 }
348
349 static char const * const builtin_sparse_checkout_set_usage[] = {
350         N_("git sparse-checkout set (--stdin | <patterns>)"),
351         NULL
352 };
353
354 static struct sparse_checkout_set_opts {
355         int use_stdin;
356 } set_opts;
357
358 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
359 {
360         int i;
361         struct pattern_list pl;
362         int result;
363         int changed_config = 0;
364
365         static struct option builtin_sparse_checkout_set_options[] = {
366                 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
367                          N_("read patterns from standard in")),
368                 OPT_END(),
369         };
370
371         memset(&pl, 0, sizeof(pl));
372
373         argc = parse_options(argc, argv, prefix,
374                              builtin_sparse_checkout_set_options,
375                              builtin_sparse_checkout_set_usage,
376                              PARSE_OPT_KEEP_UNKNOWN);
377
378         if (core_sparse_checkout_cone) {
379                 struct strbuf line = STRBUF_INIT;
380
381                 hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
382                 hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
383                 pl.use_cone_patterns = 1;
384
385                 if (set_opts.use_stdin) {
386                         while (!strbuf_getline(&line, stdin))
387                                 strbuf_to_cone_pattern(&line, &pl);
388                 } else {
389                         for (i = 0; i < argc; i++) {
390                                 strbuf_setlen(&line, 0);
391                                 strbuf_addstr(&line, argv[i]);
392                                 strbuf_to_cone_pattern(&line, &pl);
393                         }
394                 }
395         } else {
396                 if (set_opts.use_stdin) {
397                         struct strbuf line = STRBUF_INIT;
398
399                         while (!strbuf_getline(&line, stdin)) {
400                                 size_t len;
401                                 char *buf = strbuf_detach(&line, &len);
402                                 add_pattern(buf, empty_base, 0, &pl, 0);
403                         }
404                 } else {
405                         for (i = 0; i < argc; i++)
406                                 add_pattern(argv[i], empty_base, 0, &pl, 0);
407                 }
408         }
409
410         if (!core_apply_sparse_checkout) {
411                 set_config(MODE_ALL_PATTERNS);
412                 core_apply_sparse_checkout = 1;
413                 changed_config = 1;
414         }
415
416         result = write_patterns_and_update(&pl);
417
418         if (result && changed_config)
419                 set_config(MODE_NO_PATTERNS);
420
421         clear_pattern_list(&pl);
422         return result;
423 }
424
425 static int sparse_checkout_disable(int argc, const char **argv)
426 {
427         struct pattern_list pl;
428         struct strbuf match_all = STRBUF_INIT;
429
430         memset(&pl, 0, sizeof(pl));
431         hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
432         hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
433         pl.use_cone_patterns = 0;
434         core_apply_sparse_checkout = 1;
435
436         strbuf_addstr(&match_all, "/*");
437         add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
438
439         if (update_working_directory(&pl))
440                 die(_("error while refreshing working directory"));
441
442         clear_pattern_list(&pl);
443         return set_config(MODE_NO_PATTERNS);
444 }
445
446 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
447 {
448         static struct option builtin_sparse_checkout_options[] = {
449                 OPT_END(),
450         };
451
452         if (argc == 2 && !strcmp(argv[1], "-h"))
453                 usage_with_options(builtin_sparse_checkout_usage,
454                                    builtin_sparse_checkout_options);
455
456         argc = parse_options(argc, argv, prefix,
457                              builtin_sparse_checkout_options,
458                              builtin_sparse_checkout_usage,
459                              PARSE_OPT_STOP_AT_NON_OPTION);
460
461         git_config(git_default_config, NULL);
462
463         if (argc > 0) {
464                 if (!strcmp(argv[0], "list"))
465                         return sparse_checkout_list(argc, argv);
466                 if (!strcmp(argv[0], "init"))
467                         return sparse_checkout_init(argc, argv);
468                 if (!strcmp(argv[0], "set"))
469                         return sparse_checkout_set(argc, argv, prefix);
470                 if (!strcmp(argv[0], "disable"))
471                         return sparse_checkout_disable(argc, argv);
472         }
473
474         usage_with_options(builtin_sparse_checkout_usage,
475                            builtin_sparse_checkout_options);
476 }