checkout $tree: do not throw away unchanged index entries

When we "git checkout $tree", we pull paths from $tree into the index, and then check the resulting entries out to the worktree. Our method for the first step is rather heavy-handed, though; it clobbers the entire existing index entry, even if the content is the same. This means we lose our stat information, leading checkout_entry to later rewrite the entire file with identical content. Instead, let's see if we have the identical entry already in the index, in which case we leave it in place. That lets checkout_entry do the right thing. Our tests cover two interesting cases: 1. We make sure that a file which has no changes is not rewritten. 2. We make sure that we do update a file that is unchanged in the index (versus $tree), but has working tree changes. We keep the old index entry, and checkout_entry is able to realize that our stat information is out of date. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 13, 2014 at 13:30 UTC c5326bd62b7e168ba1339dacb7ee812d0fe98c7c
2 files changed +35
builtin/checkout.c
+18
@@ -67,6 +67,7 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
67 {
68 int len;
69 struct cache_entry *ce;
70 + int pos;
71
72 if (S_ISDIR(mode))
73 return READ_TREE_RECURSIVE;
@@ -79,6 +80,23 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
80 ce->ce_flags = create_ce_flags(0) | CE_UPDATE;
81 ce->ce_namelen = len;
82 ce->ce_mode = create_ce_mode(mode);
83 +
84 + /*
85 + * If the entry is the same as the current index, we can leave the old
86 + * entry in place. Whether it is UPTODATE or not, checkout_entry will
87 + * do the right thing.
88 + */
89 + pos = cache_name_pos(ce->name, ce->ce_namelen);
90 + if (pos >= 0) {
91 + struct cache_entry *old = active_cache[pos];
92 + if (ce->ce_mode == old->ce_mode &&
93 + !hashcmp(ce->sha1, old->sha1)) {
94 + old->ce_flags |= CE_UPDATE;
95 + free(ce);
96 + return 0;
97 + }
98 + }
99 +
100 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
101 return 0;
102 }
t/t2022-checkout-paths.sh
+17
@@ -61,4 +61,21 @@ test_expect_success 'do not touch unmerged entries matching $path but not in $tr
61 test_cmp expect.next0 actual.next0
62 '
63
64 +test_expect_success 'do not touch files that are already up-to-date' '
65 + git reset --hard &&
66 + echo one >file1 &&
67 + echo two >file2 &&
68 + git add file1 file2 &&
69 + git commit -m base &&
70 + echo modified >file1 &&
71 + test-chmtime =1000000000 file2 &&
72 + git update-index -q --refresh &&
73 + git checkout HEAD -- file1 file2 &&
74 + echo one >expect &&
75 + test_cmp expect file1 &&
76 + echo "1000000000 file2" >expect &&
77 + test-chmtime -v +0 file2 >actual &&
78 + test_cmp expect actual
79 +'
80 +
81 test_done