@matheusbsbs / matheus-bagliano / commits / 331fcb598e
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Nov 28, 2008 at 19:56 UTC 331fcb598ec0127fd89c992361bc573dcd3a4a63
5 files changed +45 -5
builtin-commit.c
+1 -1
@@ -639,7 +639,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
639 active_cache_tree = cache_tree();
640 if (cache_tree_update(active_cache_tree,
641 active_cache, active_nr, 0, 0) < 0) {
642 - error("Error building trees; the index is unmerged?");
642 + error("Error building trees");
643 return 0;
644 }
645
builtin-write-tree.c
+1 -1
@@ -42,7 +42,7 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
42 die("%s: error reading the index", me);
43 break;
44 case WRITE_TREE_UNMERGED_INDEX:
45 - die("%s: error building trees; the index is unmerged?", me);
45 + die("%s: error building trees", me);
46 break;
47 case WRITE_TREE_PREFIX_ERROR:
48 die("%s: prefix %s not found", me, prefix);
cache-tree.c
+7 -3
@@ -155,13 +155,17 @@ static int verify_cache(struct cache_entry **cache,
155 funny = 0;
156 for (i = 0; i < entries; i++) {
157 struct cache_entry *ce = cache[i];
158 - if (ce_stage(ce)) {
158 + if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
159 if (10 < ++funny) {
160 fprintf(stderr, "...\n");
161 break;
162 }
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);
169 }
170 }
171 if (funny)
read-cache.c
+8
@@ -257,6 +257,14 @@ int ie_match_stat(const struct index_state *istate,
257 if (!ignore_valid && (ce->ce_flags & CE_VALID))
258 return 0;
259
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 +
268 changed = ce_match_stat_basic(ce, st);
269
270 /*
t/t2203-add-intent.sh
+28
@@ -32,5 +32,33 @@ test_expect_success 'intent to add does not clobber existing paths' '
32 ! grep "$empty" actual
33 '
34
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 +
63 test_done
64