threads / rfc / 51638

RFC patchunpack-trees.c: handle empty deleted ita files

Subject: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

## tl;dr

12 messages between Aug 13, 2019 and Feb 14, 2020. Diffs are folded; open one to read it.

replies: 11people: 3as markdown or json

Varun Naik· Aug 13, 2019, 16:03 UTC · lore

It is possible to delete a committed file from the index and then add it as intent-to-add. Several variations of "reset" and "checkout" should resurrect the file in the index from HEAD. "merge", "cherry-pick", and "revert" should all fail with an error message. This patch provides the desired behavior even when the file is empty in HEAD.

The affected commands all compare two cache entries by calling unpack-trees.c:same(). A cache entry for an ita file and a cache entry for an empty file have the same oid. So, the cache entry for an empty deleted ita file was previously considered the "same" as the cache entry for an empty file. This fix adds a comparison of the intent-to-add bits so that the two cache entries are no longer considered the "same".

Signed-off-by: Varun Naik <vcnaik94@gmail.com>
---
I am marking this patch as RFC because it is changing code deep in
unpack-trees.c, and I'm sure it will generate some controversy :)

The affected "reset" and "checkout" commands call unpack-trees.c:oneway_merge(), which calls same(). The affected "merge", "cherry-pick", and "revert" commands call unpack-trees.c:threeway_merge(), which calls same(). "stash" also calls oneway_merge(), and "rebase" also calls threeway_merge(), but they are not included in the test cases because their behaviors have not changed.

The new tests are not comprehensive. In particular, they don't call plumbing commands, such as "read-tree". But hopefully they provide enough coverage to prevent most regressions.

The new test cases for "cherry-pick" and "revert" grep for the single word "overwritten" rather than a more precise error message because the error message for an empty deleted ita file changes slightly if the patch in [0] is also applied. In retrospect, the commands affected by [0], [1], and this patch were all intertwined, and it would have been better to create a single large patch instead of three smaller patches.

[0]: https://public-inbox.org/git/20190801161558.12838-1-vcnaik94@gmail.com/ [1]: https://public-inbox.org/git/20190802162852.14498-1-vcnaik94@gmail.com/

 t/t3030-merge-recursive.sh    | 25 +++++++++++++++---
 t/t3501-revert-cherry-pick.sh | 49 ++++++++++++++++++++++++++++++++++-
 t/t7104-reset-hard.sh         | 11 ++++++++
 t/t7110-reset-merge.sh        | 31 ++++++++++++++++++++++
 t/t7201-co.sh                 | 12 +++++++++
 unpack-trees.c                |  1 +
 6 files changed, 125 insertions(+), 4 deletions(-)
Show changes to 6 files +125 −4

t/t3030-merge-recursive.sh, t/t3501-revert-cherry-pick.sh, t/t7104-reset-hard.sh, t/t7110-reset-merge.sh, t/t7201-co.sh, unpack-trees.c

diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
index ff641b348a..8aebb829a6 100755
--- a/t/t3030-merge-recursive.sh
+++ b/t/t3030-merge-recursive.sh
@@ -303,13 +303,32 @@ test_expect_success 'fail if the index has unresolved entries' '
 	git checkout -f "$c1" &&
 
 	test_must_fail git merge "$c5" &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "not possible because you have unmerged files" out &&
 	git add -u &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "You have not concluded your merge" out &&
 	rm -f .git/MERGE_HEAD &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
+test_expect_success 'fail if a deleted intent-to-add file exists in the index' '
+	git checkout -f "$c1" &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git checkout -f "$c1" &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
 '
 
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index d1c68af8c5..45d816fc0c 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -91,16 +91,63 @@ test_expect_success 'cherry-pick on stat-dirty working tree' '
 	)
 '
 
-test_expect_success 'revert forbidden on dirty working tree' '
+test_expect_success 'cherry-pick forbidden on dirty working tree' '
 
+	git checkout -b temp &&
 	echo content >extra_file &&
 	git add extra_file &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors
+
+'
+
+test_expect_success 'revert forbidden on dirty working tree' '
+
 	test_must_fail git revert HEAD 2>errors &&
 	test_i18ngrep "your local changes would be overwritten by " errors
 
 '
 
+test_expect_success 'cherry-pick fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
+test_expect_success 'revert fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
 test_expect_success 'cherry-pick on unborn branch' '
+	git checkout -f rename1 &&
 	git checkout --orphan unborn &&
 	git rm --cached -r . &&
 	rm -rf * &&
diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
index 16faa07813..96a0b779e7 100755
--- a/t/t7104-reset-hard.sh
+++ b/t/t7104-reset-hard.sh
@@ -43,4 +43,15 @@ test_expect_success 'reset --hard did not corrupt index or cached-tree' '
 
 '
 
+test_expect_success 'reset --hard adds deleted intent-to-add file back to index' '
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git reset --hard &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
index a82a07a04a..3346759375 100755
--- a/t/t7110-reset-merge.sh
+++ b/t/t7110-reset-merge.sh
@@ -292,4 +292,35 @@ test_expect_success '--keep fails with added/deleted merge' '
     test_i18ngrep "middle of a merge" err.log
 '
 
+test_expect_success 'reset --merge adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    git add nonempty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached nonempty &&
+    git add -N nonempty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep nonempty err.log | grep "not uptodate" &&
+    git reset --hard initial &&
+    >empty &&
+    git add empty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached empty &&
+    git add -N empty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep empty err.log | grep "not uptodate"
+'
+
+test_expect_success 'reset --keep adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    >empty &&
+    git add nonempty empty &&
+    git commit -m "create files to be deleted" &&
+    git rm --cached nonempty empty &&
+    git add -N nonempty empty &&
+    git reset --keep HEAD &&
+    git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 5990299fc9..4c0c33ce33 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -674,4 +674,16 @@ test_expect_success 'custom merge driver with checkout -m' '
 	test_cmp expect arm
 '
 
+test_expect_success 'checkout -f HEAD adds deleted intent-to-add file back to index' '
+	git reset --hard master &&
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git checkout -f HEAD &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index 50189909b8..9b7e6b01c4 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1661,6 +1661,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
 	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
 		return 0;
 	return a->ce_mode == b->ce_mode &&
+	       !ce_intent_to_add(a) == !ce_intent_to_add(b) &&
 	       oideq(&a->oid, &b->oid);
 }
 
-- 
2.22.0
René Scharfe· Aug 13, 2019, 18:08 UTC · re: Varun Naik · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

Am 13.08.19 um 18:03 schrieb Varun Naik:
Show 17 quoted lines
> It is possible to delete a committed file from the index and then add it
> as intent-to-add. Several variations of "reset" and "checkout" should
> resurrect the file in the index from HEAD. "merge", "cherry-pick", and
> "revert" should all fail with an error message. This patch provides the
> desired behavior even when the file is empty in HEAD.
>
> The affected commands all compare two cache entries by calling
> unpack-trees.c:same(). A cache entry for an ita file and a cache entry
> for an empty file have the same oid. So, the cache entry for an empty
> deleted ita file was previously considered the "same" as the cache entry
> for an empty file. This fix adds a comparison of the intent-to-add bits
> so that the two cache entries are no longer considered the "same".
>
> Signed-off-by: Varun Naik <vcnaik94@gmail.com>
> ---
> I am marking this patch as RFC because it is changing code deep in
> unpack-trees.c, and I'm sure it will generate some controversy :)

Lacking experience with intent-to-add I don't see why this would be controversial. Copying Duy and quoting in full, as he might have more to say on that topic.

I have just one silly question below.
Show 229 quoted lines
>
> The affected "reset" and "checkout" commands call
> unpack-trees.c:oneway_merge(), which calls same(). The affected "merge",
> "cherry-pick", and "revert" commands call
> unpack-trees.c:threeway_merge(), which calls same(). "stash" also calls
> oneway_merge(), and "rebase" also calls threeway_merge(), but they are
> not included in the test cases because their behaviors have not changed.
>
> The new tests are not comprehensive. In particular, they don't call
> plumbing commands, such as "read-tree". But hopefully they provide
> enough coverage to prevent most regressions.
>
> The new test cases for "cherry-pick" and "revert" grep for the single
> word "overwritten" rather than a more precise error message because the
> error message for an empty deleted ita file changes slightly if the
> patch in [0] is also applied. In retrospect, the commands affected by
> [0], [1], and this patch were all intertwined, and it would have been
> better to create a single large patch instead of three smaller patches.
>
> [0]: https://public-inbox.org/git/20190801161558.12838-1-vcnaik94@gmail.com/
> [1]: https://public-inbox.org/git/20190802162852.14498-1-vcnaik94@gmail.com/
>
>  t/t3030-merge-recursive.sh    | 25 +++++++++++++++---
>  t/t3501-revert-cherry-pick.sh | 49 ++++++++++++++++++++++++++++++++++-
>  t/t7104-reset-hard.sh         | 11 ++++++++
>  t/t7110-reset-merge.sh        | 31 ++++++++++++++++++++++
>  t/t7201-co.sh                 | 12 +++++++++
>  unpack-trees.c                |  1 +
>  6 files changed, 125 insertions(+), 4 deletions(-)
>
> diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
> index ff641b348a..8aebb829a6 100755
> --- a/t/t3030-merge-recursive.sh
> +++ b/t/t3030-merge-recursive.sh
> @@ -303,13 +303,32 @@ test_expect_success 'fail if the index has unresolved entries' '
>  	git checkout -f "$c1" &&
>
>  	test_must_fail git merge "$c5" &&
> -	test_must_fail git merge "$c5" 2> out &&
> +	test_must_fail git merge "$c5" 2>out &&
>  	test_i18ngrep "not possible because you have unmerged files" out &&
>  	git add -u &&
> -	test_must_fail git merge "$c5" 2> out &&
> +	test_must_fail git merge "$c5" 2>out &&
>  	test_i18ngrep "You have not concluded your merge" out &&
>  	rm -f .git/MERGE_HEAD &&
> -	test_must_fail git merge "$c5" 2> out &&
> +	test_must_fail git merge "$c5" 2>out &&
> +	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
> +'
> +
> +test_expect_success 'fail if a deleted intent-to-add file exists in the index' '
> +	git checkout -f "$c1" &&
> +	echo "nonempty" >nonempty &&
> +	git add nonempty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached nonempty &&
> +	git add -N nonempty &&
> +	test_must_fail git merge "$c5" 2>out &&
> +	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
> +	git checkout -f "$c1" &&
> +	>empty &&
> +	git add empty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached empty &&
> +	git add -N empty &&
> +	test_must_fail git merge "$c5" 2>out &&
>  	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
>  '
>
> diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
> index d1c68af8c5..45d816fc0c 100755
> --- a/t/t3501-revert-cherry-pick.sh
> +++ b/t/t3501-revert-cherry-pick.sh
> @@ -91,16 +91,63 @@ test_expect_success 'cherry-pick on stat-dirty working tree' '
>  	)
>  '
>
> -test_expect_success 'revert forbidden on dirty working tree' '
> +test_expect_success 'cherry-pick forbidden on dirty working tree' '
>
> +	git checkout -b temp &&
>  	echo content >extra_file &&
>  	git add extra_file &&
> +	test_must_fail git cherry-pick rename2 2>errors &&
> +	test_i18ngrep "your local changes would be overwritten by " errors
> +
> +'
> +
> +test_expect_success 'revert forbidden on dirty working tree' '
> +
>  	test_must_fail git revert HEAD 2>errors &&
>  	test_i18ngrep "your local changes would be overwritten by " errors
>
>  '
>
> +test_expect_success 'cherry-pick fails if a deleted intent-to-add file exists in the index' '
> +	git reset --hard rename1 &&
> +	echo "nonempty" >nonempty &&
> +	git add nonempty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached nonempty &&
> +	git add -N nonempty &&
> +	test_must_fail git cherry-pick rename2 2>errors &&
> +	test_i18ngrep "overwritten" errors &&
> +	git reset --hard rename1 &&
> +	>empty &&
> +	git add empty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached empty &&
> +	git add -N empty &&
> +	test_must_fail git cherry-pick rename2 2>errors &&
> +	test_i18ngrep "overwritten" errors
> +'
> +
> +test_expect_success 'revert fails if a deleted intent-to-add file exists in the index' '
> +	git reset --hard rename1 &&
> +	echo "nonempty" >nonempty &&
> +	git add nonempty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached nonempty &&
> +	git add -N nonempty &&
> +	test_must_fail git revert rename2 2>errors &&
> +	test_i18ngrep "overwritten" errors &&
> +	git reset --hard rename1 &&
> +	>empty &&
> +	git add empty &&
> +	git commit -m "create file to be deleted" &&
> +	git rm --cached empty &&
> +	git add -N empty &&
> +	test_must_fail git revert rename2 2>errors &&
> +	test_i18ngrep "overwritten" errors
> +'
> +
>  test_expect_success 'cherry-pick on unborn branch' '
> +	git checkout -f rename1 &&
>  	git checkout --orphan unborn &&
>  	git rm --cached -r . &&
>  	rm -rf * &&
> diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
> index 16faa07813..96a0b779e7 100755
> --- a/t/t7104-reset-hard.sh
> +++ b/t/t7104-reset-hard.sh
> @@ -43,4 +43,15 @@ test_expect_success 'reset --hard did not corrupt index or cached-tree' '
>
>  '
>
> +test_expect_success 'reset --hard adds deleted intent-to-add file back to index' '
> +	echo "nonempty" >nonempty &&
> +	>empty &&
> +	git add nonempty empty &&
> +	git commit -m "create files to be deleted" &&
> +	git rm --cached nonempty empty &&
> +	git add -N nonempty empty &&
> +	git reset --hard &&
> +	git diff --cached --exit-code nonempty empty
> +'
> +
>  test_done
> diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
> index a82a07a04a..3346759375 100755
> --- a/t/t7110-reset-merge.sh
> +++ b/t/t7110-reset-merge.sh
> @@ -292,4 +292,35 @@ test_expect_success '--keep fails with added/deleted merge' '
>      test_i18ngrep "middle of a merge" err.log
>  '
>
> +test_expect_success 'reset --merge adds deleted intent-to-add file back to index' '
> +    git reset --hard initial &&
> +    echo "nonempty" >nonempty &&
> +    git add nonempty &&
> +    git commit -m "create file to be deleted" &&
> +    git rm --cached nonempty &&
> +    git add -N nonempty &&
> +    test_must_fail git reset --merge HEAD 2>err.log &&
> +    grep nonempty err.log | grep "not uptodate" &&
> +    git reset --hard initial &&
> +    >empty &&
> +    git add empty &&
> +    git commit -m "create file to be deleted" &&
> +    git rm --cached empty &&
> +    git add -N empty &&
> +    test_must_fail git reset --merge HEAD 2>err.log &&
> +    grep empty err.log | grep "not uptodate"
> +'
> +
> +test_expect_success 'reset --keep adds deleted intent-to-add file back to index' '
> +    git reset --hard initial &&
> +    echo "nonempty" >nonempty &&
> +    >empty &&
> +    git add nonempty empty &&
> +    git commit -m "create files to be deleted" &&
> +    git rm --cached nonempty empty &&
> +    git add -N nonempty empty &&
> +    git reset --keep HEAD &&
> +    git diff --cached --exit-code nonempty empty
> +'
> +
>  test_done
> diff --git a/t/t7201-co.sh b/t/t7201-co.sh
> index 5990299fc9..4c0c33ce33 100755
> --- a/t/t7201-co.sh
> +++ b/t/t7201-co.sh
> @@ -674,4 +674,16 @@ test_expect_success 'custom merge driver with checkout -m' '
>  	test_cmp expect arm
>  '
>
> +test_expect_success 'checkout -f HEAD adds deleted intent-to-add file back to index' '
> +	git reset --hard master &&
> +	echo "nonempty" >nonempty &&
> +	>empty &&
> +	git add nonempty empty &&
> +	git commit -m "create files to be deleted" &&
> +	git rm --cached nonempty empty &&
> +	git add -N nonempty empty &&
> +	git checkout -f HEAD &&
> +	git diff --cached --exit-code nonempty empty
> +'
> +
>  test_done
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 50189909b8..9b7e6b01c4 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -1661,6 +1661,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
>  	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
>  		return 0;
>  	return a->ce_mode == b->ce_mode &&
> +	       !ce_intent_to_add(a) == !ce_intent_to_add(b) &&

Why the bangs? This would work just as well and be slightly easier to read without negating both sides, wouldn't it?

>  	       oideq(&a->oid, &b->oid);
>  }
>
>
Junio C Hamano· Aug 13, 2019, 20:32 UTC · re: René Scharfe · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

René Scharfe <l.s.r@web.de> writes:
Show 21 quoted lines
> Am 13.08.19 um 18:03 schrieb Varun Naik:
>> It is possible to delete a committed file from the index and then add it
>> as intent-to-add. Several variations of "reset" and "checkout" should
>> resurrect the file in the index from HEAD. "merge", "cherry-pick", and
>> "revert" should all fail with an error message. This patch provides the
>> desired behavior even when the file is empty in HEAD.
>>
>> The affected commands all compare two cache entries by calling
>> unpack-trees.c:same(). A cache entry for an ita file and a cache entry
>> for an empty file have the same oid. So, the cache entry for an empty
>> deleted ita file was previously considered the "same" as the cache entry
>> for an empty file. This fix adds a comparison of the intent-to-add bits
>> so that the two cache entries are no longer considered the "same".
>>
>> Signed-off-by: Varun Naik <vcnaik94@gmail.com>
>> ---
>> I am marking this patch as RFC because it is changing code deep in
>> unpack-trees.c, and I'm sure it will generate some controversy :)
>
> Lacking experience with intent-to-add I don't see why this would be
> controversial.

The "same()" function here is used to compare two cache entries (either came from the in-core index, or fabricated out of one of the tree objects as a potential merge result to replace the one in the index), and is expected to tell if they are the same or the different, which is used for example in a logic like "if the one in the index and the one came from HEAD are not the same, then it is not safe to continue the merge", "if the one came from HEAD and the one from the other branch are the same, just keep the one from the index (which may or may not be the same as HEAD)".

The original code considered that two entries with the same mode and the same "contents" are the same. As nobody sane tracks an empty file for an extended span of history, that meant that most of the time, intent-to-add entries, which has the normal mode bits for the blobs (with or without the executable bit) and object name for a zero length blob, would have been judged "different".

This change extends the logic to the case in which the contents recorded in the tree-ishes is also a zero length blob. We used to say the I-T-A entry was the same as the recorded blob, but whether the recorded one in the tree-ish is 0-length or 1-byte blob, this patch does not want it to be declared the "same" as any I-T-A entry.

So in that sense, it makes the behaviour for I-T-A entries consistent. But it is a separate matter if the consistency is good; we do not want our code to be consistently wrong ;-)

Show 12 quoted lines
>> diff --git a/unpack-trees.c b/unpack-trees.c
>> index 50189909b8..9b7e6b01c4 100644
>> --- a/unpack-trees.c
>> +++ b/unpack-trees.c
>> @@ -1661,6 +1661,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
>>  	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
>>  		return 0;
>>  	return a->ce_mode == b->ce_mode &&
>> +	       !ce_intent_to_add(a) == !ce_intent_to_add(b) &&
>
> Why the bangs?  This would work just as well and be slightly easier to
> read without negating both sides, wouldn't it?

Looking at the implementation of that macro, I agree. If it were "returns non-zero for true, zero for false, we do not guarantee that we return the same non-zero value for true all the time", then these bangs do make sense, but that is not the case here.

But more importantly, can both sides of the comparison be I-T-A entries?

I offhand do not think such a situation can arise (a cache entry with I-T-A bit on can only come from the in-core index, IIUC, and never from a tree-ish in this codepath), but if we encouter such a case, I would imagine that we do not want to treat an I-T-A entry to be the same with anything else, including another I-T-A entry.

So perhaps 
+	!ce_intent_to_add(a) && !ce_intent_to_add(b) &&

i.e. "a cache entry is eligible to be same with something else only when its I-T-A bit is unset".

I dunno.
>>  	       oideq(&a->oid, &b->oid);
>>  }
>>
>>
René Scharfe· Aug 14, 2019, 04:30 UTC · re: Junio C Hamano · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

Am 13.08.19 um 22:32 schrieb Junio C Hamano:
Show 6 quoted lines
> So perhaps
>
> +	!ce_intent_to_add(a) && !ce_intent_to_add(b) &&
>
> i.e. "a cache entry is eligible to be same with something else only
> when its I-T-A bit is unset".
FWIW, here's another way to express that:
Show changes to unpack-trees.c +1 −1
diff --git a/unpack-trees.c b/unpack-trees.c
index 62276d4fef..a62d67d131 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1658,7 +1658,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
 		return 0;
 	if (!a && !b)
 		return 1;
-	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
+	if ((a->ce_flags | b->ce_flags) & (CE_CONFLICTED | CE_INTENT_TO_ADD))
 		return 0;
 	return a->ce_mode == b->ce_mode &&
 	       oideq(&a->oid, &b->oid);
Varun Naik· Aug 15, 2019, 16:17 UTC · re: Junio C Hamano · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

On Tue, Aug 13, 2019 at 1:33 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 8 quoted lines
>
> The original code considered that two entries with the same mode and
> the same "contents" are the same.  As nobody sane tracks an empty
> file for an extended span of history, that meant that most of the
> time, intent-to-add entries, which has the normal mode bits for the
> blobs (with or without the executable bit) and object name for a
> zero length blob, would have been judged "different".
>
I agree, this edge case is really arcane. The rabbit hole was deep :)
Show 7 quoted lines
> So perhaps
>
> +       !ce_intent_to_add(a) && !ce_intent_to_add(b) &&
>
> i.e. "a cache entry is eligible to be same with something else only
> when its I-T-A bit is unset".
>
I decided to follow René's suggestion in response to this. Patch coming soon.
Varun
Junio C Hamano· Aug 15, 2019, 16:34 UTC · re: Varun Naik · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

Varun Naik <vcnaik94@gmail.com> writes:
Show 11 quoted lines
> On Tue, Aug 13, 2019 at 1:33 PM Junio C Hamano <gitster@pobox.com> wrote:
>>
>> The original code considered that two entries with the same mode and
>> the same "contents" are the same.  As nobody sane tracks an empty
>> file for an extended span of history, that meant that most of the
>> time, intent-to-add entries, which has the normal mode bits for the
>> blobs (with or without the executable bit) and object name for a
>> zero length blob, would have been judged "different".
>>
>
> I agree, this edge case is really arcane. The rabbit hole was deep :)

In retrospect, perhaps I shouldn't have used the empty-blob SHA-1 for I-T-A entries (instead, perhaps 0{40} or something impossible) when I invented this feature at 39425819 ("git-add --intent-to-add (-N)", 2008-08-21). It made the code to "diff" easier than having to special case comparison between a real blob and an I-T-A entry, but we are paying the price for that laziness with a discussion and a patch like this one.

Show 9 quoted lines
>> So perhaps
>>
>> +       !ce_intent_to_add(a) && !ce_intent_to_add(b) &&
>>
>> i.e. "a cache entry is eligible to be same with something else only
>> when its I-T-A bit is unset".
>>
>
> I decided to follow René's suggestion in response to this. Patch coming soon.

Either is fine as the implementation of the same semantics; I however am not sure if two I-T-A entries should compare "same" or "not same", or if we are better off catching the caller that feeds two I-T-A entries to same() with a BUG().

Thanks.
Varun Naik· Aug 19, 2019, 15:35 UTC · re: Junio C Hamano · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

On Thu, Aug 15, 2019 at 9:34 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
>
> Varun Naik <vcnaik94@gmail.com> writes:
>
> > On Tue, Aug 13, 2019 at 1:33 PM Junio C Hamano <gitster@pobox.com> wrote:
> >>
> >> So perhaps
> >>
> >> +       !ce_intent_to_add(a) && !ce_intent_to_add(b) &&
> >>
> >> i.e. "a cache entry is eligible to be same with something else only
> >> when its I-T-A bit is unset".
> >>
> >
> > I decided to follow René's suggestion in response to this. Patch coming soon.
>
> Either is fine as the implementation of the same semantics; I
> however am not sure if two I-T-A entries should compare "same" or
> "not same", or if we are better off catching the caller that feeds
> two I-T-A entries to same() with a BUG().

I'd argue that two ita cache entries should be a BUG. Since we believe that a cache entry in the tree can never have the intent-to-add bit set, it suffices to show that no call to same() ever passes two cache entries from the index.

The call in unpack-trees.c:merged_entry() compares the "old" entry (which comes from the index in all cases) to a newly created "merge" entry (which is a duplicate of an entry from a tree in all cases). All other calls compare either entries from two trees, or an entry from a tree and an entry from the index. I also can't think of any case where someone would want to check if two index entries are the "same" in the future.

The same argument probably extends to the conflicted bit, but changing that is probably out of scope of this patch.

Varun
Junio C Hamano· Aug 19, 2019, 19:54 UTC · re: Varun Naik · lore

Re: [RFC PATCH] unpack-trees.c: handle empty deleted ita files

Varun Naik <vcnaik94@gmail.com> writes:
Show 12 quoted lines
>> Either is fine as the implementation of the same semantics; I
>> however am not sure if two I-T-A entries should compare "same" or
>> "not same", or if we are better off catching the caller that feeds
>> two I-T-A entries to same() with a BUG().
>
> I'd argue that two ita cache entries should be a BUG. Since we believe
> that a cache entry in the tree can never have the intent-to-add bit set,
> it suffices to show that no call to same() ever passes two cache entries
> from the index.
> ...
> The same argument probably extends to the conflicted bit, but changing
> that is probably out of scope of this patch.

Yup. I think the patch as-posted is fine. I also agree that tightening the validity check of parameters to same() is better done as a separate topic.

Varun Naik· Aug 15, 2019, 16:21 UTC · re: Varun Naik · lore

[PATCH v2] unpack-trees.c: distinguish ita files from empty files

It is possible to delete a committed file from the index and then add it as intent-to-add. Several variations of "reset" and "checkout" should resurrect the file in the index from HEAD. "merge" (non-fast-forward), "cherry-pick", and "revert" should all fail with an error message. This patch provides the desired behavior even when the file is empty in HEAD.

Furthermore, "merge --ff-only" should prevent a fast-forward merge if a file is marked as intent-to-add. This patch provides the desired behavior even when the file exists with empty contents in the final commit.

The affected commands all compare two cache entries by calling unpack-trees.c:same(). Since a cache entry for an ita file and a cache entry for an empty file have the same oid, if the two files had the same mode, then the cache entries were previously considered the "same". This fix checks the intent-to-add bits so if one or both of the cache entries are marked as ita, then the two cache entries are not considered the "same".

Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Varun Naik <vcnaik94@gmail.com>
---
I rewrote the code in same(), as René suggested.

New in v2 is a test for "merge --ff-only", which calls unpack-trees.c:twoway_merge(), which calls unpack-trees.c:same(). Previously, if the file existed with empty contents in the final commit, then the fast-forward merge went through, but the file ended up as a deleted ita file in the index. Now, the merge fails with an error.

 t/t3030-merge-recursive.sh    | 25 +++++++++++++++---
 t/t3501-revert-cherry-pick.sh | 49 ++++++++++++++++++++++++++++++++++-
 t/t7104-reset-hard.sh         | 11 ++++++++
 t/t7110-reset-merge.sh        | 31 ++++++++++++++++++++++
 t/t7201-co.sh                 | 12 +++++++++
 t/t7607-merge-overwrite.sh    | 23 ++++++++++++++++
 unpack-trees.c                |  3 ++-
 7 files changed, 149 insertions(+), 5 deletions(-)
Show changes to 7 files +149 −5

t/t3030-merge-recursive.sh, t/t3501-revert-cherry-pick.sh, t/t7104-reset-hard.sh, t/t7110-reset-merge.sh, t/t7201-co.sh, t/t7607-merge-overwrite.sh, unpack-trees.c

diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
index ff641b348a..8aebb829a6 100755
--- a/t/t3030-merge-recursive.sh
+++ b/t/t3030-merge-recursive.sh
@@ -303,13 +303,32 @@ test_expect_success 'fail if the index has unresolved entries' '
 	git checkout -f "$c1" &&
 
 	test_must_fail git merge "$c5" &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "not possible because you have unmerged files" out &&
 	git add -u &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "You have not concluded your merge" out &&
 	rm -f .git/MERGE_HEAD &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
+test_expect_success 'fail if a deleted intent-to-add file exists in the index' '
+	git checkout -f "$c1" &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git checkout -f "$c1" &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
 '
 
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index d1c68af8c5..45d816fc0c 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -91,16 +91,63 @@ test_expect_success 'cherry-pick on stat-dirty working tree' '
 	)
 '
 
-test_expect_success 'revert forbidden on dirty working tree' '
+test_expect_success 'cherry-pick forbidden on dirty working tree' '
 
+	git checkout -b temp &&
 	echo content >extra_file &&
 	git add extra_file &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors
+
+'
+
+test_expect_success 'revert forbidden on dirty working tree' '
+
 	test_must_fail git revert HEAD 2>errors &&
 	test_i18ngrep "your local changes would be overwritten by " errors
 
 '
 
+test_expect_success 'cherry-pick fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
+test_expect_success 'revert fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
 test_expect_success 'cherry-pick on unborn branch' '
+	git checkout -f rename1 &&
 	git checkout --orphan unborn &&
 	git rm --cached -r . &&
 	rm -rf * &&
diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
index 16faa07813..96a0b779e7 100755
--- a/t/t7104-reset-hard.sh
+++ b/t/t7104-reset-hard.sh
@@ -43,4 +43,15 @@ test_expect_success 'reset --hard did not corrupt index or cached-tree' '
 
 '
 
+test_expect_success 'reset --hard adds deleted intent-to-add file back to index' '
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git reset --hard &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
index a82a07a04a..3346759375 100755
--- a/t/t7110-reset-merge.sh
+++ b/t/t7110-reset-merge.sh
@@ -292,4 +292,35 @@ test_expect_success '--keep fails with added/deleted merge' '
     test_i18ngrep "middle of a merge" err.log
 '
 
+test_expect_success 'reset --merge adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    git add nonempty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached nonempty &&
+    git add -N nonempty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep nonempty err.log | grep "not uptodate" &&
+    git reset --hard initial &&
+    >empty &&
+    git add empty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached empty &&
+    git add -N empty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep empty err.log | grep "not uptodate"
+'
+
+test_expect_success 'reset --keep adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    >empty &&
+    git add nonempty empty &&
+    git commit -m "create files to be deleted" &&
+    git rm --cached nonempty empty &&
+    git add -N nonempty empty &&
+    git reset --keep HEAD &&
+    git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 5990299fc9..4c0c33ce33 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -674,4 +674,16 @@ test_expect_success 'custom merge driver with checkout -m' '
 	test_cmp expect arm
 '
 
+test_expect_success 'checkout -f HEAD adds deleted intent-to-add file back to index' '
+	git reset --hard master &&
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git checkout -f HEAD &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7607-merge-overwrite.sh b/t/t7607-merge-overwrite.sh
index dd8ab7ede1..7e6b7b9a07 100755
--- a/t/t7607-merge-overwrite.sh
+++ b/t/t7607-merge-overwrite.sh
@@ -20,6 +20,16 @@ test_expect_success 'setup' '
 	git add sub/f sub2/f &&
 	git commit -m sub &&
 	git tag sub &&
+	git reset --hard c0 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m nonempty &&
+	git tag nonempty &&
+	git reset --hard c0 &&
+	>empty &&
+	git add empty &&
+	git commit -m empty &&
+	git tag empty &&
 	echo "VERY IMPORTANT CHANGES" > important
 '
 
@@ -192,4 +202,17 @@ test_expect_success 'will not clobber WT/index when merging into unborn' '
 	grep bar untracked-file
 '
 
+test_expect_success 'will not overwrite ita file on fast-forward merge' '
+	git reset --hard c0 &&
+	>nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge --ff-only nonempty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git reset --hard c0 &&
+	>empty &&
+	git add -N empty &&
+	test_must_fail git merge --ff-only empty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
 test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index 50189909b8..5e6d88f36b 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1658,9 +1658,10 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
 		return 0;
 	if (!a && !b)
 		return 1;
-	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
+	if ((a->ce_flags | b->ce_flags) & (CE_CONFLICTED | CE_INTENT_TO_ADD))
 		return 0;
 	return a->ce_mode == b->ce_mode &&
+	       !ce_intent_to_add(a) == !ce_intent_to_add(b) &&
 	       oideq(&a->oid, &b->oid);
 }
 
-- 
2.22.0
René Scharfe· Aug 15, 2019, 16:46 UTC · re: Varun Naik · lore

Re: [PATCH v2] unpack-trees.c: distinguish ita files from empty files

Am 15.08.19 um 18:21 schrieb Varun Naik:
Show 11 quoted lines
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 50189909b8..5e6d88f36b 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -1658,9 +1658,10 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
>  		return 0;
>  	if (!a && !b)
>  		return 1;
> -	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
> +	if ((a->ce_flags | b->ce_flags) & (CE_CONFLICTED | CE_INTENT_TO_ADD))
>  		return 0;

If any of the two entries has the intent-to-add flag set, they won't be considered the same and we exit early.

>  	return a->ce_mode == b->ce_mode &&
> +	       !ce_intent_to_add(a) == !ce_intent_to_add(b) &&

So if we reach this point, the flag can't be set, ce_intent_to_add() returns 0 on both sides and thus the added condition is always true. You might as well remove it.

>  	       oideq(&a->oid, &b->oid);
>  }
>
>
Varun Naik· Aug 21, 2019, 03:21 UTC · re: Varun Naik · lore

[PATCH v3] unpack-trees.c: distinguish ita files from empty files

It is possible to delete a committed file from the index and then add it as intent-to-add. Several variations of "reset" and "checkout" should resurrect the file in the index from HEAD. "merge" (non-fast-forward), "cherry-pick", and "revert" should all fail with an error message. This patch provides the desired behavior even when the file is empty in HEAD.

Furthermore, "merge --ff-only" should prevent a fast-forward merge if a file is marked as intent-to-add. This patch provides the desired behavior even when the file exists with empty contents in the final commit.

The affected commands all compare two cache entries by calling unpack-trees.c:same(). Since a cache entry for an ita file and a cache entry for an empty file have the same oid, if the two files had the same mode, then the cache entries were previously considered the "same". This fix checks the intent-to-add bits so if one or both of the cache entries are marked as ita, then the two cache entries are not considered the "same".

Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Varun Naik <vcnaik94@gmail.com>
---
I rewrote the code in same(), as René suggested. Somehow the extra line
slipped through into v2.

As Junio suggested, I'm ignoring the validity check of the CE_CONFLICTED and CE_INTENT_TO_ADD bits in this patch.

 t/t3030-merge-recursive.sh    | 25 +++++++++++++++---
 t/t3501-revert-cherry-pick.sh | 49 ++++++++++++++++++++++++++++++++++-
 t/t7104-reset-hard.sh         | 11 ++++++++
 t/t7110-reset-merge.sh        | 31 ++++++++++++++++++++++
 t/t7201-co.sh                 | 12 +++++++++
 t/t7607-merge-overwrite.sh    | 23 ++++++++++++++++
 unpack-trees.c                |  2 +-
 7 files changed, 148 insertions(+), 5 deletions(-)
Show changes to 7 files +148 −5

t/t3030-merge-recursive.sh, t/t3501-revert-cherry-pick.sh, t/t7104-reset-hard.sh, t/t7110-reset-merge.sh, t/t7201-co.sh, t/t7607-merge-overwrite.sh, unpack-trees.c

diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
index ff641b348a..8aebb829a6 100755
--- a/t/t3030-merge-recursive.sh
+++ b/t/t3030-merge-recursive.sh
@@ -303,13 +303,32 @@ test_expect_success 'fail if the index has unresolved entries' '
 	git checkout -f "$c1" &&
 
 	test_must_fail git merge "$c5" &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "not possible because you have unmerged files" out &&
 	git add -u &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "You have not concluded your merge" out &&
 	rm -f .git/MERGE_HEAD &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
+test_expect_success 'fail if a deleted intent-to-add file exists in the index' '
+	git checkout -f "$c1" &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git checkout -f "$c1" &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
 '
 
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index d1c68af8c5..45d816fc0c 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -91,16 +91,63 @@ test_expect_success 'cherry-pick on stat-dirty working tree' '
 	)
 '
 
-test_expect_success 'revert forbidden on dirty working tree' '
+test_expect_success 'cherry-pick forbidden on dirty working tree' '
 
+	git checkout -b temp &&
 	echo content >extra_file &&
 	git add extra_file &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors
+
+'
+
+test_expect_success 'revert forbidden on dirty working tree' '
+
 	test_must_fail git revert HEAD 2>errors &&
 	test_i18ngrep "your local changes would be overwritten by " errors
 
 '
 
+test_expect_success 'cherry-pick fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
+test_expect_success 'revert fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "overwritten" errors
+'
+
 test_expect_success 'cherry-pick on unborn branch' '
+	git checkout -f rename1 &&
 	git checkout --orphan unborn &&
 	git rm --cached -r . &&
 	rm -rf * &&
diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
index 16faa07813..96a0b779e7 100755
--- a/t/t7104-reset-hard.sh
+++ b/t/t7104-reset-hard.sh
@@ -43,4 +43,15 @@ test_expect_success 'reset --hard did not corrupt index or cached-tree' '
 
 '
 
+test_expect_success 'reset --hard adds deleted intent-to-add file back to index' '
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git reset --hard &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
index a82a07a04a..3346759375 100755
--- a/t/t7110-reset-merge.sh
+++ b/t/t7110-reset-merge.sh
@@ -292,4 +292,35 @@ test_expect_success '--keep fails with added/deleted merge' '
     test_i18ngrep "middle of a merge" err.log
 '
 
+test_expect_success 'reset --merge adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    git add nonempty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached nonempty &&
+    git add -N nonempty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep nonempty err.log | grep "not uptodate" &&
+    git reset --hard initial &&
+    >empty &&
+    git add empty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached empty &&
+    git add -N empty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep empty err.log | grep "not uptodate"
+'
+
+test_expect_success 'reset --keep adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    >empty &&
+    git add nonempty empty &&
+    git commit -m "create files to be deleted" &&
+    git rm --cached nonempty empty &&
+    git add -N nonempty empty &&
+    git reset --keep HEAD &&
+    git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index 5990299fc9..4c0c33ce33 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -674,4 +674,16 @@ test_expect_success 'custom merge driver with checkout -m' '
 	test_cmp expect arm
 '
 
+test_expect_success 'checkout -f HEAD adds deleted intent-to-add file back to index' '
+	git reset --hard master &&
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git checkout -f HEAD &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7607-merge-overwrite.sh b/t/t7607-merge-overwrite.sh
index dd8ab7ede1..7e6b7b9a07 100755
--- a/t/t7607-merge-overwrite.sh
+++ b/t/t7607-merge-overwrite.sh
@@ -20,6 +20,16 @@ test_expect_success 'setup' '
 	git add sub/f sub2/f &&
 	git commit -m sub &&
 	git tag sub &&
+	git reset --hard c0 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m nonempty &&
+	git tag nonempty &&
+	git reset --hard c0 &&
+	>empty &&
+	git add empty &&
+	git commit -m empty &&
+	git tag empty &&
 	echo "VERY IMPORTANT CHANGES" > important
 '
 
@@ -192,4 +202,17 @@ test_expect_success 'will not clobber WT/index when merging into unborn' '
 	grep bar untracked-file
 '
 
+test_expect_success 'will not overwrite ita file on fast-forward merge' '
+	git reset --hard c0 &&
+	>nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge --ff-only nonempty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git reset --hard c0 &&
+	>empty &&
+	git add -N empty &&
+	test_must_fail git merge --ff-only empty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
 test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index 50189909b8..8d974667f2 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1658,7 +1658,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
 		return 0;
 	if (!a && !b)
 		return 1;
-	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
+	if ((a->ce_flags | b->ce_flags) & (CE_CONFLICTED | CE_INTENT_TO_ADD))
 		return 0;
 	return a->ce_mode == b->ce_mode &&
 	       oideq(&a->oid, &b->oid);
-- 
2.23.0
Varun Naik· Feb 14, 2020, 17:14 UTC · re: Varun Naik · lore

[PATCH v4] unpack-trees.c: distinguish ita files from empty files

It is possible to delete a committed file from the index and then add it as intent-to-add. Several variations of "reset" and "checkout" should resurrect the file in the index from HEAD. "merge" (non-fast-forward), "cherry-pick", and "revert" should all fail with an error message. This patch provides the desired behavior even when the file is empty in HEAD.

Furthermore, "merge --ff-only" should prevent a fast-forward merge if a file is marked as intent-to-add. This patch provides the desired behavior even when the file exists with empty contents in the final commit.

The affected commands all compare two cache entries by calling unpack-trees.c:same(). Since a cache entry for an ita file and a cache entry for an empty file have the same oid, if the two files had the same mode, then the cache entries were previously considered the "same". This fix checks the intent-to-add bits so if one or both of the cache entries are marked as ita, then the two cache entries are not considered the "same".

Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Varun Naik <vcnaik94@gmail.com>
---
This patch never made it into pu, but I'll try one more time. I decided to
discard [0], so I tweaked the test cases in this patch.

To repeat, I'm ignoring the validity check of the CE_CONFLICTED and CE_INTENT_TO_ADD bits in this patch.

[0]: https://public-inbox.org/git/20190801161558.12838-1-vcnaik94@gmail.com/
 t/t3030-merge-recursive.sh    | 25 +++++++++++++++---
 t/t3501-revert-cherry-pick.sh | 49 ++++++++++++++++++++++++++++++++++-
 t/t7104-reset-hard.sh         | 11 ++++++++
 t/t7110-reset-merge.sh        | 31 ++++++++++++++++++++++
 t/t7201-co.sh                 | 12 +++++++++
 t/t7607-merge-overwrite.sh    | 23 ++++++++++++++++
 unpack-trees.c                |  2 +-
 7 files changed, 148 insertions(+), 5 deletions(-)
Show changes to 7 files +148 −5

t/t3030-merge-recursive.sh, t/t3501-revert-cherry-pick.sh, t/t7104-reset-hard.sh, t/t7110-reset-merge.sh, t/t7201-co.sh, t/t7607-merge-overwrite.sh, unpack-trees.c

diff --git a/t/t3030-merge-recursive.sh b/t/t3030-merge-recursive.sh
index 2170758e38..b07153fa14 100755
--- a/t/t3030-merge-recursive.sh
+++ b/t/t3030-merge-recursive.sh
@@ -303,13 +303,32 @@ test_expect_success 'fail if the index has unresolved entries' '
 	git checkout -f "$c1" &&
 
 	test_must_fail git merge "$c5" &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "not possible because you have unmerged files" out &&
 	git add -u &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "You have not concluded your merge" out &&
 	rm -f .git/MERGE_HEAD &&
-	test_must_fail git merge "$c5" 2> out &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
+test_expect_success 'fail if a deleted intent-to-add file exists in the index' '
+	git checkout -f "$c1" &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge "$c5" 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git checkout -f "$c1" &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git merge "$c5" 2>out &&
 	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
 '
 
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index 7c1da21df1..88c15466ea 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -91,16 +91,63 @@ test_expect_success 'cherry-pick on stat-dirty working tree' '
 	)
 '
 
-test_expect_success 'revert forbidden on dirty working tree' '
+test_expect_success 'cherry-pick forbidden on dirty working tree' '
 
+	git checkout -b temp &&
 	echo content >extra_file &&
 	git add extra_file &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors
+
+'
+
+test_expect_success 'revert forbidden on dirty working tree' '
+
 	test_must_fail git revert HEAD 2>errors &&
 	test_i18ngrep "your local changes would be overwritten by " errors
 
 '
 
+test_expect_success 'cherry-pick fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git cherry-pick rename2 2>errors &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" errors
+'
+
+test_expect_success 'revert fails if a deleted intent-to-add file exists in the index' '
+	git reset --hard rename1 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached nonempty &&
+	git add -N nonempty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "your local changes would be overwritten by " errors &&
+	git reset --hard rename1 &&
+	>empty &&
+	git add empty &&
+	git commit -m "create file to be deleted" &&
+	git rm --cached empty &&
+	git add -N empty &&
+	test_must_fail git revert rename2 2>errors &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" errors
+'
+
 test_expect_success 'cherry-pick on unborn branch' '
+	git checkout -f rename1 &&
 	git checkout --orphan unborn &&
 	git rm --cached -r . &&
 	rm -rf * &&
diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
index 16faa07813..96a0b779e7 100755
--- a/t/t7104-reset-hard.sh
+++ b/t/t7104-reset-hard.sh
@@ -43,4 +43,15 @@ test_expect_success 'reset --hard did not corrupt index or cached-tree' '
 
 '
 
+test_expect_success 'reset --hard adds deleted intent-to-add file back to index' '
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git reset --hard &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
index a82a07a04a..3346759375 100755
--- a/t/t7110-reset-merge.sh
+++ b/t/t7110-reset-merge.sh
@@ -292,4 +292,35 @@ test_expect_success '--keep fails with added/deleted merge' '
     test_i18ngrep "middle of a merge" err.log
 '
 
+test_expect_success 'reset --merge adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    git add nonempty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached nonempty &&
+    git add -N nonempty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep nonempty err.log | grep "not uptodate" &&
+    git reset --hard initial &&
+    >empty &&
+    git add empty &&
+    git commit -m "create file to be deleted" &&
+    git rm --cached empty &&
+    git add -N empty &&
+    test_must_fail git reset --merge HEAD 2>err.log &&
+    grep empty err.log | grep "not uptodate"
+'
+
+test_expect_success 'reset --keep adds deleted intent-to-add file back to index' '
+    git reset --hard initial &&
+    echo "nonempty" >nonempty &&
+    >empty &&
+    git add nonempty empty &&
+    git commit -m "create files to be deleted" &&
+    git rm --cached nonempty empty &&
+    git add -N nonempty empty &&
+    git reset --keep HEAD &&
+    git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
index b696bae5f5..f00c16d615 100755
--- a/t/t7201-co.sh
+++ b/t/t7201-co.sh
@@ -674,4 +674,16 @@ test_expect_success 'custom merge driver with checkout -m' '
 	test_cmp expect arm
 '
 
+test_expect_success 'checkout -f HEAD adds deleted intent-to-add file back to index' '
+	git reset --hard master &&
+	echo "nonempty" >nonempty &&
+	>empty &&
+	git add nonempty empty &&
+	git commit -m "create files to be deleted" &&
+	git rm --cached nonempty empty &&
+	git add -N nonempty empty &&
+	git checkout -f HEAD &&
+	git diff --cached --exit-code nonempty empty
+'
+
 test_done
diff --git a/t/t7607-merge-overwrite.sh b/t/t7607-merge-overwrite.sh
index dd8ab7ede1..7e6b7b9a07 100755
--- a/t/t7607-merge-overwrite.sh
+++ b/t/t7607-merge-overwrite.sh
@@ -20,6 +20,16 @@ test_expect_success 'setup' '
 	git add sub/f sub2/f &&
 	git commit -m sub &&
 	git tag sub &&
+	git reset --hard c0 &&
+	echo "nonempty" >nonempty &&
+	git add nonempty &&
+	git commit -m nonempty &&
+	git tag nonempty &&
+	git reset --hard c0 &&
+	>empty &&
+	git add empty &&
+	git commit -m empty &&
+	git tag empty &&
 	echo "VERY IMPORTANT CHANGES" > important
 '
 
@@ -192,4 +202,17 @@ test_expect_success 'will not clobber WT/index when merging into unborn' '
 	grep bar untracked-file
 '
 
+test_expect_success 'will not overwrite ita file on fast-forward merge' '
+	git reset --hard c0 &&
+	>nonempty &&
+	git add -N nonempty &&
+	test_must_fail git merge --ff-only nonempty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out &&
+	git reset --hard c0 &&
+	>empty &&
+	git add -N empty &&
+	test_must_fail git merge --ff-only empty 2>out &&
+	test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
+'
+
 test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index 2399b6818b..3e63d175f8 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -1715,7 +1715,7 @@ static int same(const struct cache_entry *a, const struct cache_entry *b)
 		return 0;
 	if (!a && !b)
 		return 1;
-	if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
+	if ((a->ce_flags | b->ce_flags) & (CE_CONFLICTED | CE_INTENT_TO_ADD))
 		return 0;
 	return a->ce_mode == b->ce_mode &&
 	       oideq(&a->oid, &b->oid);
-- 
2.25.0

← back to recent threads