commit: don't be fooled by ita entries when creating initial commit

ita entries are dropped at tree generation phase. If the entire index consists of just ita entries, the result would be a a commit with no entries, which should be caught unless --allow-empty is specified. The test "!!active_nr" is not sufficient to catch this. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Oct 24, 2016 at 17:42 UTC 2c49f7ffb3063fdccccf2a038ab2eee66e7395e4
2 files changed +18 -3
builtin/commit.c
+8 -3
@@ -894,9 +894,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
894 if (amend)
895 parent = "HEAD^1";
896
897 - if (get_sha1(parent, sha1))
898 - commitable = !!active_nr;
899 - else {
897 + if (get_sha1(parent, sha1)) {
898 + int i, ita_nr = 0;
899 +
900 + for (i = 0; i < active_nr; i++)
901 + if (ce_intent_to_add(active_cache[i]))
902 + ita_nr++;
903 + commitable = active_nr - ita_nr > 0;
904 + } else {
905 /*
906 * Unless the user did explicitly request a submodule
907 * ignore mode by passing a command line option we do
t/t2203-add-intent.sh
+10
@@ -129,6 +129,16 @@ test_expect_success 'cache-tree does skip dir that becomes empty' '
129 )
130 '
131
132 +test_expect_success 'commit: ita entries ignored in empty intial commit check' '
133 + git init empty-intial-commit &&
134 + (
135 + cd empty-intial-commit &&
136 + : >one &&
137 + git add -N one &&
138 + test_must_fail git commit -m nothing-new-here
139 + )
140 +'
141 +
142 test_expect_success 'commit: ita entries ignored in empty commit check' '
143 git init empty-subsequent-commit &&
144 (