read-tree, merge-recursive: overwrite ignored files by default

This fixes a long-standing patchwork of ignored files handling in read-tree and merge-recursive, called out and suggested by Junio long ago. Quoting from commit dcf0c16ef1 ("core.excludesfile clean-up" 2007-11-16): git-read-tree takes --exclude-per-directory=<gitignore>, not because the flexibility was needed. Again, this was because the option predates the standardization of the ignore files. ... On the other hand, I think it makes perfect sense to fix git-read-tree, git-merge-recursive and git-clean to follow the same rule as other commands. I do not think of a valid use case to give an exclude-per-directory that is nonstandard to read-tree command, outside a "negative" test in the t1004 test script. This patch is the first step to untangle this mess. The next step would be to teach read-tree, merge-recursive and clean (in C) to use setup_standard_excludes(). History shows each of these were partially or fully fixed: * clean was taught the new trick in 1617adc7a0 ("Teach git clean to use setup_standard_excludes()", 2007-11-14). * read-tree was primarily used by checkout & merge scripts. checkout and merge later became builtins and were both fixed to use the new setup_standard_excludes() handling in fc001b526c ("checkout,merge: loosen overwriting untracked file check based on info/exclude", 2011-11-27). So the primary users were fixed, though read-tree itself was not. * merge-recursive has now been replaced as the default merge backend by merge-ort. merge-ort fixed this by using setup_standard_excludes() starting early in its implementation; see commit 6681ce5cf6 ("merge-ort: add implementation of checkout()", 2020-12-13), largely due to its design depending on checkout() and thus being influenced by the checkout code. However, merge-recursive itself was not fixed here, in part because its design meant it had difficulty differentiating between untracked files, ignored files, leftover tracked files that haven't been removed yet due to order of processing files, and files written by itself due to collisions). Make the conversion more complete by now handling read-tree and handling at least the unpack_trees() portion of merge-recursive. While merge-recursive is on its way out, fixing the unpack_trees() portion is easy and facilitates some of the later changes in this series. Note that fixing read-tree makes the --exclude-per-directory option to read-tree useless, so we remove it from the documentation (though we continue to accept it if passed). The read-tree changes happen to fix a bug in t1013. 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 491a7575f188cbf6cfb5be75981a40beb4c22b44
4 files changed +21 -34
Documentation/git-read-tree.txt
+1 -17
@@ -10,8 +10,7 @@ SYNOPSIS
10 --------
11 [verse]
12 'git read-tree' [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]
13 - [-u [--exclude-per-directory=<gitignore>] | -i]]
14 - [--index-output=<file>] [--no-sparse-checkout]
13 + [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]
14 (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])
15
16
@@ -88,21 +87,6 @@ OPTIONS
87 The command will refuse to overwrite entries that already
88 existed in the original index file.
89
91 ---exclude-per-directory=<gitignore>::
92 - When running the command with `-u` and `-m` options, the
93 - merge result may need to overwrite paths that are not
94 - tracked in the current branch. The command usually
95 - refuses to proceed with the merge to avoid losing such a
96 - path. However this safety valve sometimes gets in the
97 - way. For example, it often happens that the other
98 - branch added a file that used to be a generated file in
99 - your branch, and the safety valve triggers when you try
100 - to switch to that branch after you ran `make` but before
101 - running `make clean` to remove the generated file. This
102 - option tells the command to read per-directory exclude
103 - file (usually '.gitignore') and allows such an untracked
104 - but explicitly ignored file to be overwritten.
105 -
90 --index-output=<file>::
91 Instead of writing the results out to `$GIT_INDEX_FILE`,
92 write the resulting index in the named file. While the
builtin/read-tree.c
+10 -15
@@ -38,7 +38,7 @@ static int list_tree(struct object_id *oid)
38 }
39
40 static const char * const read_tree_usage[] = {
41 - N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
41 + N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
42 NULL
43 };
44
@@ -53,24 +53,16 @@ static int index_output_cb(const struct option *opt, const char *arg,
53 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
54 int unset)
55 {
56 - struct dir_struct *dir;
56 struct unpack_trees_options *opts;
57
58 BUG_ON_OPT_NEG(unset);
59
60 opts = (struct unpack_trees_options *)opt->value;
61
63 - if (opts->dir)
64 - die("more than one --exclude-per-directory given.");
65 -
66 - dir = xcalloc(1, sizeof(*opts->dir));
67 - dir->flags |= DIR_SHOW_IGNORED;
68 - dir->exclude_per_dir = arg;
69 - opts->dir = dir;
70 - /* We do not need to nor want to do read-directory
71 - * here; we are merely interested in reusing the
72 - * per directory ignore stack mechanism.
73 - */
62 + if (!opts->update)
63 + die("--exclude-per-directory is meaningless unless -u");
64 + if (strcmp(arg, ".gitignore"))
65 + die("--exclude-per-directory argument must be .gitignore");
66 return 0;
67 }
68
@@ -209,8 +201,11 @@ 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");
212 - if ((opts.dir && !opts.update))
213 - die("--exclude-per-directory is meaningless unless -u");
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 + }
209 if (opts.merge && !opts.index_only)
210 setup_work_tree();
211
merge-recursive.c
+10 -1
@@ -408,8 +408,13 @@ static int unpack_trees_start(struct merge_options *opt,
408 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
409 if (opt->priv->call_depth)
410 opt->priv->unpack_opts.index_only = 1;
411 - else
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);
417 + }
418 opt->priv->unpack_opts.merge = 1;
419 opt->priv->unpack_opts.head_idx = 2;
420 opt->priv->unpack_opts.fn = threeway_merge;
@@ -423,6 +428,10 @@ static int unpack_trees_start(struct merge_options *opt,
428 init_tree_desc_from_tree(t+2, merge);
429
430 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 + }
435 cache_tree_free(&opt->repo->index->cache_tree);
436
437 /*
t/t1013-read-tree-submodule.sh
-1
@@ -6,7 +6,6 @@ test_description='read-tree can handle submodules'
6 . "$TEST_DIRECTORY"/lib-submodule-update.sh
7
8 KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1
9 -KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1
9
10 test_submodule_switch_recursing_with_args "read-tree -u -m"
11