checkout.c: unstage empty deleted ita files
authorVarun Naik <vcnaik94@gmail.com>
Fri, 2 Aug 2019 16:28:52 +0000 (09:28 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Aug 2019 17:28:10 +0000 (10:28 -0700)
It is possible to delete a committed file from the index and then add it
as intent-to-add. After `git checkout HEAD <pathspec>`, the file should
be identical in the index and HEAD. The command already works correctly
if the file has contents in HEAD. This patch provides the desired
behavior even when the file is empty in HEAD.

`git checkout HEAD <pathspec>` calls tree.c:read_tree_1(), with fn
pointing to checkout.c:update_some(). update_some() creates a new cache
entry but discards it when its mode and oid match those of the old
entry. A cache entry for an ita file and a cache entry for an empty file
have the same oid. Therefore, an empty deleted ita file previously
passed both of these checks, and the new entry was discarded, so the
file remained unchanged in the index. After this fix, if the file is
marked as ita in the cache, then we avoid discarding the new entry and
add the new entry to the cache instead.

This change should not affect newly added ita files. For those, inside
tree.c:read_tree_1(), tree_entry_interesting() returns
entry_not_interesting, so fn is never called.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Varun Naik <vcnaik94@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2022-checkout-paths.sh
index 91f8509f85396cb957ee87d1b0e8ba1284c8c800..27daa09c3ce860f967802e9783afcf00d965fcbd 100644 (file)
@@ -126,6 +126,7 @@ static int update_some(const struct object_id *oid, struct strbuf *base,
        if (pos >= 0) {
                struct cache_entry *old = active_cache[pos];
                if (ce->ce_mode == old->ce_mode &&
+                   !ce_intent_to_add(old) &&
                    oideq(&ce->oid, &old->oid)) {
                        old->ce_flags |= CE_UPDATE;
                        discard_cache_entry(ce);
index fc3eb43b890977bf793f6ed70e6d6caed2902407..6844afafc0eb926713e73c1042874155d18c08bc 100755 (executable)
@@ -78,4 +78,15 @@ test_expect_success 'do not touch files that are already up-to-date' '
        test_cmp expect actual
 '
 
+test_expect_success 'checkout HEAD 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 checkout HEAD nonempty empty &&
+       git diff --cached --exit-code
+'
+
 test_done