Skip to content

Commit 7c096b8

Browse files
aleen42gitster
authored andcommitted
am: support --empty=<option> to handle empty patches
Since that the command 'git-format-patch' can include patches of commits that emit no changes, the 'git-am' command should also support an option, named as '--empty', to specify how to handle those empty patches. In this commit, we have implemented three valid options ('stop', 'drop' and 'keep'). Signed-off-by: 徐沛文 (Aleen) <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 552038e commit 7c096b8

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

Documentation/git-am.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SYNOPSIS
1616
[--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet]
1717
[--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>]
1818
[--quoted-cr=<action>]
19+
[--empty=(stop|drop|keep)]
1920
[(<mbox> | <Maildir>)...]
2021
'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)])
2122

@@ -63,6 +64,14 @@ OPTIONS
6364
--quoted-cr=<action>::
6465
This flag will be passed down to 'git mailinfo' (see linkgit:git-mailinfo[1]).
6566

67+
--empty=(stop|drop|keep)::
68+
By default, or when the option is set to 'stop', the command
69+
errors out on an input e-mail message lacking a patch
70+
and stops into the middle of the current am session. When this
71+
option is set to 'drop', skip such an e-mail message instead.
72+
When this option is set to 'keep', create an empty commit,
73+
recording the contents of the e-mail message as its log.
74+
6675
-m::
6776
--message-id::
6877
Pass the `-m` flag to 'git mailinfo' (see linkgit:git-mailinfo[1]),

builtin/am.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ enum show_patch_type {
8787
SHOW_PATCH_DIFF = 1,
8888
};
8989

90+
enum empty_action {
91+
STOP_ON_EMPTY_COMMIT = 0, /* output errors and stop in the middle of an am session */
92+
DROP_EMPTY_COMMIT, /* skip with a notice message, unless "--quiet" has been passed */
93+
KEEP_EMPTY_COMMIT, /* keep recording as empty commits */
94+
};
95+
9096
struct am_state {
9197
/* state directory path */
9298
char *dir;
@@ -118,6 +124,7 @@ struct am_state {
118124
int message_id;
119125
int scissors; /* enum scissors_type */
120126
int quoted_cr; /* enum quoted_cr_action */
127+
int empty_type; /* enum empty_action */
121128
struct strvec git_apply_opts;
122129
const char *resolvemsg;
123130
int committer_date_is_author_date;
@@ -178,6 +185,25 @@ static int am_option_parse_quoted_cr(const struct option *opt,
178185
return 0;
179186
}
180187

188+
static int am_option_parse_empty(const struct option *opt,
189+
const char *arg, int unset)
190+
{
191+
int *opt_value = opt->value;
192+
193+
BUG_ON_OPT_NEG(unset);
194+
195+
if (!strcmp(arg, "stop"))
196+
*opt_value = STOP_ON_EMPTY_COMMIT;
197+
else if (!strcmp(arg, "drop"))
198+
*opt_value = DROP_EMPTY_COMMIT;
199+
else if (!strcmp(arg, "keep"))
200+
*opt_value = KEEP_EMPTY_COMMIT;
201+
else
202+
return error(_("Invalid value for --empty: %s"), arg);
203+
204+
return 0;
205+
}
206+
181207
/**
182208
* Returns path relative to the am_state directory.
183209
*/
@@ -1248,11 +1274,6 @@ static int parse_mail(struct am_state *state, const char *mail)
12481274
goto finish;
12491275
}
12501276

1251-
if (is_empty_or_missing_file(am_path(state, "patch"))) {
1252-
printf_ln(_("Patch is empty."));
1253-
die_user_resolve(state);
1254-
}
1255-
12561277
strbuf_addstr(&msg, "\n\n");
12571278
strbuf_addbuf(&msg, &mi.log_message);
12581279
strbuf_stripspace(&msg, 0);
@@ -1763,6 +1784,7 @@ static void am_run(struct am_state *state, int resume)
17631784
while (state->cur <= state->last) {
17641785
const char *mail = am_path(state, msgnum(state));
17651786
int apply_status;
1787+
int to_keep;
17661788

17671789
reset_ident_date();
17681790

@@ -1792,8 +1814,29 @@ static void am_run(struct am_state *state, int resume)
17921814
if (state->interactive && do_interactive(state))
17931815
goto next;
17941816

1817+
to_keep = 0;
1818+
if (is_empty_or_missing_file(am_path(state, "patch"))) {
1819+
switch (state->empty_type) {
1820+
case DROP_EMPTY_COMMIT:
1821+
say(state, stdout, _("Skipping: %.*s"), linelen(state->msg), state->msg);
1822+
goto next;
1823+
break;
1824+
case KEEP_EMPTY_COMMIT:
1825+
to_keep = 1;
1826+
say(state, stdout, _("Creating an empty commit: %.*s"),
1827+
linelen(state->msg), state->msg);
1828+
break;
1829+
case STOP_ON_EMPTY_COMMIT:
1830+
printf_ln(_("Patch is empty."));
1831+
die_user_resolve(state);
1832+
break;
1833+
}
1834+
}
1835+
17951836
if (run_applypatch_msg_hook(state))
17961837
exit(1);
1838+
if (to_keep)
1839+
goto commit;
17971840

17981841
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
17991842

@@ -1827,6 +1870,7 @@ static void am_run(struct am_state *state, int resume)
18271870
die_user_resolve(state);
18281871
}
18291872

1873+
commit:
18301874
do_commit(state);
18311875

18321876
next:
@@ -2357,6 +2401,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23572401
{ OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
23582402
N_("GPG-sign commits"),
23592403
PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
2404+
OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT, "empty", &state.empty_type, "{stop,drop,keep}",
2405+
N_("how to handle empty patches"),
2406+
PARSE_OPT_NONEG, am_option_parse_empty),
23602407
OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
23612408
N_("(internal use for git-rebase)")),
23622409
OPT_END()

t/t4150-am.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ test_expect_success setup '
196196
197197
git format-patch -M --stdout lorem^ >rename-add.patch &&
198198
199+
git checkout -b empty-commit &&
200+
git commit -m "empty commit" --allow-empty &&
201+
202+
: >empty.patch &&
203+
git format-patch --always --stdout empty-commit^ >empty-commit.patch &&
204+
199205
# reset time
200206
sane_unset test_tick &&
201207
test_tick
@@ -1152,4 +1158,48 @@ test_expect_success 'apply binary blob in partial clone' '
11521158
git -C client am ../patch
11531159
'
11541160

1161+
test_expect_success 'an empty input file is error regardless of --empty option' '
1162+
test_when_finished "git am --abort || :" &&
1163+
test_must_fail git am --empty=drop empty.patch 2>actual &&
1164+
echo "Patch format detection failed." >expected &&
1165+
test_cmp expected actual
1166+
'
1167+
1168+
test_expect_success 'invalid when passing the --empty option alone' '
1169+
test_when_finished "git am --abort || :" &&
1170+
git checkout empty-commit^ &&
1171+
test_must_fail git am --empty empty-commit.patch 2>err &&
1172+
echo "error: Invalid value for --empty: empty-commit.patch" >expected &&
1173+
test_cmp expected err
1174+
'
1175+
1176+
test_expect_success 'a message without a patch is an error (default)' '
1177+
test_when_finished "git am --abort || :" &&
1178+
test_must_fail git am empty-commit.patch >err &&
1179+
grep "Patch is empty" err
1180+
'
1181+
1182+
test_expect_success 'a message without a patch is an error where an explicit "--empty=stop" is given' '
1183+
test_when_finished "git am --abort || :" &&
1184+
test_must_fail git am --empty=stop empty-commit.patch >err &&
1185+
grep "Patch is empty." err
1186+
'
1187+
1188+
test_expect_success 'a message without a patch will be skipped when "--empty=drop" is given' '
1189+
git am --empty=drop empty-commit.patch >output &&
1190+
git rev-parse empty-commit^ >expected &&
1191+
git rev-parse HEAD >actual &&
1192+
test_cmp expected actual &&
1193+
grep "Skipping: empty commit" output
1194+
'
1195+
1196+
test_expect_success 'record as an empty commit when meeting e-mail message that lacks a patch' '
1197+
git am --empty=keep empty-commit.patch >output &&
1198+
test_path_is_missing .git/rebase-apply &&
1199+
git show empty-commit --format="%B" >expected &&
1200+
git show HEAD --format="%B" >actual &&
1201+
grep -f actual expected &&
1202+
grep "Creating an empty commit: empty commit" output
1203+
'
1204+
11551205
test_done

0 commit comments

Comments
 (0)