Merge branch 'ln/userdiff-elixir'
[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,
317                            ignore_case ?
318                            strihash(e->pattern) :
319                            strhash(e->pattern));
320
321         hashmap_add(&pl->recursive_hashmap, &e->ent);
322
323         while (e->patternlen) {
324                 char *slash = strrchr(e->pattern, '/');
325                 char *oldpattern = e->pattern;
326                 size_t newlen;
327
328                 if (slash == e->pattern)
329                         break;
330
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,
336                                    ignore_case ?
337                                    strihash(e->pattern) :
338                                    strhash(e->pattern));
339
340                 if (!hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL))
341                         hashmap_add(&pl->parent_hashmap, &e->ent);
342         }
343 }
344
345 static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
346 {
347         strbuf_trim(line);
348
349         strbuf_trim_trailing_dir_sep(line);
350
351         if (!line->len)
352                 return;
353
354         if (line->buf[0] != '/')
355                 strbuf_insert(line, 0, "/", 1);
356
357         insert_recursive_pattern(pl, line);
358 }
359
360 static char const * const builtin_sparse_checkout_set_usage[] = {
361         N_("git sparse-checkout set (--stdin | <patterns>)"),
362         NULL
363 };
364
365 static struct sparse_checkout_set_opts {
366         int use_stdin;
367 } set_opts;
368
369 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
370 {
371         int i;
372         struct pattern_list pl;
373         int result;
374         int changed_config = 0;
375
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")),
379                 OPT_END(),
380         };
381
382         repo_read_index(the_repository);
383         require_clean_work_tree(the_repository,
384                                 N_("set sparse-checkout patterns"), NULL, 1, 0);
385
386         memset(&pl, 0, sizeof(pl));
387
388         argc = parse_options(argc, argv, prefix,
389                              builtin_sparse_checkout_set_options,
390                              builtin_sparse_checkout_set_usage,
391                              PARSE_OPT_KEEP_UNKNOWN);
392
393         if (core_sparse_checkout_cone) {
394                 struct strbuf line = STRBUF_INIT;
395
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;
399
400                 if (set_opts.use_stdin) {
401                         while (!strbuf_getline(&line, stdin))
402                                 strbuf_to_cone_pattern(&line, &pl);
403                 } else {
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);
408                         }
409                 }
410         } else {
411                 if (set_opts.use_stdin) {
412                         struct strbuf line = STRBUF_INIT;
413
414                         while (!strbuf_getline(&line, stdin)) {
415                                 size_t len;
416                                 char *buf = strbuf_detach(&line, &len);
417                                 add_pattern(buf, empty_base, 0, &pl, 0);
418                         }
419                 } else {
420                         for (i = 0; i < argc; i++)
421                                 add_pattern(argv[i], empty_base, 0, &pl, 0);
422                 }
423         }
424
425         if (!core_apply_sparse_checkout) {
426                 set_config(MODE_ALL_PATTERNS);
427                 core_apply_sparse_checkout = 1;
428                 changed_config = 1;
429         }
430
431         result = write_patterns_and_update(&pl);
432
433         if (result && changed_config)
434                 set_config(MODE_NO_PATTERNS);
435
436         clear_pattern_list(&pl);
437         return result;
438 }
439
440 static int sparse_checkout_disable(int argc, const char **argv)
441 {
442         struct pattern_list pl;
443         struct strbuf match_all = STRBUF_INIT;
444
445         repo_read_index(the_repository);
446         require_clean_work_tree(the_repository,
447                                 N_("disable sparse-checkout"), NULL, 1, 0);
448
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;
454
455         strbuf_addstr(&match_all, "/*");
456         add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
457
458         if (update_working_directory(&pl))
459                 die(_("error while refreshing working directory"));
460
461         clear_pattern_list(&pl);
462         return set_config(MODE_NO_PATTERNS);
463 }
464
465 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
466 {
467         static struct option builtin_sparse_checkout_options[] = {
468                 OPT_END(),
469         };
470
471         if (argc == 2 && !strcmp(argv[1], "-h"))
472                 usage_with_options(builtin_sparse_checkout_usage,
473                                    builtin_sparse_checkout_options);
474
475         argc = parse_options(argc, argv, prefix,
476                              builtin_sparse_checkout_options,
477                              builtin_sparse_checkout_usage,
478                              PARSE_OPT_STOP_AT_NON_OPTION);
479
480         git_config(git_default_config, NULL);
481
482         if (argc > 0) {
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);
491         }
492
493         usage_with_options(builtin_sparse_checkout_usage,
494                            builtin_sparse_checkout_options);
495 }