unpack-trees: introduce preserve_ignored to unpack_trees_options

Currently, every caller of unpack_trees() that wants to ensure ignored files are overwritten by default needs to: * allocate unpack_trees_options.dir * flip the DIR_SHOW_IGNORED flag in unpack_trees_options.dir->flags * call setup_standard_excludes AND then after the call to unpack_trees() needs to * call dir_clear() * deallocate unpack_trees_options.dir That's a fair amount of boilerplate, and every caller uses identical code. Make this easier by instead introducing a new boolean value where the default value (0) does what we want so that new callers of unpack_trees() automatically get the appropriate behavior. And move all the handling of unpack_trees_options.dir into unpack_trees() itself. While preserve_ignored = 0 is the behavior we feel is the appropriate default, we defer fixing commands to use the appropriate default until a later commit. So, this commit introduces several locations where we manually set preserve_ignored=1. This makes it clear where code paths were previously preserving ignored files when they should not have been; a future commit will flip these to instead use a value of 0 to get the behavior we want. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Sep 27, 2021 at 16:33 UTC 04988c8d182da945cd9420274f33487157c5636f
14 files changed +35 -40
builtin/am.c
+3
@@ -1919,6 +1919,9 @@ static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1919 opts.update = 1;
1920 opts.merge = 1;
1921 opts.reset = reset;
1922 + if (!reset)
1923 + /* FIXME: Default should be to remove ignored files */
1924 + opts.preserve_ignored = 1;
1925 opts.fn = twoway_merge;
1926 init_tree_desc(&t[0], head->buffer, head->size);
1927 init_tree_desc(&t[1], remote->buffer, remote->size);
builtin/checkout.c
+2 -9
@@ -648,6 +648,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
648 opts.skip_unmerged = !worktree;
649 opts.reset = 1;
650 opts.merge = 1;
651 + opts.preserve_ignored = 0;
652 opts.fn = oneway_merge;
653 opts.verbose_update = o->show_progress;
654 opts.src_index = &the_index;
@@ -746,11 +747,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
747 new_branch_info->commit ?
748 &new_branch_info->commit->object.oid :
749 &new_branch_info->oid, NULL);
749 - if (opts->overwrite_ignore) {
750 - topts.dir = xcalloc(1, sizeof(*topts.dir));
751 - topts.dir->flags |= DIR_SHOW_IGNORED;
752 - setup_standard_excludes(topts.dir);
753 - }
750 + topts.preserve_ignored = !opts->overwrite_ignore;
751 tree = parse_tree_indirect(old_branch_info->commit ?
752 &old_branch_info->commit->object.oid :
753 the_hash_algo->empty_tree);
@@ -760,10 +757,6 @@ static int merge_working_tree(const struct checkout_opts *opts,
757 init_tree_desc(&trees[1], tree->buffer, tree->size);
758
759 ret = unpack_trees(2, trees, &topts);
763 - if (topts.dir) {
764 - dir_clear(topts.dir);
765 - FREE_AND_NULL(topts.dir);
766 - }
760 clear_unpack_trees_porcelain(&topts);
761 if (ret == -1) {
762 /*
builtin/clone.c
+2
@@ -803,6 +803,8 @@ static int checkout(int submodule_progress)
803 opts.update = 1;
804 opts.merge = 1;
805 opts.clone = 1;
806 + /* FIXME: Default should be to remove ignored files */
807 + opts.preserve_ignored = 1;
808 opts.fn = oneway_merge;
809 opts.verbose_update = (option_verbosity >= 0);
810 opts.src_index = &the_index;
builtin/merge.c
+2
@@ -681,6 +681,8 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
681 opts.verbose_update = 1;
682 opts.trivial_merges_only = 1;
683 opts.merge = 1;
684 + /* FIXME: Default should be to remove ignored files */
685 + opts.preserve_ignored = 1;
686 trees[nr_trees] = parse_tree_indirect(common);
687 if (!trees[nr_trees++])
688 return -1;
builtin/read-tree.c
+3 -10
@@ -201,11 +201,9 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
201 if ((opts.update || opts.index_only) && !opts.merge)
202 die("%s is meaningless without -m, --reset, or --prefix",
203 opts.update ? "-u" : "-i");
204 - if (opts.update && !opts.reset) {
205 - CALLOC_ARRAY(opts.dir, 1);
206 - opts.dir->flags |= DIR_SHOW_IGNORED;
207 - setup_standard_excludes(opts.dir);
208 - }
204 + if (opts.update && !opts.reset)
205 + opts.preserve_ignored = 0;
206 + /* otherwise, opts.preserve_ignored is irrelevant */
207 if (opts.merge && !opts.index_only)
208 setup_work_tree();
209
@@ -245,11 +243,6 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
243 if (unpack_trees(nr_trees, t, &opts))
244 return 128;
245
248 - if (opts.dir) {
249 - dir_clear(opts.dir);
250 - FREE_AND_NULL(opts.dir);
251 - }
252 -
246 if (opts.debug_unpack || opts.dry_run)
247 return 0; /* do not write the index out */
248
builtin/reset.c
+2
@@ -67,6 +67,8 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t
67 case KEEP:
68 case MERGE:
69 opts.update = 1;
70 + /* FIXME: Default should be to remove ignored files */
71 + opts.preserve_ignored = 1;
72 break;
73 case HARD:
74 opts.update = 1;
builtin/stash.c
+3
@@ -258,6 +258,9 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
258 opts.merge = 1;
259 opts.reset = reset;
260 opts.update = update;
261 + if (update && !reset)
262 + /* FIXME: Default should be to remove ignored files */
263 + opts.preserve_ignored = 1;
264 opts.fn = oneway_merge;
265
266 if (unpack_trees(nr_trees, t, &opts))
merge-ort.c
+1 -7
@@ -4045,11 +4045,7 @@ static int checkout(struct merge_options *opt,
4045 unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
4046 unpack_opts.verbose_update = (opt->verbosity > 2);
4047 unpack_opts.fn = twoway_merge;
4048 - if (1/* FIXME: opts->overwrite_ignore*/) {
4049 - CALLOC_ARRAY(unpack_opts.dir, 1);
4050 - unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
4051 - setup_standard_excludes(unpack_opts.dir);
4052 - }
4048 + unpack_opts.preserve_ignored = 0; /* FIXME: !opts->overwrite_ignore*/
4049 parse_tree(prev);
4050 init_tree_desc(&trees[0], prev->buffer, prev->size);
4051 parse_tree(next);
@@ -4057,8 +4053,6 @@ static int checkout(struct merge_options *opt,
4053
4054 ret = unpack_trees(2, trees, &unpack_opts);
4055 clear_unpack_trees_porcelain(&unpack_opts);
4060 - dir_clear(unpack_opts.dir);
4061 - FREE_AND_NULL(unpack_opts.dir);
4056 return ret;
4057 }
4058
merge-recursive.c
+1 -7
@@ -411,9 +411,7 @@ static int unpack_trees_start(struct merge_options *opt,
411 else {
412 opt->priv->unpack_opts.update = 1;
413 /* FIXME: should only do this if !overwrite_ignore */
414 - CALLOC_ARRAY(opt->priv->unpack_opts.dir, 1);
415 - opt->priv->unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
416 - setup_standard_excludes(opt->priv->unpack_opts.dir);
414 + opt->priv->unpack_opts.preserve_ignored = 0;
415 }
416 opt->priv->unpack_opts.merge = 1;
417 opt->priv->unpack_opts.head_idx = 2;
@@ -428,10 +426,6 @@ static int unpack_trees_start(struct merge_options *opt,
426 init_tree_desc_from_tree(t+2, merge);
427
428 rc = unpack_trees(3, t, &opt->priv->unpack_opts);
431 - if (opt->priv->unpack_opts.dir) {
432 - dir_clear(opt->priv->unpack_opts.dir);
433 - FREE_AND_NULL(opt->priv->unpack_opts.dir);
434 - }
429 cache_tree_free(&opt->repo->index->cache_tree);
430
431 /*
merge.c
+1 -7
@@ -53,7 +53,6 @@ int checkout_fast_forward(struct repository *r,
53 struct unpack_trees_options opts;
54 struct tree_desc t[MAX_UNPACK_TREES];
55 int i, nr_trees = 0;
56 - struct dir_struct dir = DIR_INIT;
56 struct lock_file lock_file = LOCK_INIT;
57
58 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
@@ -80,11 +79,7 @@ int checkout_fast_forward(struct repository *r,
79 }
80
81 memset(&opts, 0, sizeof(opts));
83 - if (overwrite_ignore) {
84 - dir.flags |= DIR_SHOW_IGNORED;
85 - setup_standard_excludes(&dir);
86 - opts.dir = &dir;
87 - }
82 + opts.preserve_ignored = !overwrite_ignore;
83
84 opts.head_idx = 1;
85 opts.src_index = r->index;
@@ -101,7 +96,6 @@ int checkout_fast_forward(struct repository *r,
96 clear_unpack_trees_porcelain(&opts);
97 return -1;
98 }
104 - dir_clear(&dir);
99 clear_unpack_trees_porcelain(&opts);
100
101 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
reset.c
+2
@@ -56,6 +56,8 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
56 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
57 unpack_tree_opts.update = 1;
58 unpack_tree_opts.merge = 1;
59 + /* FIXME: Default should be to remove ignored files */
60 + unpack_tree_opts.preserve_ignored = 1;
61 init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);
62 if (!detach_head)
63 unpack_tree_opts.reset = 1;
sequencer.c
+2
@@ -3690,6 +3690,8 @@ static int do_reset(struct repository *r,
3690 unpack_tree_opts.fn = oneway_merge;
3691 unpack_tree_opts.merge = 1;
3692 unpack_tree_opts.update = 1;
3693 + /* FIXME: Default should be to remove ignored files */
3694 + unpack_tree_opts.preserve_ignored = 1;
3695 init_checkout_metadata(&unpack_tree_opts.meta, name, &oid, NULL);
3696
3697 if (repo_read_index_unmerged(r)) {
unpack-trees.c
+10
@@ -1705,6 +1705,12 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1705 ensure_full_index(o->dst_index);
1706 }
1707
1708 + if (!o->preserve_ignored) {
1709 + CALLOC_ARRAY(o->dir, 1);
1710 + o->dir->flags |= DIR_SHOW_IGNORED;
1711 + setup_standard_excludes(o->dir);
1712 + }
1713 +
1714 if (!core_apply_sparse_checkout || !o->update)
1715 o->skip_sparse_checkout = 1;
1716 if (!o->skip_sparse_checkout && !o->pl) {
@@ -1866,6 +1872,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
1872 done:
1873 if (free_pattern_list)
1874 clear_pattern_list(&pl);
1875 + if (o->dir) {
1876 + dir_clear(o->dir);
1877 + FREE_AND_NULL(o->dir);
1878 + }
1879 trace2_region_leave("unpack_trees", "unpack_trees", the_repository);
1880 trace_performance_leave("unpack_trees");
1881 return ret;
unpack-trees.h
+1
@@ -49,6 +49,7 @@ struct unpack_trees_options {
49 unsigned int reset,
50 merge,
51 update,
52 + preserve_ignored,
53 clone,
54 index_only,
55 nontrivial_merge,