Skip to content

Commit c1d7036

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f44054c commit c1d7036

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

builtin/checkout.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct checkout_opts {
3434
int force_detach;
3535
int writeout_stage;
3636
int writeout_error;
37+
int overwrite_ignore;
3738

3839
/* not set by parse_options */
3940
int branch_exists;
@@ -409,9 +410,11 @@ static int merge_working_tree(struct checkout_opts *opts,
409410
topts.gently = opts->merge && old->commit;
410411
topts.verbose_update = !opts->quiet;
411412
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+
}
415418
tree = parse_tree_indirect(old->commit ?
416419
old->commit->object.sha1 :
417420
EMPTY_TREE_SHA1_BIN);
@@ -926,6 +929,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
926929
3),
927930
OPT__FORCE(&opts.force, "force checkout (throw away local modifications)"),
928931
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)"),
929933
OPT_STRING(0, "conflict", &conflict_style, "style",
930934
"conflict style (merge or diff3)"),
931935
OPT_BOOLEAN('p', "patch", &patch_mode, "select hunks interactively"),
@@ -937,6 +941,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
937941

938942
memset(&opts, 0, sizeof(opts));
939943
memset(&new, 0, sizeof(new));
944+
opts.overwrite_ignore = 1;
940945

941946
gitmodules_config();
942947
git_config(git_checkout_config, &opts);

builtin/merge.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static int show_diffstat = 1, shortlog_len, squash;
4848
static int option_commit = 1, allow_fast_forward = 1;
4949
static int fast_forward_only, option_edit;
5050
static int allow_trivial = 1, have_message;
51+
static int overwrite_ignore = 1;
5152
static struct strbuf merge_msg;
5253
static struct commit_list *remoteheads;
5354
static struct strategy **use_strategies;
@@ -207,6 +208,7 @@ static struct option builtin_merge_options[] = {
207208
OPT_BOOLEAN(0, "abort", &abort_current_merge,
208209
"abort the current in-progress merge"),
209210
OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1),
211+
OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, "update ignored files (default)"),
210212
OPT_END()
211213
};
212214

@@ -773,10 +775,12 @@ int checkout_fast_forward(const unsigned char *head, const unsigned char *remote
773775
memset(&trees, 0, sizeof(trees));
774776
memset(&opts, 0, sizeof(opts));
775777
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+
}
780784

781785
opts.head_idx = 1;
782786
opts.src_index = &the_index;

0 commit comments

Comments
 (0)