Skip to content

Commit ead6767

Browse files
committed
Merge branch 'xw/am-empty'
"git am" learns "--empty=(stop|drop|keep)" option to tweak what is done to a piece of e-mail without a patch in it. * xw/am-empty: am: support --allow-empty to record specific empty patches am: support --empty=<option> to handle empty patches doc: git-format-patch: describe the option --always
2 parents da81d47 + 9e7e41b commit ead6767

File tree

6 files changed

+211
-16
lines changed

6 files changed

+211
-16
lines changed

Documentation/git-am.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ 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>)...]
20-
'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)])
21+
'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)] | --allow-empty)
2122

2223
DESCRIPTION
2324
-----------
@@ -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]),
@@ -191,6 +200,11 @@ default. You can use `--no-utf8` to override this.
191200
the e-mail message; if `diff`, show the diff portion only.
192201
Defaults to `raw`.
193202

203+
--allow-empty::
204+
After a patch failure on an input e-mail message lacking a patch,
205+
create an empty commit with the contents of the e-mail message
206+
as its log message.
207+
194208
DISCUSSION
195209
----------
196210

Documentation/git-format-patch.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SYNOPSIS
1818
[-n | --numbered | -N | --no-numbered]
1919
[--start-number <n>] [--numbered-files]
2020
[--in-reply-to=<message id>] [--suffix=.<sfx>]
21-
[--ignore-if-in-upstream]
21+
[--ignore-if-in-upstream] [--always]
2222
[--cover-from-description=<mode>]
2323
[--rfc] [--subject-prefix=<subject prefix>]
2424
[(--reroll-count|-v) <n>]
@@ -192,6 +192,10 @@ will want to ensure that threading is disabled for `git send-email`.
192192
patches being generated, and any patch that matches is
193193
ignored.
194194

195+
--always::
196+
Include patches for commits that do not introduce any change,
197+
which are omitted by default.
198+
195199
--cover-from-description=<mode>::
196200
Controls which parts of the cover letter will be automatically
197201
populated using the branch's description.

builtin/am.c

Lines changed: 76 additions & 13 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
*/
@@ -1126,6 +1152,12 @@ static void NORETURN die_user_resolve(const struct am_state *state)
11261152

11271153
printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
11281154
printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
1155+
1156+
if (advice_enabled(ADVICE_AM_WORK_DIR) &&
1157+
is_empty_or_missing_file(am_path(state, "patch")) &&
1158+
!repo_index_has_changes(the_repository, NULL, NULL))
1159+
printf_ln(_("To record the empty patch as an empty commit, run \"%s --allow-empty\"."), cmdline);
1160+
11291161
printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
11301162
}
11311163

@@ -1248,11 +1280,6 @@ static int parse_mail(struct am_state *state, const char *mail)
12481280
goto finish;
12491281
}
12501282

1251-
if (is_empty_or_missing_file(am_path(state, "patch"))) {
1252-
printf_ln(_("Patch is empty."));
1253-
die_user_resolve(state);
1254-
}
1255-
12561283
strbuf_addstr(&msg, "\n\n");
12571284
strbuf_addbuf(&msg, &mi.log_message);
12581285
strbuf_stripspace(&msg, 0);
@@ -1763,6 +1790,7 @@ static void am_run(struct am_state *state, int resume)
17631790
while (state->cur <= state->last) {
17641791
const char *mail = am_path(state, msgnum(state));
17651792
int apply_status;
1793+
int to_keep;
17661794

17671795
reset_ident_date();
17681796

@@ -1792,8 +1820,29 @@ static void am_run(struct am_state *state, int resume)
17921820
if (state->interactive && do_interactive(state))
17931821
goto next;
17941822

1823+
to_keep = 0;
1824+
if (is_empty_or_missing_file(am_path(state, "patch"))) {
1825+
switch (state->empty_type) {
1826+
case DROP_EMPTY_COMMIT:
1827+
say(state, stdout, _("Skipping: %.*s"), linelen(state->msg), state->msg);
1828+
goto next;
1829+
break;
1830+
case KEEP_EMPTY_COMMIT:
1831+
to_keep = 1;
1832+
say(state, stdout, _("Creating an empty commit: %.*s"),
1833+
linelen(state->msg), state->msg);
1834+
break;
1835+
case STOP_ON_EMPTY_COMMIT:
1836+
printf_ln(_("Patch is empty."));
1837+
die_user_resolve(state);
1838+
break;
1839+
}
1840+
}
1841+
17951842
if (run_applypatch_msg_hook(state))
17961843
exit(1);
1844+
if (to_keep)
1845+
goto commit;
17971846

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

@@ -1827,6 +1876,7 @@ static void am_run(struct am_state *state, int resume)
18271876
die_user_resolve(state);
18281877
}
18291878

1879+
commit:
18301880
do_commit(state);
18311881

18321882
next:
@@ -1856,19 +1906,24 @@ static void am_run(struct am_state *state, int resume)
18561906
/**
18571907
* Resume the current am session after patch application failure. The user did
18581908
* all the hard work, and we do not have to do any patch application. Just
1859-
* trust and commit what the user has in the index and working tree.
1909+
* trust and commit what the user has in the index and working tree. If `allow_empty`
1910+
* is true, commit as an empty commit when index has not changed and lacking a patch.
18601911
*/
1861-
static void am_resolve(struct am_state *state)
1912+
static void am_resolve(struct am_state *state, int allow_empty)
18621913
{
18631914
validate_resume_state(state);
18641915

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

18671918
if (!repo_index_has_changes(the_repository, NULL, NULL)) {
1868-
printf_ln(_("No changes - did you forget to use 'git add'?\n"
1869-
"If there is nothing left to stage, chances are that something else\n"
1870-
"already introduced the same changes; you might want to skip this patch."));
1871-
die_user_resolve(state);
1919+
if (allow_empty && is_empty_or_missing_file(am_path(state, "patch"))) {
1920+
printf_ln(_("No changes - recorded it as an empty commit."));
1921+
} else {
1922+
printf_ln(_("No changes - did you forget to use 'git add'?\n"
1923+
"If there is nothing left to stage, chances are that something else\n"
1924+
"already introduced the same changes; you might want to skip this patch."));
1925+
die_user_resolve(state);
1926+
}
18721927
}
18731928

18741929
if (unmerged_cache()) {
@@ -2195,7 +2250,8 @@ enum resume_type {
21952250
RESUME_SKIP,
21962251
RESUME_ABORT,
21972252
RESUME_QUIT,
2198-
RESUME_SHOW_PATCH
2253+
RESUME_SHOW_PATCH,
2254+
RESUME_ALLOW_EMPTY,
21992255
};
22002256

22012257
struct resume_mode {
@@ -2348,6 +2404,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23482404
N_("show the patch being applied"),
23492405
PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
23502406
parse_opt_show_current_patch, RESUME_SHOW_PATCH },
2407+
OPT_CMDMODE(0, "allow-empty", &resume.mode,
2408+
N_("record the empty patch as an empty commit"),
2409+
RESUME_ALLOW_EMPTY),
23512410
OPT_BOOL(0, "committer-date-is-author-date",
23522411
&state.committer_date_is_author_date,
23532412
N_("lie about committer date")),
@@ -2357,6 +2416,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23572416
{ OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
23582417
N_("GPG-sign commits"),
23592418
PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
2419+
OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT, "empty", &state.empty_type, "{stop,drop,keep}",
2420+
N_("how to handle empty patches"),
2421+
PARSE_OPT_NONEG, am_option_parse_empty),
23602422
OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
23612423
N_("(internal use for git-rebase)")),
23622424
OPT_END()
@@ -2453,7 +2515,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
24532515
am_run(&state, 1);
24542516
break;
24552517
case RESUME_RESOLVED:
2456-
am_resolve(&state);
2518+
case RESUME_ALLOW_EMPTY:
2519+
am_resolve(&state, resume.mode == RESUME_ALLOW_EMPTY ? 1 : 0);
24572520
break;
24582521
case RESUME_SKIP:
24592522
am_skip(&state);

t/t4150-am.sh

Lines changed: 107 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,105 @@ 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+
1205+
test_expect_success 'skip an empty patch in the middle of an am session' '
1206+
git checkout empty-commit^ &&
1207+
test_must_fail git am empty-commit.patch >err &&
1208+
grep "Patch is empty." err &&
1209+
grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
1210+
git am --skip &&
1211+
test_path_is_missing .git/rebase-apply &&
1212+
git rev-parse empty-commit^ >expected &&
1213+
git rev-parse HEAD >actual &&
1214+
test_cmp expected actual
1215+
'
1216+
1217+
test_expect_success 'record an empty patch as an empty commit in the middle of an am session' '
1218+
git checkout empty-commit^ &&
1219+
test_must_fail git am empty-commit.patch >err &&
1220+
grep "Patch is empty." err &&
1221+
grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
1222+
git am --allow-empty >output &&
1223+
grep "No changes - recorded it as an empty commit." output &&
1224+
test_path_is_missing .git/rebase-apply &&
1225+
git show empty-commit --format="%B" >expected &&
1226+
git show HEAD --format="%B" >actual &&
1227+
grep -f actual expected
1228+
'
1229+
1230+
test_expect_success 'create an non-empty commit when the index IS changed though "--allow-empty" is given' '
1231+
git checkout empty-commit^ &&
1232+
test_must_fail git am empty-commit.patch >err &&
1233+
: >empty-file &&
1234+
git add empty-file &&
1235+
git am --allow-empty &&
1236+
git show empty-commit --format="%B" >expected &&
1237+
git show HEAD --format="%B" >actual &&
1238+
grep -f actual expected &&
1239+
git diff HEAD^..HEAD --name-only
1240+
'
1241+
1242+
test_expect_success 'cannot create empty commits when there is a clean index due to merge conflicts' '
1243+
test_when_finished "git am --abort || :" &&
1244+
git rev-parse HEAD >expected &&
1245+
test_must_fail git am seq.patch &&
1246+
test_must_fail git am --allow-empty >err &&
1247+
! grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
1248+
git rev-parse HEAD >actual &&
1249+
test_cmp actual expected
1250+
'
1251+
1252+
test_expect_success 'cannot create empty commits when there is unmerged index due to merge conflicts' '
1253+
test_when_finished "git am --abort || :" &&
1254+
git rev-parse HEAD >expected &&
1255+
test_must_fail git am -3 seq.patch &&
1256+
test_must_fail git am --allow-empty >err &&
1257+
! grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
1258+
git rev-parse HEAD >actual &&
1259+
test_cmp actual expected
1260+
'
1261+
11551262
test_done

t/t7512-status-help.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ On branch am_empty
659659
You are in the middle of an am session.
660660
The current patch is empty.
661661
(use "git am --skip" to skip this patch)
662+
(use "git am --allow-empty" to record this patch as an empty commit)
662663
(use "git am --abort" to restore the original branch)
663664
664665
nothing to commit (use -u to show untracked files)

wt-status.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,17 +1218,23 @@ static void show_merge_in_progress(struct wt_status *s,
12181218
static void show_am_in_progress(struct wt_status *s,
12191219
const char *color)
12201220
{
1221+
int am_empty_patch;
1222+
12211223
status_printf_ln(s, color,
12221224
_("You are in the middle of an am session."));
12231225
if (s->state.am_empty_patch)
12241226
status_printf_ln(s, color,
12251227
_("The current patch is empty."));
12261228
if (s->hints) {
1227-
if (!s->state.am_empty_patch)
1229+
am_empty_patch = s->state.am_empty_patch;
1230+
if (!am_empty_patch)
12281231
status_printf_ln(s, color,
12291232
_(" (fix conflicts and then run \"git am --continue\")"));
12301233
status_printf_ln(s, color,
12311234
_(" (use \"git am --skip\" to skip this patch)"));
1235+
if (am_empty_patch)
1236+
status_printf_ln(s, color,
1237+
_(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
12321238
status_printf_ln(s, color,
12331239
_(" (use \"git am --abort\" to restore the original branch)"));
12341240
}

0 commit comments

Comments
 (0)