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) <aleen42@vip.qq.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

徐沛文 (Aleen) committed Dec 9, 2021 at 07:25 UTC 7c096b8d61e1276acb6f84bf446cfe74578382a0
3 files changed +111 -5
Documentation/git-am.txt
+9
@@ -16,6 +16,7 @@ SYNOPSIS
16 [--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet]
17 [--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>]
18 [--quoted-cr=<action>]
19 + [--empty=(stop|drop|keep)]
20 [(<mbox> | <Maildir>)...]
21 'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)])
22
@@ -63,6 +64,14 @@ OPTIONS
64 --quoted-cr=<action>::
65 This flag will be passed down to 'git mailinfo' (see linkgit:git-mailinfo[1]).
66
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 +
75 -m::
76 --message-id::
77 Pass the `-m` flag to 'git mailinfo' (see linkgit:git-mailinfo[1]),
builtin/am.c
+52 -5
@@ -87,6 +87,12 @@ enum show_patch_type {
87 SHOW_PATCH_DIFF = 1,
88 };
89
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 +
96 struct am_state {
97 /* state directory path */
98 char *dir;
@@ -118,6 +124,7 @@ struct am_state {
124 int message_id;
125 int scissors; /* enum scissors_type */
126 int quoted_cr; /* enum quoted_cr_action */
127 + int empty_type; /* enum empty_action */
128 struct strvec git_apply_opts;
129 const char *resolvemsg;
130 int committer_date_is_author_date;
@@ -178,6 +185,25 @@ static int am_option_parse_quoted_cr(const struct option *opt,
185 return 0;
186 }
187
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 +
207 /**
208 * Returns path relative to the am_state directory.
209 */
@@ -1248,11 +1274,6 @@ static int parse_mail(struct am_state *state, const char *mail)
1274 goto finish;
1275 }
1276
1251 - if (is_empty_or_missing_file(am_path(state, "patch"))) {
1252 - printf_ln(_("Patch is empty."));
1253 - die_user_resolve(state);
1254 - }
1255 -
1277 strbuf_addstr(&msg, "\n\n");
1278 strbuf_addbuf(&msg, &mi.log_message);
1279 strbuf_stripspace(&msg, 0);
@@ -1763,6 +1784,7 @@ static void am_run(struct am_state *state, int resume)
1784 while (state->cur <= state->last) {
1785 const char *mail = am_path(state, msgnum(state));
1786 int apply_status;
1787 + int to_keep;
1788
1789 reset_ident_date();
1790
@@ -1792,8 +1814,29 @@ static void am_run(struct am_state *state, int resume)
1814 if (state->interactive && do_interactive(state))
1815 goto next;
1816
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 +
1836 if (run_applypatch_msg_hook(state))
1837 exit(1);
1838 + if (to_keep)
1839 + goto commit;
1840
1841 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1842
@@ -1827,6 +1870,7 @@ static void am_run(struct am_state *state, int resume)
1870 die_user_resolve(state);
1871 }
1872
1873 +commit:
1874 do_commit(state);
1875
1876 next:
@@ -2357,6 +2401,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2401 { OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
2402 N_("GPG-sign commits"),
2403 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),
2407 OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
2408 N_("(internal use for git-rebase)")),
2409 OPT_END()
t/t4150-am.sh
+50
@@ -196,6 +196,12 @@ test_expect_success setup '
196
197 git format-patch -M --stdout lorem^ >rename-add.patch &&
198
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 +
205 # reset time
206 sane_unset test_tick &&
207 test_tick
@@ -1152,4 +1158,48 @@ test_expect_success 'apply binary blob in partial clone' '
1158 git -C client am ../patch
1159 '
1160
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 +
1205 test_done