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