Skip to content

Commit

Permalink
git add --intent-to-add: do not let an empty blob be committed by acc…
Browse files Browse the repository at this point in the history
…ident

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]>
  • Loading branch information
gitster committed Dec 1, 2008
1 parent 388b2ac commit 331fcb5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion builtin-commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
active_cache_tree = cache_tree();
if (cache_tree_update(active_cache_tree,
active_cache, active_nr, 0, 0) < 0) {
error("Error building trees; the index is unmerged?");
error("Error building trees");
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion builtin-write-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
die("%s: error reading the index", me);
break;
case WRITE_TREE_UNMERGED_INDEX:
die("%s: error building trees; the index is unmerged?", me);
die("%s: error building trees", me);
break;
case WRITE_TREE_PREFIX_ERROR:
die("%s: prefix %s not found", me, prefix);
Expand Down
10 changes: 7 additions & 3 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ static int verify_cache(struct cache_entry **cache,
funny = 0;
for (i = 0; i < entries; i++) {
struct cache_entry *ce = cache[i];
if (ce_stage(ce)) {
if (ce_stage(ce) || (ce->ce_flags & CE_INTENT_TO_ADD)) {
if (10 < ++funny) {
fprintf(stderr, "...\n");
break;
}
fprintf(stderr, "%s: unmerged (%s)\n",
ce->name, sha1_to_hex(ce->sha1));
if (ce_stage(ce))
fprintf(stderr, "%s: unmerged (%s)\n",
ce->name, sha1_to_hex(ce->sha1));
else
fprintf(stderr, "%s: not added yet\n",
ce->name);
}
}
if (funny)
Expand Down
8 changes: 8 additions & 0 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ int ie_match_stat(const struct index_state *istate,
if (!ignore_valid && (ce->ce_flags & CE_VALID))
return 0;

/*
* Intent-to-add entries have not been added, so the index entry
* by definition never matches what is in the work tree until it
* actually gets added.
*/
if (ce->ce_flags & CE_INTENT_TO_ADD)
return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;

changed = ce_match_stat_basic(ce, st);

/*
Expand Down
28 changes: 28 additions & 0 deletions t/t2203-add-intent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,33 @@ test_expect_success 'intent to add does not clobber existing paths' '
! grep "$empty" actual
'

test_expect_success 'cannot commit with i-t-a entry' '
test_tick &&
git commit -a -m initial &&
git reset --hard &&
echo xyzzy >rezrov &&
echo frotz >nitfol &&
git add rezrov &&
git add -N nitfol &&
test_must_fail git commit
'

test_expect_success 'can commit with an unrelated i-t-a entry in index' '
git reset --hard &&
echo xyzzy >rezrov &&
echo frotz >nitfol &&
git add rezrov &&
git add -N nitfol &&
git commit -m partial rezrov
'

test_expect_success 'can "commit -a" with an i-t-a entry' '
git reset --hard &&
: >nitfol &&
git add -N nitfol &&
git commit -a -m all
'

test_done

0 comments on commit 331fcb5

Please sign in to comment.