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