refresh_index: do not show unmerged path that is outside pathspec

When running "git add --refresh <pathspec>", we incorrectly showed the path that is unmerged even if it is outside the specified pathspec, even though we did honor pathspec and refreshed only the paths that matched. Note that this cange does not affect "git update-index --refresh"; for hysterical raisins, it does not take a pathspec (it takes real paths) and more importantly itss command line options are parsed and executed one by one as they are encountered, so "git update-index --refresh foo" means "first refresh the index, and then update the entry 'foo' by hashing the contents in file 'foo'", not "refresh only entry 'foo'". Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 17, 2012 at 10:11 UTC 3d1f148c3381de410a9b02a05bde05cd0fb17a0c
2 files changed +24 -2
read-cache.c
+9 -2
@@ -1120,11 +1120,16 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p
1120 struct cache_entry *ce, *new;
1121 int cache_errno = 0;
1122 int changed = 0;
1123 + int filtered = 0;
1124
1125 ce = istate->cache[i];
1126 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1127 continue;
1128
1129 + if (pathspec &&
1130 + !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
1131 + filtered = 1;
1132 +
1133 if (ce_stage(ce)) {
1134 while ((i < istate->cache_nr) &&
1135 ! strcmp(istate->cache[i]->name, ce->name))
@@ -1132,12 +1137,14 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p
1137 i--;
1138 if (allow_unmerged)
1139 continue;
1135 - show_file(unmerged_fmt, ce->name, in_porcelain, &first, header_msg);
1140 + if (!filtered)
1141 + show_file(unmerged_fmt, ce->name, in_porcelain,
1142 + &first, header_msg);
1143 has_errors = 1;
1144 continue;
1145 }
1146
1140 - if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
1147 + if (filtered)
1148 continue;
1149
1150 new = refresh_cache_ent(istate, ce, options, &cache_errno, &changed);
t/t3700-add.sh
+15
@@ -179,6 +179,21 @@ test_expect_success 'git add --refresh' '
179 test -z "`git diff-index HEAD -- foo`"
180 '
181
182 +test_expect_success 'git add --refresh with pathspec' '
183 + git reset --hard &&
184 + echo >foo && echo >bar && echo >baz &&
185 + git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
186 + echo "100644 $H 3 foo" | git update-index --index-info &&
187 + test-chmtime -60 bar baz &&
188 + >expect &&
189 + git add --refresh bar >actual &&
190 + test_cmp expect actual &&
191 +
192 + git diff-files --name-only >actual &&
193 + ! grep bar actual&&
194 + grep baz actual
195 +'
196 +
197 test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
198 git reset --hard &&
199 date >foo1 &&