Skip to content

Commit 7533687

Browse files
iabervongitster
authored andcommitted
Write index file on any checkout of files
We need to rewrite the index file when we check out files, even if we haven't modified the blob info by reading from another tree, so that we get the stat cache to include the fact that we just modified the file so it doesn't need to be refreshed. While we're at it, move everything that needs to be done to check out some paths from a tree (or the current index) into checkout_paths(). Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a5aa930 commit 7533687

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

builtin-checkout.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,16 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
6767

6868
static int read_tree_some(struct tree *tree, const char **pathspec)
6969
{
70-
int newfd;
71-
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
72-
newfd = hold_locked_index(lock_file, 1);
73-
read_cache();
74-
7570
read_tree_recursive(tree, "", 0, 0, pathspec, update_some);
7671

77-
if (write_cache(newfd, active_cache, active_nr) ||
78-
commit_locked_index(lock_file))
79-
die("unable to write new index file");
80-
8172
/* update the index with the given tree's info
8273
* for all args, expanding wildcards, and exit
8374
* with any non-zero return code.
8475
*/
8576
return 0;
8677
}
8778

88-
static int checkout_paths(const char **pathspec)
79+
static int checkout_paths(struct tree *source_tree, const char **pathspec)
8980
{
9081
int pos;
9182
struct checkout state;
@@ -94,6 +85,15 @@ static int checkout_paths(const char **pathspec)
9485
int flag;
9586
struct commit *head;
9687

88+
int newfd;
89+
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
90+
91+
newfd = hold_locked_index(lock_file, 1);
92+
read_cache();
93+
94+
if (source_tree)
95+
read_tree_some(source_tree, pathspec);
96+
9797
for (pos = 0; pathspec[pos]; pos++)
9898
;
9999
ps_matched = xcalloc(1, pos);
@@ -116,6 +116,10 @@ static int checkout_paths(const char **pathspec)
116116
}
117117
}
118118

119+
if (write_cache(newfd, active_cache, active_nr) ||
120+
commit_locked_index(lock_file))
121+
die("unable to write new index file");
122+
119123
resolve_ref("HEAD", rev, 0, &flag);
120124
head = lookup_commit_reference_gently(rev, 1);
121125

@@ -558,11 +562,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
558562
}
559563
}
560564

561-
if (source_tree)
562-
read_tree_some(source_tree, pathspec);
563-
else
564-
read_cache();
565-
return checkout_paths(pathspec);
565+
return checkout_paths(source_tree, pathspec);
566566
}
567567

568568
if (new.name && !new.commit) {

t/t2009-checkout-statinfo.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
test_description='checkout should leave clean stat info'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
9+
echo hello >world &&
10+
git update-index --add world &&
11+
git commit -m initial &&
12+
git branch side &&
13+
echo goodbye >world &&
14+
git update-index --add world &&
15+
git commit -m second
16+
17+
'
18+
19+
test_expect_success 'branch switching' '
20+
21+
git reset --hard &&
22+
test "$(git diff-files --raw)" = "" &&
23+
24+
git checkout master &&
25+
test "$(git diff-files --raw)" = "" &&
26+
27+
git checkout side &&
28+
test "$(git diff-files --raw)" = "" &&
29+
30+
git checkout master &&
31+
test "$(git diff-files --raw)" = ""
32+
33+
'
34+
35+
test_expect_success 'path checkout' '
36+
37+
git reset --hard &&
38+
test "$(git diff-files --raw)" = "" &&
39+
40+
git checkout master world &&
41+
test "$(git diff-files --raw)" = "" &&
42+
43+
git checkout side world &&
44+
test "$(git diff-files --raw)" = "" &&
45+
46+
git checkout master world &&
47+
test "$(git diff-files --raw)" = ""
48+
49+
'
50+
51+
test_done
52+

0 commit comments

Comments
 (0)