commit: fix empty commit creation when there's no changes but ita entries

If i-t-a entries are present and there is no change between the index and HEAD i-t-a entries, index_differs_from() still returns "dirty, new entries" (aka, the resulting commit is not empty), but cache-tree will skip i-t-a entries and produce the exact same tree of current commit. index_differs_from() is supposed to catch this so we can abort git-commit (unless --no-empty is specified). Update it to optionally ignore i-t-a entries when doing a diff between the index and HEAD so that it would return "no change" in this case and abort commit. 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 018ec3c8203ad1aee683840a580cfba7914419b6
5 files changed +18 -5
builtin/commit.c
+1 -1
@@ -910,7 +910,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
910 if (ignore_submodule_arg &&
911 !strcmp(ignore_submodule_arg, "all"))
912 diff_flags |= DIFF_OPT_IGNORE_SUBMODULES;
913 - commitable = index_differs_from(parent, diff_flags);
913 + commitable = index_differs_from(parent, diff_flags, 1);
914 }
915 }
916 strbuf_release(&committer_ident);
diff-lib.c
+3 -1
@@ -535,7 +535,8 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
535 return 0;
536 }
537
538 -int index_differs_from(const char *def, int diff_flags)
538 +int index_differs_from(const char *def, int diff_flags,
539 + int ita_invisible_in_index)
540 {
541 struct rev_info rev;
542 struct setup_revision_opt opt;
@@ -547,6 +548,7 @@ int index_differs_from(const char *def, int diff_flags)
548 DIFF_OPT_SET(&rev.diffopt, QUICK);
549 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
550 rev.diffopt.flags |= diff_flags;
551 + rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
552 run_diff_index(&rev, 1);
553 if (rev.pending.alloc)
554 free(rev.pending.objects);
diff.h
+1 -1
@@ -357,7 +357,7 @@ extern int diff_result_code(struct diff_options *, int);
357
358 extern void diff_no_index(struct rev_info *, int, const char **);
359
360 -extern int index_differs_from(const char *def, int diff_flags);
360 +extern int index_differs_from(const char *def, int diff_flags, int ita_invisible_in_index);
361
362 /*
363 * Fill the contents of the filespec "df", respecting any textconv defined by
sequencer.c
+2 -2
@@ -469,7 +469,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
469 unborn = get_sha1("HEAD", head);
470 if (unborn)
471 hashcpy(head, EMPTY_TREE_SHA1_BIN);
472 - if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0))
472 + if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0, 0))
473 return error_dirty_index(opts);
474 }
475 discard_cache();
@@ -1064,7 +1064,7 @@ static int sequencer_continue(struct replay_opts *opts)
1064 if (ret)
1065 return ret;
1066 }
1067 - if (index_differs_from("HEAD", 0))
1067 + if (index_differs_from("HEAD", 0, 0))
1068 return error_dirty_index(opts);
1069 todo_list = todo_list->next;
1070 return pick_commits(todo_list, opts);
t/t2203-add-intent.sh
+11
@@ -129,5 +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 commit check' '
133 + git init empty-subsequent-commit &&
134 + (
135 + cd empty-subsequent-commit &&
136 + test_commit one &&
137 + : >two &&
138 + git add -N two &&
139 + test_must_fail git commit -m nothing-new-here
140 + )
141 +'
142 +
143 test_done
144