checkout,merge: disallow overwriting ignored files with --no-overwrite-ignore

Ignored files usually are generated files (e.g. .o files) and can be safely discarded. However sometimes users may have important files in working directory, but still want a clean "git status", so they mark them as ignored files. But in this case, these files should not be overwritten without asking first. Enable this use case with --no-overwrite-ignore, where git only sees tracked and untracked files, no ignored files. Those who mix discardable ignored files with important ones may have to sort it out themselves. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 27, 2011 at 17:15 UTC c1d7036b6bfea586a30c6eac672e58e22e5ceaf5
2 files changed +16 -7
builtin/checkout.c
+8 -3
@@ -34,6 +34,7 @@ struct checkout_opts {
34 int force_detach;
35 int writeout_stage;
36 int writeout_error;
37 + int overwrite_ignore;
38
39 /* not set by parse_options */
40 int branch_exists;
@@ -409,9 +410,11 @@ static int merge_working_tree(struct checkout_opts *opts,
410 topts.gently = opts->merge && old->commit;
411 topts.verbose_update = !opts->quiet;
412 topts.fn = twoway_merge;
412 - topts.dir = xcalloc(1, sizeof(*topts.dir));
413 - topts.dir->flags |= DIR_SHOW_IGNORED;
414 - setup_standard_excludes(topts.dir);
413 + if (opts->overwrite_ignore) {
414 + topts.dir = xcalloc(1, sizeof(*topts.dir));
415 + topts.dir->flags |= DIR_SHOW_IGNORED;
416 + setup_standard_excludes(topts.dir);
417 + }
418 tree = parse_tree_indirect(old->commit ?
419 old->commit->object.sha1 :
420 EMPTY_TREE_SHA1_BIN);
@@ -926,6 +929,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
929 3),
930 OPT__FORCE(&opts.force, "force checkout (throw away local modifications)"),
931 OPT_BOOLEAN('m', "merge", &opts.merge, "perform a 3-way merge with the new branch"),
932 + OPT_BOOLEAN(0, "overwrite-ignore", &opts.overwrite_ignore, "update ignored files (default)"),
933 OPT_STRING(0, "conflict", &conflict_style, "style",
934 "conflict style (merge or diff3)"),
935 OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
@@ -937,6 +941,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
941
942 memset(&opts, 0, sizeof(opts));
943 memset(&new, 0, sizeof(new));
944 + opts.overwrite_ignore = 1;
945
946 gitmodules_config();
947 git_config(git_checkout_config, &opts);
builtin/merge.c
+8 -4
@@ -48,6 +48,7 @@ static int show_diffstat = 1, shortlog_len, squash;
48 static int option_commit = 1, allow_fast_forward = 1;
49 static int fast_forward_only, option_edit;
50 static int allow_trivial = 1, have_message;
51 +static int overwrite_ignore = 1;
52 static struct strbuf merge_msg;
53 static struct commit_list *remoteheads;
54 static struct strategy **use_strategies;
@@ -207,6 +208,7 @@ static struct option builtin_merge_options[] = {
208 OPT_BOOLEAN(0, "abort", &abort_current_merge,
209 "abort the current in-progress merge"),
210 OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1),
211 + OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, "update ignored files (default)"),
212 OPT_END()
213 };
214
@@ -773,10 +775,12 @@ int checkout_fast_forward(const unsigned char *head, const unsigned char *remote
775 memset(&trees, 0, sizeof(trees));
776 memset(&opts, 0, sizeof(opts));
777 memset(&t, 0, sizeof(t));
776 - memset(&dir, 0, sizeof(dir));
777 - dir.flags |= DIR_SHOW_IGNORED;
778 - setup_standard_excludes(&dir);
779 - opts.dir = &dir;
778 + if (overwrite_ignore) {
779 + memset(&dir, 0, sizeof(dir));
780 + dir.flags |= DIR_SHOW_IGNORED;
781 + setup_standard_excludes(&dir);
782 + opts.dir = &dir;
783 + }
784
785 opts.head_idx = 1;
786 opts.src_index = &the_index;