Skip to content

Commit 6503602

Browse files
committed
Merge branch 'nd/ita-empty-commit'
When new paths were added by "git add -N" to the index, it was enough to circumvent the check by "git commit" to refrain from making an empty commit without "--allow-empty". The same logic prevented "git status" to show such a path as "new file" in the "Changes not staged for commit" section. * nd/ita-empty-commit: commit: don't be fooled by ita entries when creating initial commit commit: fix empty commit creation when there's no changes but ita entries diff: add --ita-[in]visible-in-index diff-lib: allow ita entries treated as "not yet exist in index"
2 parents cca3be6 + 2c49f7f commit 6503602

File tree

9 files changed

+89
-13
lines changed

9 files changed

+89
-13
lines changed

Documentation/diff-options.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,5 +572,13 @@ endif::git-format-patch[]
572572
--line-prefix=<prefix>::
573573
Prepend an additional prefix to every line of output.
574574

575+
--ita-invisible-in-index::
576+
By default entries added by "git add -N" appear as an existing
577+
empty file in "git diff" and a new file in "git diff --cached".
578+
This option makes the entry appear as a new file in "git diff"
579+
and non-existent in "git diff --cached". This option could be
580+
reverted with `--ita-visible-in-index`. Both options are
581+
experimental and could be removed in future.
582+
575583
For more detailed explanation on these common options, see also
576584
linkgit:gitdiffcore[7].

builtin/commit.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
894894
if (amend)
895895
parent = "HEAD^1";
896896

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 {
900905
/*
901906
* Unless the user did explicitly request a submodule
902907
* ignore mode by passing a command line option we do
@@ -910,7 +915,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
910915
if (ignore_submodule_arg &&
911916
!strcmp(ignore_submodule_arg, "all"))
912917
diff_flags |= DIFF_OPT_IGNORE_SUBMODULES;
913-
commitable = index_differs_from(parent, diff_flags);
918+
commitable = index_differs_from(parent, diff_flags, 1);
914919
}
915920
}
916921
strbuf_release(&committer_ident);

diff-lib.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
214214
!is_null_oid(&ce->oid),
215215
ce->name, 0);
216216
continue;
217+
} else if (revs->diffopt.ita_invisible_in_index &&
218+
ce_intent_to_add(ce)) {
219+
diff_addremove(&revs->diffopt, '+', ce->ce_mode,
220+
EMPTY_BLOB_SHA1_BIN, 0,
221+
ce->name, 0);
222+
continue;
217223
}
218224

219225
changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
@@ -379,6 +385,14 @@ static void do_oneway_diff(struct unpack_trees_options *o,
379385
struct rev_info *revs = o->unpack_data;
380386
int match_missing, cached;
381387

388+
/* i-t-a entries do not actually exist in the index */
389+
if (revs->diffopt.ita_invisible_in_index &&
390+
idx && ce_intent_to_add(idx)) {
391+
idx = NULL;
392+
if (!tree)
393+
return; /* nothing to diff.. */
394+
}
395+
382396
/* if the entry is not checked out, don't examine work tree */
383397
cached = o->index_only ||
384398
(idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
@@ -521,7 +535,8 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
521535
return 0;
522536
}
523537

524-
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)
525540
{
526541
struct rev_info rev;
527542
struct setup_revision_opt opt;
@@ -533,6 +548,7 @@ int index_differs_from(const char *def, int diff_flags)
533548
DIFF_OPT_SET(&rev.diffopt, QUICK);
534549
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
535550
rev.diffopt.flags |= diff_flags;
551+
rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
536552
run_diff_index(&rev, 1);
537553
if (rev.pending.alloc)
538554
free(rev.pending.objects);

diff.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3987,6 +3987,10 @@ int diff_opt_parse(struct diff_options *options,
39873987
return parse_submodule_opt(options, arg);
39883988
else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
39893989
return parse_ws_error_highlight_opt(options, arg);
3990+
else if (!strcmp(arg, "--ita-invisible-in-index"))
3991+
options->ita_invisible_in_index = 1;
3992+
else if (!strcmp(arg, "--ita-visible-in-index"))
3993+
options->ita_invisible_in_index = 0;
39903994

39913995
/* misc options */
39923996
else if (!strcmp(arg, "-z"))

diff.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ struct diff_options {
146146
int dirstat_permille;
147147
int setup;
148148
int abbrev;
149+
int ita_invisible_in_index;
149150
/* white-space error highlighting */
150151
#define WSEH_NEW 1
151152
#define WSEH_CONTEXT 2
@@ -360,7 +361,7 @@ extern int diff_result_code(struct diff_options *, int);
360361

361362
extern void diff_no_index(struct rev_info *, int, const char **);
362363

363-
extern int index_differs_from(const char *def, int diff_flags);
364+
extern int index_differs_from(const char *def, int diff_flags, int ita_invisible_in_index);
364365

365366
/*
366367
* Fill the contents of the filespec "df", respecting any textconv defined by

sequencer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
658658
unborn = get_sha1("HEAD", head);
659659
if (unborn)
660660
hashcpy(head, EMPTY_TREE_SHA1_BIN);
661-
if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0))
661+
if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0, 0))
662662
return error_dirty_index(opts);
663663
}
664664
discard_cache();
@@ -1312,7 +1312,7 @@ int sequencer_continue(struct replay_opts *opts)
13121312
if (res)
13131313
goto release_todo_list;
13141314
}
1315-
if (index_differs_from("HEAD", 0)) {
1315+
if (index_differs_from("HEAD", 0, 0)) {
13161316
res = error_dirty_index(opts);
13171317
goto release_todo_list;
13181318
}

t/t2203-add-intent.sh

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ test_description='Intent to add'
55
. ./test-lib.sh
66

77
test_expect_success 'intent to add' '
8+
test_commit 1 &&
9+
git rm 1.t &&
10+
echo hello >1.t &&
811
echo hello >file &&
912
echo hello >elif &&
1013
git add -N file &&
11-
git add elif
14+
git add elif &&
15+
git add -N 1.t
16+
'
17+
18+
test_expect_success 'git status' '
19+
git status --porcelain | grep -v actual >actual &&
20+
cat >expect <<-\EOF &&
21+
DA 1.t
22+
A elif
23+
A file
24+
EOF
25+
test_cmp expect actual
1226
'
1327

1428
test_expect_success 'check result of "add -N"' '
@@ -43,7 +57,9 @@ test_expect_success 'i-t-a entry is simply ignored' '
4357
git add -N nitfol &&
4458
git commit -m second &&
4559
test $(git ls-tree HEAD -- nitfol | wc -l) = 0 &&
46-
test $(git diff --name-only HEAD -- nitfol | wc -l) = 1
60+
test $(git diff --name-only HEAD -- nitfol | wc -l) = 1 &&
61+
test $(git diff --name-only --ita-invisible-in-index HEAD -- nitfol | wc -l) = 0 &&
62+
test $(git diff --name-only --ita-invisible-in-index -- nitfol | wc -l) = 1
4763
'
4864

4965
test_expect_success 'can commit with an unrelated i-t-a entry in index' '
@@ -113,5 +129,26 @@ test_expect_success 'cache-tree does skip dir that becomes empty' '
113129
)
114130
'
115131

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+
(
145+
cd empty-subsequent-commit &&
146+
test_commit one &&
147+
: >two &&
148+
git add -N two &&
149+
test_must_fail git commit -m nothing-new-here
150+
)
151+
'
152+
116153
test_done
117154

t/t7064-wtstatus-pv2.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ test_expect_success 'verify --intent-to-add output' '
246246
git add --intent-to-add intent1.add intent2.add &&
247247
248248
cat >expect <<-EOF &&
249-
1 AM N... 000000 100644 100644 $_z40 $EMPTY_BLOB intent1.add
250-
1 AM N... 000000 100644 100644 $_z40 $EMPTY_BLOB intent2.add
249+
1 .A N... 000000 000000 100644 $_z40 $_z40 intent1.add
250+
1 .A N... 000000 000000 100644 $_z40 $_z40 intent2.add
251251
EOF
252252
253253
git status --porcelain=v2 >actual &&

wt-status.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
438438

439439
switch (p->status) {
440440
case DIFF_STATUS_ADDED:
441-
die("BUG: worktree status add???");
441+
d->mode_worktree = p->two->mode;
442442
break;
443443

444444
case DIFF_STATUS_DELETED:
@@ -548,6 +548,7 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
548548
setup_revisions(0, NULL, &rev, NULL);
549549
rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
550550
DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
551+
rev.diffopt.ita_invisible_in_index = 1;
551552
if (!s->show_untracked_files)
552553
DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
553554
if (s->ignore_submodule_arg) {
@@ -571,6 +572,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
571572
setup_revisions(0, NULL, &rev, &opt);
572573

573574
DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
575+
rev.diffopt.ita_invisible_in_index = 1;
574576
if (s->ignore_submodule_arg) {
575577
handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
576578
} else {
@@ -606,6 +608,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
606608

607609
if (!ce_path_match(ce, &s->pathspec, NULL))
608610
continue;
611+
if (ce_intent_to_add(ce))
612+
continue;
609613
it = string_list_insert(&s->change, ce->name);
610614
d = it->util;
611615
if (!d) {
@@ -912,6 +916,7 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
912916

913917
init_revisions(&rev, NULL);
914918
DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
919+
rev.diffopt.ita_invisible_in_index = 1;
915920

916921
memset(&opt, 0, sizeof(opt));
917922
opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;

0 commit comments

Comments
 (0)