Skip to content

Commit 331fcb5

Browse files
committed
git add --intent-to-add: do not let an empty blob be committed by accident
Writing a tree out of an index with an "intent to add" entry is blocked. This implies that you cannot "git commit" from such a state; however you can still do "git commit -a" or "git commit $that_path". Signed-off-by: Junio C Hamano <[email protected]>
1 parent 388b2ac commit 331fcb5

5 files changed

Lines changed: 45 additions & 5 deletions

File tree

builtin-commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
639639
active_cache_tree = cache_tree();
640640
if (cache_tree_update(active_cache_tree,
641641
active_cache, active_nr, 0, 0) < 0) {
642-
error("Error building trees; the index is unmerged?");
642+
error("Error building trees");
643643
return 0;
644644
}
645645

builtin-write-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
4242
die("%s: error reading the index", me);
4343
break;
4444
case WRITE_TREE_UNMERGED_INDEX:
45-
die("%s: error building trees; the index is unmerged?", me);
45+
die("%s: error building trees", me);
4646
break;
4747
case WRITE_TREE_PREFIX_ERROR:
4848
die("%s: prefix %s not found", me, prefix);

cache-tree.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,17 @@ static int verify_cache(struct cache_entry **cache,
155155
funny = 0;
156156
for (i = 0; i < entries; i++) {
157157
struct cache_entry *ce = cache[i];
158-
if (ce_stage(ce)) {
158+
if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
159159
if (10 < ++funny) {
160160
fprintf(stderr, "...\n");
161161
break;
162162
}
163-
fprintf(stderr, "%s: unmerged (%s)\n",
164-
ce->name, sha1_to_hex(ce->sha1));
163+
if (ce_stage(ce))
164+
fprintf(stderr, "%s: unmerged (%s)\n",
165+
ce->name, sha1_to_hex(ce->sha1));
166+
else
167+
fprintf(stderr, "%s: not added yet\n",
168+
ce->name);
165169
}
166170
}
167171
if (funny)

read-cache.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ int ie_match_stat(const struct index_state *istate,
257257
if (!ignore_valid && (ce->ce_flags & CE_VALID))
258258
return 0;
259259

260+
/*
261+
* Intent-to-add entries have not been added, so the index entry
262+
* by definition never matches what is in the work tree until it
263+
* actually gets added.
264+
*/
265+
if (ce->ce_flags & CE_INTENT_TO_ADD)
266+
return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
267+
260268
changed = ce_match_stat_basic(ce, st);
261269

262270
/*

t/t2203-add-intent.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,33 @@ test_expect_success 'intent to add does not clobber existing paths' '
3232
! grep "$empty" actual
3333
'
3434

35+
test_expect_success 'cannot commit with i-t-a entry' '
36+
test_tick &&
37+
git commit -a -m initial &&
38+
git reset --hard &&
39+
40+
echo xyzzy >rezrov &&
41+
echo frotz >nitfol &&
42+
git add rezrov &&
43+
git add -N nitfol &&
44+
test_must_fail git commit
45+
'
46+
47+
test_expect_success 'can commit with an unrelated i-t-a entry in index' '
48+
git reset --hard &&
49+
echo xyzzy >rezrov &&
50+
echo frotz >nitfol &&
51+
git add rezrov &&
52+
git add -N nitfol &&
53+
git commit -m partial rezrov
54+
'
55+
56+
test_expect_success 'can "commit -a" with an i-t-a entry' '
57+
git reset --hard &&
58+
: >nitfol &&
59+
git add -N nitfol &&
60+
git commit -a -m all
61+
'
62+
3563
test_done
3664

0 commit comments

Comments
 (0)