git add --intent-to-add: fix removal of cached emptiness
This uses the extended index flag mechanism introduced earlier to mark the entries added to the index via "git add -N" with CE_INTENT_TO_ADD. The logic to detect an "intent to add" entry for the purpose of allowing "git rm --cached $path" is tightened to check not just for a staged empty blob, but with the CE_INTENT_TO_ADD bit. This protects an empty blob that was explicitly added and then modified in the work tree from being dropped with this sequence: $ >empty $ git add empty $ echo "non empty" >empty $ git rm --cached empty Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Nov 28, 2008 at 19:55 UTC
388b2acd6e649145d4368a10cf4df5f5bbbac2c2
4 files changed
+7
-4
builtin-rm.c
+1
-1
@@ -115,7 +115,7 @@ static int check_local_mod(unsigned char *head, int index_only)
115
* "intent to add" entry.
116
*/
117
if (local_changes && staged_changes) {
118
- if (!index_only || !is_empty_blob_sha1(ce->sha1))
118
+ if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD))
119
errs = error("'%s' has staged content different "
120
"from both the file and the HEAD\n"
121
"(use -f to force removal)", name);
cache.h
+2
-1
@@ -176,10 +176,11 @@ struct cache_entry {
176
/*
177
* Extended on-disk flags
178
*/
179
+#define CE_INTENT_TO_ADD 0x20000000
180
/* CE_EXTENDED2 is for future extension */
181
#define CE_EXTENDED2 0x80000000
182
182
-#define CE_EXTENDED_FLAGS (0)
183
+#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD)
184
185
/*
186
* Safeguard to avoid saving wrong flags:
read-cache.c
+2
@@ -546,6 +546,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
546
ce->ce_flags = namelen;
547
if (!intent_only)
548
fill_stat_cache_info(ce, st);
549
+ else
550
+ ce->ce_flags |= CE_INTENT_TO_ADD;
551
552
if (trust_executable_bit && has_symlinks)
553
ce->ce_mode = create_ce_mode(st_mode);
t/t3600-rm.sh
+2
-2
@@ -187,8 +187,8 @@ test_expect_success 'but with -f it should work.' '
187
test_must_fail git ls-files --error-unmatch baz
188
'
189
190
-test_expect_failure 'refuse to remove cached empty file with modifications' '
191
- touch empty &&
190
+test_expect_success 'refuse to remove cached empty file with modifications' '
191
+ >empty &&
192
git add empty &&
193
echo content >empty &&
194
test_must_fail git rm --cached empty