Skip to contentSkip to conversation

stash: honor --no-overwrite-ignore when updating index

PATCH In review Pushkar Singh 6 participants 15 messages Feb 2 – Feb 3

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 stash: honor --no-overwrite-ignore when updating index

The stash code unconditionally cleared opts.preserve_ignored when updating the index, leaving a FIXME suggesting this should depend on an overwrite_ignore flag.

Introduce overwrite_ignore plumbing for git stash push/save and use it to control preserve_ignored during reset_tree(). Add a test to verify that --no-overwrite-ignore preserves ignored files.

This removes the long-standing FIXME and aligns stash behavior with checkout/reset/merge.

---
 builtin/stash.c                    | 11 ++++++++++-
 t/t3905-stash-include-untracked.sh | 13 +++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
builtin/stash.c +10 -1
index 193e3ea47a..82d10520fe 100644
@@ -150,6 +150,7 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 static int use_index;
+static int overwrite_ignore = 1;
 
 /*
  * w_commit is set to the commit containing the working tree
@@ -360,7 +361,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
 	opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
 	opts.update = update;
 	if (update)
-		opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
+		opts.preserve_ignored = !overwrite_ignore;
 	opts.fn = oneway_merge;
 
 	if (unpack_trees(nr_trees, t, &opts))
@@ -1856,6 +1857,10 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+			N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+			N_("do not update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, N_("message"),
 			   N_("stash message")),
 		OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1959,6 +1964,10 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+				N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+				N_("do not update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, "message",
 			   N_("stash message")),
 		OPT_END()
t/t3905-stash-include-untracked.sh +13 -0
index 7704709054..9c5421cd76 100755
@@ -427,4 +427,17 @@ test_expect_success 'stash -u ignores sub-repository' '
 	git stash -u
 '
 
+test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+	echo ignored.txt >>.gitignore &&
+	echo before >ignored.txt &&
+	git add .gitignore &&
+	git commit -m "add ignore" &&
+
+	echo after >ignored.txt &&
+	git stash push --no-overwrite-ignore &&
+
+	test_path_is_file ignored.txt &&
+	grep after ignored.txt
+'
+
 test_done
Signature · 1 line
2.43.0
#1 lore raw
Quoted 37 lines · Pushkar Singh

The stash code unconditionally cleared opts.preserve_ignored when updating the index, leaving a FIXME suggesting this should depend on an overwrite_ignore flag.

Introduce overwrite_ignore plumbing for git stash push/save and use it to control preserve_ignored during reset_tree(). Add a test to verify that --no-overwrite-ignore preserves ignored files.

This removes the long-standing FIXME and aligns stash behavior with checkout/reset/merge.

---
 builtin/stash.c                    | 11 ++++++++++-
 t/t3905-stash-include-untracked.sh | 13 +++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
builtin/stash.c +2 -1
index 193e3ea47a..82d10520fe 100644
@@ -150,6 +150,7 @@ static int show_stat = 1;
 static int show_patch;
 static int show_include_untracked;
 static int use_index;
+static int overwrite_ignore = 1;
 
 /*
  * w_commit is set to the commit containing the working tree
@@ -360,7 +361,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
 	opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
 	opts.update = update;
 	if (update)
-		opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
+		opts.preserve_ignored = !overwrite_ignore;
 	opts.fn = oneway_merge;
 
 	if (unpack_trees(nr_trees, t, &opts))

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1863

Quoted 7 lines
builtin/stash.c +4 -0
@@ -1856,6 +1857,10 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+			N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+			N_("do not update ignored files")),

Adding an OPT_BOOL by default adds a [no] option.

from Documentation/technical/api-parse-options.adoc:

OPT_BOOL(short, long, &int_var, description)::
Introduce a boolean option. int_var is set to one with --option and set to zero with --no-option.

Apart from that, isn't this plain wrong?? The '--overwrite-ignore' and '--no-overwrite-ignore' do the same thing here?

builtin/stash.c static int save_stash(int argc, const char **argv, const char *prefix, · line 1970

Quoted 11 lines
builtin/stash.c +4 -0
 		OPT_STRING('m', "message", &stash_msg, N_("message"),
 			   N_("stash message")),
 		OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1959,6 +1964,10 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+				N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+				N_("do not update ignored files")),

Here too.

Quoted 14 lines
builtin/stash.c +0 -0
 		OPT_STRING('m', "message", &stash_msg, "message",
 			   N_("stash message")),
 		OPT_END()
t/t3905-stash-include-untracked.sh +3 -0
index 7704709054..9c5421cd76 100755
@@ -427,4 +427,17 @@ test_expect_success 'stash -u ignores sub-repository' '
 	git stash -u
 '
 
+test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+	echo ignored.txt >>.gitignore &&
+	echo before >ignored.txt &&

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 442

Quoted 10 lines
t/t3905-stash-include-untracked.sh +10 -0
+	git add .gitignore &&
+	git commit -m "add ignore" &&
+
+	echo after >ignored.txt &&
+	git stash push --no-overwrite-ignore &&
+
+	test_path_is_file ignored.txt &&
+	grep after ignored.txt
+'
+

To confirm, changing the test

modified t/t3905-stash-include-untracked.sh
@@ -434,7 +434,7 @@ test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
git commit -m "add ignore" &&

echo after >ignored.txt &&
- git stash push --no-overwrite-ignore &&
+ git stash push --overwrite-ignore &&

test_path_is_file ignored.txt &&
grep after ignored.txt

still passes the test. We should be testing both scenarios.

Quoted 3 lines
t/t3905-stash-include-untracked.sh +0 -1
 test_done
--

2.43.0

#2 lore raw
Quoted 14 lines · Karthik Nayak

To confirm, changing the test

modified t/t3905-stash-include-untracked.sh

(quoted diff) +1 -2
@@ -434,7 +434,7 @@ test_expect_success 'stash push
--no-overwrite-ignore preserves ignored files' '
        git commit -m "add ignore" &&
 
        echo after >ignored.txt &&
-       git stash push --no-overwrite-ignore &&
+       git stash push --overwrite-ignore &&
 
        test_path_is_file ignored.txt &&
        grep after ignored.txt
Quoted 1 line

still passes the test. We should be testing both scenarios.

Hm. Using "git stash push" (no new flag) on my build of 2.53.0 with only this test added passes, so I agree it seems unlikely to be exercising the intent that led to the FIXME.

Signature · 1 line
D. Ben Knoble
#3 lore raw
In reply to Pushkar Singh
Quoted 7 lines · Pushkar Singh

The stash code unconditionally cleared opts.preserve_ignored when updating the index, leaving a FIXME suggesting this should depend on an overwrite_ignore flag.

Introduce overwrite_ignore plumbing for git stash push/save and use it to control preserve_ignored during reset_tree(). Add a test to verify that --no-overwrite-ignore preserves ignored files.

It's somewhat surprising that this requires so little code changes. Do the mailing list archives yield any justification for why specifically this feature wasn't implemented?

Quoted 2 lines

This removes the long-standing FIXME and aligns stash behavior with checkout/reset/merge.

Missing signoff.

Quoted 5 lines
builtin/stash.c +0 -0
index 193e3ea47a..82d10520fe 100644

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1866

Quoted 10 lines
builtin/stash.c +4 -0
@@ -1856,6 +1857,10 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+			N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+			N_("do not update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, N_("message"),
 			   N_("stash message")),
 		OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),

OPT_BOOL() already handles both the positive and negative case, so there's no need to specify both here. Furthermore, both of your options actually do the exact same thing.

builtin/stash.c static int save_stash(int argc, const char **argv, const char *prefix, · line 1973

Quoted 11 lines
builtin/stash.c +4 -0
@@ -1959,6 +1964,10 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 			 N_("include untracked files in stash")),
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
+		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
+				N_("update ignored files (default)")),
+		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
+				N_("do not update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, "message",
 			   N_("stash message")),
 		OPT_END()

Same here.

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 431

Quoted 10 lines
t/t3905-stash-include-untracked.sh +2 -0
index 7704709054..9c5421cd76 100755
@@ -427,4 +427,17 @@ test_expect_success 'stash -u ignores sub-repository' '
 	git stash -u
 '
 
+test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+	echo ignored.txt >>.gitignore &&

Is there any specific reason why we append instead of overwriting the gitignore file? Overwriting would probably be preferred so that it's easier to reason about the test without requiring context around what the current contents of this file are.

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 440

Quoted 9 lines
t/t3905-stash-include-untracked.sh +9 -0
+	echo before >ignored.txt &&
+	git add .gitignore &&
+	git commit -m "add ignore" &&
+
+	echo after >ignored.txt &&
+	git stash push --no-overwrite-ignore &&
+
+	test_path_is_file ignored.txt &&
+	grep after ignored.txt

I think another good step would be to verify that git stash push --overwrite-ignore _would_ cause us to overwrite the file.

I guess this test only happens to work because the first option takes precedence over the ambiguous second one?

Patrick

#4 lore raw
In reply to Pushkar Singh
Quoted 3 lines · Pushkar Singh

The stash code unconditionally cleared opts.preserve_ignored when updating the index, leaving a FIXME suggesting this should depend on an overwrite_ignore flag.

The commit message should discuss what the code does without the patch in the present tense (SubmittingPathces, “present-tense”).

Quoted 7 lines

Introduce overwrite_ignore plumbing for git stash push/save and use it to control preserve_ignored during reset_tree(). Add a test to verify that --no-overwrite-ignore preserves ignored files.

This removes the long-standing FIXME and aligns stash behavior with checkout/reset/merge.

Missing signoff.

Quoted 5 lines
---
 builtin/stash.c                    | 11 ++++++++++-
 t/t3905-stash-include-untracked.sh | 13 +++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

[snip]

#5 lore raw

PATCH v2 1/1 stash: honor --no-overwrite-ignore with --all

In reply to Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore, and removes the stash FIXME by plumbing overwrite_ignore into unpack_trees().

Add regression tests covering both overwrite and no-overwrite cases.

Changes since v1:
- Use OPT_BOOL correctly for overwrite-ignore.
- Fix stash -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.
- Add regression test for --overwrite-ignore.
- Adjust no-overwrite-ignore test to explicitly use -a.
- Add Signed-off-by.

---
 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 8 deletions(-)
builtin/stash.c +8 -6
index 82d10520fe..c3ee33cce1 100644
@@ -1858,9 +1858,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
 		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-			N_("update ignored files (default)")),
-		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-			N_("do not update ignored files")),
+			 N_("update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, N_("message"),
 			   N_("stash message")),
 		OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1894,6 +1892,9 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
 		       prefix, argv);
 
+	if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+		include_untracked = 1;
+
 	if (pathspec_from_file) {
 		if (patch_mode)
 			die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
@@ -1965,9 +1966,7 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
 		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-				N_("update ignored files (default)")),
-		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-				N_("do not update ignored files")),
+			 N_("update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, "message",
 			   N_("stash message")),
 		OPT_END()
@@ -1994,6 +1993,9 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 			die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
 	}
 
+	if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+		include_untracked = 1;
+
 	ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
 			    patch_mode, &add_p_opt, include_untracked,
 			    only_staged);
t/t3905-stash-include-untracked.sh +14 -2
index 9c5421cd76..a979831a64 100755
@@ -427,17 +427,29 @@ test_expect_success 'stash -u ignores sub-repository' '
 	git stash -u
 '
 
-test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+test_expect_success 'stash push -a --no-overwrite-ignore preserves ignored files' '
 	echo ignored.txt >>.gitignore &&
 	echo before >ignored.txt &&
 	git add .gitignore &&
 	git commit -m "add ignore" &&
 
 	echo after >ignored.txt &&
-	git stash push --no-overwrite-ignore &&
+	git stash push -a --no-overwrite-ignore &&
 
 	test_path_is_file ignored.txt &&
 	grep after ignored.txt
 '
 
+test_expect_success 'stash push -a --overwrite-ignore overwrites ignored files' '
+	echo ignored.txt >>.gitignore &&
+	echo before >ignored.txt &&
+	git add .gitignore &&
+	git commit -m "add ignore" &&
+
+	echo after >ignored.txt &&
+	git stash push -a --overwrite-ignore &&
+
+	! grep after ignored.txt
+'
+
 test_done
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

Quoted 9 lines · Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore, and removes the stash FIXME by plumbing overwrite_ignore into unpack_trees().

Add regression tests covering both overwrite and no-overwrite cases.

Quoted 7 lines

Changes since v1:
- Use OPT_BOOL correctly for overwrite-ignore.
- Fix stash -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.
- Add regression test for --overwrite-ignore.
- Adjust no-overwrite-ignore test to explicitly use -a.
- Add Signed-off-by.

These patch version changes are supposed to go after the --- (after the Signed-off-by). I guess people who are comfortable editing patches write them manually in that place (unless something like b4 or gigitgadget does it for them). I prefer to use --notes and let git-format-patch(1) inject it for me. :)

Quoted 7 lines
---
 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 8 deletions(-)

[snip]

#7 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

Thanks for pointing that out, understood.

I'll move the "Changes since v1" section below the --- in the next revision.

Thanks for the tip.

#8 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

In reply to Pushkar Singh
Quoted 29 lines · Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore, and removes the stash FIXME by plumbing overwrite_ignore into unpack_trees().

Add regression tests covering both overwrite and no-overwrite cases.

Changes since v1:
- Use OPT_BOOL correctly for overwrite-ignore.
- Fix stash -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.
- Add regression test for --overwrite-ignore.
- Adjust no-overwrite-ignore test to explicitly use -a.
- Add Signed-off-by.

---
 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 8 deletions(-)
builtin/stash.c +0 -0
index 82d10520fe..c3ee33cce1 100644

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1864

Quoted 10 lines
builtin/stash.c +1 -3
@@ -1858,9 +1858,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
                OPT_SET_INT('a', "all", &include_untracked,
                            N_("include ignore files"), 2),
                OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-                       N_("update ignored files (default)")),
-               OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-                       N_("do not update ignored files")),
+                        N_("update ignored files")),
                OPT_STRING('m', "message", &stash_msg, N_("message"),
                           N_("stash message")),
                OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),

This doesn't apply on top of, say, the master branch; it looks like you generated this patch on top of the previous version?

Quoted 24 lines
t/t3905-stash-include-untracked.sh +3 -2
index 9c5421cd76..a979831a64 100755
@@ -427,17 +427,29 @@ test_expect_success 'stash -u ignores sub-repository' '
        git stash -u
 '
 
-test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+test_expect_success 'stash push -a --no-overwrite-ignore preserves ignored files' '
        echo ignored.txt >>.gitignore &&
        echo before >ignored.txt &&
        git add .gitignore &&
        git commit -m "add ignore" &&
 
        echo after >ignored.txt &&
-       git stash push --no-overwrite-ignore &&
+       git stash push -a --no-overwrite-ignore &&
 
        test_path_is_file ignored.txt &&
        grep after ignored.txt
 '
 
+test_expect_success 'stash push -a --overwrite-ignore overwrites ignored files' '

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 453

Quoted 10 lines
t/t3905-stash-include-untracked.sh +10 -0
+       echo ignored.txt >>.gitignore &&
+       echo before >ignored.txt &&
+       git add .gitignore &&
+       git commit -m "add ignore" &&
+
+       echo after >ignored.txt &&
+       git stash push -a --overwrite-ignore &&
+
+       ! grep after ignored.txt
+'

After removing --overwrite-ignore from these 2 tests to run them on unmodified Git, the first one fails (good: exercising new feature and showing improvement) and the 2nd one succeeds (probably good:
exercising existing behavior, but strange: see below).

Use test_grep instead of plain grep. For example, it reveals that in the first test, ignored.txt doesn't exist in current Git! (Which is expected with -a, although should be changed by this series).

For the second test, I think we're hitting a similar issue (test_grep complains ignored.txt doesn't exist, so while inverted grep would succeed, we actually want to see that the file doesn't exist, right?). Anyway, the current "! grep …" passes on current Git because it's not really the right test, plus "--overwrite-ignore" is the current behavior of "-a", so a modified version of this test _should_ pass on current Git. I think we want "test_path_is_missing" here?

Signature · 1 line
D. Ben Knoble
#9 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

In reply to Pushkar Singh
Quoted 29 lines · Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore, and removes the stash FIXME by plumbing overwrite_ignore into unpack_trees().

Add regression tests covering both overwrite and no-overwrite cases.

Changes since v1:
- Use OPT_BOOL correctly for overwrite-ignore.
- Fix stash -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.
- Add regression test for --overwrite-ignore.
- Adjust no-overwrite-ignore test to explicitly use -a.
- Add Signed-off-by.

---
 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 8 deletions(-)
builtin/stash.c +0 -0
index 82d10520fe..c3ee33cce1 100644

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1863

Quoted 6 lines
builtin/stash.c +0 -3
@@ -1858,9 +1858,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
                OPT_SET_INT('a', "all", &include_untracked,
                            N_("include ignore files"), 2),
                OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-                       N_("update ignored files (default)")),
-               OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-                       N_("do not update ignored files")),

What's the basis for this patch? I don't see any "overwrite-ignore" anywhere in builtin/stash.c .

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1896

Quoted 10 lines
builtin/stash.c +3 -0
+                        N_("update ignored files")),
                OPT_STRING('m', "message", &stash_msg, N_("message"),
                           N_("stash message")),
                OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1894,6 +1892,9 @@ static int push_stash(int argc, const char **argv, const char *prefix,
        parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
                       prefix, argv);
 
+       if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+               include_untracked = 1;

This suggests that --all and --no-overwrite-ignore are incompatible, yes? Shouldn't they be reported as such rather than having one silently override the other?

Quoted 16 lines
builtin/stash.c +2 -3
+
        if (pathspec_from_file) {
                if (patch_mode)
                        die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
@@ -1965,9 +1966,7 @@ static int save_stash(int argc, const char **argv, const char *prefix,
                OPT_SET_INT('a', "all", &include_untracked,
                            N_("include ignore files"), 2),
                OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-                               N_("update ignored files (default)")),
-               OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-                               N_("do not update ignored files")),
+                        N_("update ignored files")),
                OPT_STRING('m', "message", &stash_msg, "message",
                           N_("stash message")),
                OPT_END()

builtin/stash.c static int save_stash(int argc, const char **argv, const char *prefix, · line 1998

Quoted 6 lines
builtin/stash.c +3 -0
@@ -1994,6 +1993,9 @@ static int save_stash(int argc, const char **argv, const char *prefix,
                        die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
        }
 
+       if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+               include_untracked = 1;
+

Same comments as above.

Also, the commit message claims you are removing a FIXME comment, but no such removal is found in this patch. Is this simply a patch against v1? If so, don't do that; please send a corrected patch

I took a look at v1 as well, and I'll note here that if that FIXME was the only line that needed fixing, I would have just fixed it at the time. I left the FIXME there because I knew it was *one* of the places that would need fixing and didn't have the time or energy (already being a few levels deep in the rabbit hole) to track down all the stash related issues in this area. Perhaps the other sites have since been fixed by someone else, but if so, that should really be documented in the commit message. I'd personally be pretty surprised if the other locations have been fixed; see
https://lore.kernel.org/git/CABPp-BFyR19ch71W10oJDFuRX1OHzQ3si971pMn6dPtHKxJDXQ@mail.gmail.com/
and perhaps the references to stash in
https://lore.kernel.org/git/[email protected]/
; there may also be other issues within stash, those were just the ones I was aware of that looked fishy at the time.

Quoted 11 lines
builtin/stash.c +0 -0
        ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
                            patch_mode, &add_p_opt, include_untracked,
                            only_staged);
t/t3905-stash-include-untracked.sh +0 -0
index 9c5421cd76..a979831a64 100755
@@ -427,17 +427,29 @@ test_expect_success 'stash -u ignores sub-repository' '
        git stash -u
 '
 

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 437

Quoted 10 lines
t/t3905-stash-include-untracked.sh +2 -2
-test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+test_expect_success 'stash push -a --no-overwrite-ignore preserves ignored files' '
        echo ignored.txt >>.gitignore &&
        echo before >ignored.txt &&
        git add .gitignore &&
        git commit -m "add ignore" &&
 
        echo after >ignored.txt &&
-       git stash push --no-overwrite-ignore &&
+       git stash push -a --no-overwrite-ignore &&

Isn't this a non-sensical combination of command line options? --no-overwrite-ignore is explicitly setting include_untracked to 1 while --all sets include_untracked to 2, and I believe those are the _only_ things each of those flags do, which means these ought to be incompatible flags.

Quoted 20 lines
t/t3905-stash-include-untracked.sh +12 -1
        test_path_is_file ignored.txt &&
        grep after ignored.txt
 '
 
+test_expect_success 'stash push -a --overwrite-ignore overwrites ignored files' '
+       echo ignored.txt >>.gitignore &&
+       echo before >ignored.txt &&
+       git add .gitignore &&
+       git commit -m "add ignore" &&
+
+       echo after >ignored.txt &&
+       git stash push -a --overwrite-ignore &&
+
+       ! grep after ignored.txt
+'
+
 test_done
--

2.43.0

#10 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

Hi Elijah,

Thanks for the detailed feedback. Much appreciated.

Quoted 2 lines

What's the basis for this patch? I don't see any "overwrite-ignore" anywhere in builtin/stash.c .

The basis was the existing behavior where git stash push -a would remove ignored files even when --no-overwrite-ignore is provided. The intent was to make stash honor --no-overwrite-ignore consistently with other callers of unpack_trees, limited specifically to the stash -a cleanup path.

In v3 I rebased onto current master and also removed the commit message claim about removing the stash FIXME, since this series only addresses the concrete stash behavior and does not attempt to solve the broader unpack_trees issues.

Quoted 3 lines

This suggests that --all and --no-overwrite-ignore are incompatible, yes? Shouldn't they be reported as such rather than having one silently override the other?

I agree they are philosophically contradictory. I chose to downgrade INCLUDE_ALL_FILES to include-untracked when --no-overwrite-ignore is given so that users explicitly requesting preservation of ignored files are not surprised by their removal.

I am open to changing this to an explicit error instead if that is preferred. I went with downgrading to preserve backwards compatibility and to honor the more conservative option.

Quoted 2 lines

Also, the commit message claims you are removing a FIXME comment, but no such removal is found in this patch.

Yes, that was an error in v2. In v3 the commit message no longer claims to remove the FIXME and is scoped only to fixing stash -a behavior plus adding regression tests.

Regarding tests, v3 now explicitly covers both:

- stash push -a --no-overwrite-ignore preserving ignored files
- stash push -a --overwrite-ignore removing them

using test_grep and test_path_is_missing as suggested.

Thanks also for the references to the broader unpack_trees and stash history. I understand this patch only addresses a small part of a much larger and messier area.

Please let me know if you would prefer the -a plus --no-overwrite-ignore combination to error out instead of downgrading.

Thanks,
Pushkar

#11 lore raw

Re: [PATCH v2] stash: honor --no-overwrite-ignore with --all

Hi Pushkar,

Quoted 10 lines · Pushkar Singh

Hi Elijah,

Thanks for the detailed feedback. Much appreciated.

Quoted 2 lines

What's the basis for this patch? I don't see any "overwrite-ignore" anywhere in builtin/stash.c .

The basis was the existing behavior where git stash push -a would remove ignored files even when --no-overwrite-ignore is provided.

By basis, I meant what commit was it based on. The patch(es) you send need to be applied by others, and if they are based on commits only you have locally, others can't apply or try them and have to guess the details of all your intermediate patches. v2 should be what you would have sent to the list if you had gotten everything right the first time. Same with v3, v4, etc.

You appear to have thought in terms of the purpose of the patch, but your stated purpose doesn't make sense either. There is no --no-overwrite-ignore option, so complaining about how the command behaved when that non-existent option is given doesn't help me understand the purpose of the new option.

Quoted 3 lines

The intent was
to make stash honor --no-overwrite-ignore consistently with other callers of unpack_trees, limited specifically to the stash -a cleanup path.

Oh, so the point of the patch is an attempt to make command line options more consistent along some axis? If so, I think you picked a place where it doesn't actually make sense. We need to back up and figure out what the user-side desired behavior is and what they cannot achieve today, or what is confusing today, and find ways to improve that and implement it. Starting from the low-level details can work, but only if at the end we can explain to users why our changes make sense.

Quoted 5 lines

In v3 I rebased onto current master and also removed the commit message claim about removing the stash FIXME, since this series only addresses the concrete stash behavior and does not attempt to solve the broader unpack_trees issues.

Quoted 8 lines
Quoted 3 lines

This suggests that --all and --no-overwrite-ignore are incompatible, yes? Shouldn't they be reported as such rather than having one silently override the other?

I agree they are philosophically contradictory. I chose to downgrade INCLUDE_ALL_FILES to include-untracked when --no-overwrite-ignore is given so that users explicitly requesting preservation of ignored files are not surprised by their removal.

You don't want people who pass "--no-overwrite-ignore" (a new option) to be surprised when ignores are overwritten, but don't care about people who pass "-a" ("--all") getting surprised that all files aren't included in the stash? I don't quite understand the logic.

Quoted 3 lines

I am open to changing this to an explicit error instead if that is preferred. I went with downgrading to preserve backwards compatibility and to honor the more conservative option.

You added a new option and only changed behavior relative to when that new option is invoked, so I don't understand the claim about preserving backwards compatibility; how can backwards compatibility even be relevant in this situation?

I'm not sure I understand the "honor the more conservative option" either. Is that a cyclical argument (you're introducing a new option and deciding to honor it in order to honor it), or am I misunderstanding?

#12 lore raw

PATCH v3 1/1 stash: honor --no-overwrite-ignore with --all

In reply to Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore.

Add regression tests covering both overwrite and no-overwrite cases.

---
Changes since v2:
- Use test_grep instead of grep
- Use test_path_is_missing for overwrite-ignore test
- Rebase onto current master so patch applies cleanly

 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 18 +++++++++++++++---
 2 files changed, 23 insertions(+), 9 deletions(-)
builtin/stash.c +8 -6
index 82d10520fe..c3ee33cce1 100644
@@ -1858,9 +1858,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
 		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-			N_("update ignored files (default)")),
-		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-			N_("do not update ignored files")),
+			 N_("update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, N_("message"),
 			   N_("stash message")),
 		OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1894,6 +1892,9 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 	parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
 		       prefix, argv);
 
+	if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+		include_untracked = 1;
+
 	if (pathspec_from_file) {
 		if (patch_mode)
 			die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
@@ -1965,9 +1966,7 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 		OPT_SET_INT('a', "all", &include_untracked,
 			    N_("include ignore files"), 2),
 		OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-				N_("update ignored files (default)")),
-		OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-				N_("do not update ignored files")),
+			 N_("update ignored files")),
 		OPT_STRING('m', "message", &stash_msg, "message",
 			   N_("stash message")),
 		OPT_END()
@@ -1994,6 +1993,9 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 			die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
 	}
 
+	if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+		include_untracked = 1;
+
 	ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
 			    patch_mode, &add_p_opt, include_untracked,
 			    only_staged);
t/t3905-stash-include-untracked.sh +15 -3
index 9c5421cd76..63b59de47b 100755
@@ -427,17 +427,29 @@ test_expect_success 'stash -u ignores sub-repository' '
 	git stash -u
 '
 
-test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+test_expect_success 'stash push -a --no-overwrite-ignore preserves ignored files' '
 	echo ignored.txt >>.gitignore &&
 	echo before >ignored.txt &&
 	git add .gitignore &&
 	git commit -m "add ignore" &&
 
 	echo after >ignored.txt &&
-	git stash push --no-overwrite-ignore &&
+	git stash push -a --no-overwrite-ignore &&
 
 	test_path_is_file ignored.txt &&
-	grep after ignored.txt
+	test_grep after ignored.txt
+'
+
+test_expect_success 'stash push -a --overwrite-ignore overwrites ignored files' '
+	echo ignored.txt >>.gitignore &&
+	echo before >ignored.txt &&
+	git add .gitignore &&
+	git commit -m "add ignore" &&
+
+	echo after >ignored.txt &&
+	git stash push -a --overwrite-ignore &&
+
+	test_path_is_missing ignored.txt
 '
 
 test_done
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v3] stash: honor --no-overwrite-ignore with --all

Quoted 3 lines · Pushkar Singh

Teach stash push/save to avoid -a cleanup when --no-overwrite-ignore is given by downgrading INCLUDE_ALL_FILES to include-untracked.

This feels like you're regurgitating the patch with low-enough level of details ("-a cleanup", INCLUDE_ALL_FILES, include-untracked) that it'll only be intelligible to someone who has builtin/stash.c code fresh on their mind. It doesn't explain the high-level purpose behind your patch, and, in fact, will likely lead readers to try to read the patch in order to understand the commit message, when usually we hope for the opposite.

Quoted 2 lines

This fixes ignored files being incorrectly removed despite --no-overwrite-ignore.

This claim makes no sense; --no-overwrite-ignore doesn't exist in git yet, and this is the first (and only) patch in your series, so at best you're claiming to fix something you introduced? Very confusing.

Quoted 8 lines

Add regression tests covering both overwrite and no-overwrite cases.

---
Changes since v2:
- Use test_grep instead of grep
- Use test_path_is_missing for overwrite-ignore test
- Rebase onto current master so patch applies cleanly

Great...but you still seem to be submitting a patch that is based on your previous (rebased?) patches, without submitting the previous patches, leaving us to guess how you got to your current state, as noted below. You should have been editing your previous patch and then submitting the edited patch. After v2, you should have squashed and resent.

Quoted 9 lines
 builtin/stash.c                    | 14 ++++++++------
 t/t3905-stash-include-untracked.sh | 18 +++++++++++++++---
 2 files changed, 23 insertions(+), 9 deletions(-)
builtin/stash.c +0 -0
index 82d10520fe..c3ee33cce1 100644

builtin/stash.c static int push_stash(int argc, const char **argv, const char *prefix, · line 1861

Quoted 7 lines
builtin/stash.c +1 -3
@@ -1858,9 +1858,7 @@ static int push_stash(int argc, const char **argv, const char *prefix,
                OPT_SET_INT('a', "all", &include_untracked,
                            N_("include ignore files"), 2),
                OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-                       N_("update ignored files (default)")),
-               OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-                       N_("do not update ignored files")),
+                        N_("update ignored files")),

And here's where it's clear that this patch was broken in the same way as v2: "no-overwrite-ignore" has never appeared in any version of builtin/stash.c upstream (same with "overwrite-ignore"), so this patch is clearly against some local state you have. The base of your series (or the base of your patch, since you only have one patch in this series) needs to be an upstream commit, not some other commit that only you have access to. Might I interest you in using gitgitgadget, which would make it easier to submit patches?

Quoted 43 lines
builtin/stash.c +7 -3
                OPT_STRING('m', "message", &stash_msg, N_("message"),
                           N_("stash message")),
                OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
@@ -1894,6 +1892,9 @@ static int push_stash(int argc, const char **argv, const char *prefix,
        parse_pathspec(&ps, 0, PATHSPEC_PREFER_FULL | PATHSPEC_PREFIX_ORIGIN,
                       prefix, argv);
 
+       if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+               include_untracked = 1;
+
        if (pathspec_from_file) {
                if (patch_mode)
                        die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
@@ -1965,9 +1966,7 @@ static int save_stash(int argc, const char **argv, const char *prefix,
                OPT_SET_INT('a', "all", &include_untracked,
                            N_("include ignore files"), 2),
                OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore,
-                               N_("update ignored files (default)")),
-               OPT_BOOL(0, "no-overwrite-ignore", &overwrite_ignore,
-                               N_("do not update ignored files")),
+                        N_("update ignored files")),
                OPT_STRING('m', "message", &stash_msg, "message",
                           N_("stash message")),
                OPT_END()
@@ -1994,6 +1993,9 @@ static int save_stash(int argc, const char **argv, const char *prefix,
                        die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
        }
 
+       if (!overwrite_ignore && include_untracked == INCLUDE_ALL_FILES)
+               include_untracked = 1;
+
 
        ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
                            patch_mode, &add_p_opt, include_untracked,
                            only_staged);
t/t3905-stash-include-untracked.sh +0 -0
index 9c5421cd76..63b59de47b 100755
@@ -427,17 +427,29 @@ test_expect_success 'stash -u ignores sub-repository' '
        git stash -u
 '
 

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 437

Quoted 10 lines
t/t3905-stash-include-untracked.sh +2 -2
-test_expect_success 'stash push --no-overwrite-ignore preserves ignored files' '
+test_expect_success 'stash push -a --no-overwrite-ignore preserves ignored files' '
        echo ignored.txt >>.gitignore &&
        echo before >ignored.txt &&
        git add .gitignore &&
        git commit -m "add ignore" &&
 
        echo after >ignored.txt &&
-       git stash push --no-overwrite-ignore &&
+       git stash push -a --no-overwrite-ignore &&

Not only is the patch broken ("no-overwrite-ignore" has never appeared in any version of git; so this patch is clearly against your local state), but the command line makes no sense:
-a : stash ignored files too
--no-overwrite-ignore: wait, we don't want to mess with ignored
files, so nevermind, don't stash them

Why wouldn't the user just leave off "-a" if they don't want them stashed?

t/t3905-stash-include-untracked.sh test_expect_success 'stash -u ignores sub-repository' ' · line 450

Quoted 13 lines
t/t3905-stash-include-untracked.sh +11 -1
        test_path_is_file ignored.txt &&
-       grep after ignored.txt
+       test_grep after ignored.txt
+'
+
+test_expect_success 'stash push -a --overwrite-ignore overwrites ignored files' '
+       echo ignored.txt >>.gitignore &&
+       echo before >ignored.txt &&
+       git add .gitignore &&
+       git commit -m "add ignore" &&
+
+       echo after >ignored.txt &&
+       git stash push -a --overwrite-ignore &&

And this command line makes no sense either:
-a: stash ignored files too
--overwrite-ignore: yes, I'm explicitly giving you permission to pay
attention to the fact that I already passed you the "-a" parameter. Please do what that other parameter says.

Why would the user need an extra flag instead of just using "-a"?

Additionally, if there is some user problem you're trying to solve here, then these tests look rather incomplete; they only test the push side and not the pop side. What if someone runs "git stash push -a" followed by "git stash pop --no-overwrite-ignore"? Or is that flag not going to be added to pop? Do we only care about protecting ignored files at push/save time and not at pop time? Why? (And if we do care about pop time, won't we need to worry about both former untracked and former ignored files both having the possibility of overwriting files that are now ignored? And if we do allow users to not overwrite ignored files at pop time, do we have a similar special flag to avoid overwriting untracked files at pop time? If not, are ignored files thus more important or special than untracked files?)

I don't understand the user-driven problem this patch is attempting to solve.

#14 lore raw

Re: [PATCH v3] stash: honor --no-overwrite-ignore with --all

Hi Elijah,

Thank you for taking the time to explain this so clearly.

You are absolutely right. I misunderstood what you meant by “basis” and also approached this patch from the implementation side instead of starting from a concrete user problem.

I also realize now that I incorrectly stacked this on top of my local changes instead of rebasing and editing the previous version, which made the patch impossible to apply upstream. Sorry about that.

Given your feedback, I agree that I need to step back and rethink this from a user perspective (what real workflow is broken today, how -a should behave, and whether any new flags even make sense here), rather than trying to force consistency at a low level.

I will drop this series for now, spend time understanding stash behavior and the broader context you pointed out, and only resend if I can clearly articulate a user-driven problem with a clean patch based directly on upstream.

Thanks again for your patience and guidance.

Pushkar