Skip to content

Commit 0e0d717

Browse files
committed
Merge branch 'pb/am-show-current-patch'
"git am --short-current-patch" is a way to show the piece of e-mail for the stopped step, which is not suitable to directly feed "git apply" (it is designed to be a good "git am" input). It learned a new option to show only the patch part. * pb/am-show-current-patch: am: support --show-current-patch=diff to retrieve .git/rebase-apply/patch am: support --show-current-patch=raw as a synonym for--show-current-patch am: convert "resume" variable to a struct parse-options: convert "command mode" to a flag parse-options: add testcases for OPT_CMDMODE()
2 parents 9b7f726 + aa416b2 commit 0e0d717

File tree

8 files changed

+140
-39
lines changed

8 files changed

+140
-39
lines changed

Documentation/git-am.txt

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

2121
DESCRIPTION
2222
-----------
@@ -176,9 +176,11 @@ default. You can use `--no-utf8` to override this.
176176
Abort the patching operation but keep HEAD and the index
177177
untouched.
178178

179-
--show-current-patch::
180-
Show the entire e-mail message "git am" has stopped at, because
181-
of conflicts.
179+
--show-current-patch[=(diff|raw)]::
180+
Show the message at which `git am` has stopped due to
181+
conflicts. If `raw` is specified, show the raw contents of
182+
the e-mail message; if `diff`, show the diff portion only.
183+
Defaults to `raw`.
182184

183185
DISCUSSION
184186
----------

builtin/am.c

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ enum signoff_type {
8181
SIGNOFF_EXPLICIT /* --signoff was set on the command-line */
8282
};
8383

84+
enum show_patch_type {
85+
SHOW_PATCH_RAW = 0,
86+
SHOW_PATCH_DIFF = 1,
87+
};
88+
8489
struct am_state {
8590
/* state directory path */
8691
char *dir;
@@ -1763,7 +1768,7 @@ static void am_run(struct am_state *state, int resume)
17631768
linelen(state->msg), state->msg);
17641769

17651770
if (advice_amworkdir)
1766-
advise(_("Use 'git am --show-current-patch' to see the failed patch"));
1771+
advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
17671772

17681773
die_user_resolve(state);
17691774
}
@@ -2061,7 +2066,7 @@ static void am_abort(struct am_state *state)
20612066
am_destroy(state);
20622067
}
20632068

2064-
static int show_patch(struct am_state *state)
2069+
static int show_patch(struct am_state *state, enum show_patch_type sub_mode)
20652070
{
20662071
struct strbuf sb = STRBUF_INIT;
20672072
const char *patch_path;
@@ -2078,7 +2083,17 @@ static int show_patch(struct am_state *state)
20782083
return ret;
20792084
}
20802085

2081-
patch_path = am_path(state, msgnum(state));
2086+
switch (sub_mode) {
2087+
case SHOW_PATCH_RAW:
2088+
patch_path = am_path(state, msgnum(state));
2089+
break;
2090+
case SHOW_PATCH_DIFF:
2091+
patch_path = am_path(state, "patch");
2092+
break;
2093+
default:
2094+
BUG("invalid mode for --show-current-patch");
2095+
}
2096+
20822097
len = strbuf_read_file(&sb, patch_path, 0);
20832098
if (len < 0)
20842099
die_errno(_("failed to read '%s'"), patch_path);
@@ -2118,7 +2133,7 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
21182133
return 0;
21192134
}
21202135

2121-
enum resume_mode {
2136+
enum resume_type {
21222137
RESUME_FALSE = 0,
21232138
RESUME_APPLY,
21242139
RESUME_RESOLVED,
@@ -2128,6 +2143,45 @@ enum resume_mode {
21282143
RESUME_SHOW_PATCH
21292144
};
21302145

2146+
struct resume_mode {
2147+
enum resume_type mode;
2148+
enum show_patch_type sub_mode;
2149+
};
2150+
2151+
static int parse_opt_show_current_patch(const struct option *opt, const char *arg, int unset)
2152+
{
2153+
int *opt_value = opt->value;
2154+
struct resume_mode *resume = container_of(opt_value, struct resume_mode, mode);
2155+
2156+
/*
2157+
* Please update $__git_showcurrentpatch in git-completion.bash
2158+
* when you add new options
2159+
*/
2160+
const char *valid_modes[] = {
2161+
[SHOW_PATCH_DIFF] = "diff",
2162+
[SHOW_PATCH_RAW] = "raw"
2163+
};
2164+
int new_value = SHOW_PATCH_RAW;
2165+
2166+
if (arg) {
2167+
for (new_value = 0; new_value < ARRAY_SIZE(valid_modes); new_value++) {
2168+
if (!strcmp(arg, valid_modes[new_value]))
2169+
break;
2170+
}
2171+
if (new_value >= ARRAY_SIZE(valid_modes))
2172+
return error(_("Invalid value for --show-current-patch: %s"), arg);
2173+
}
2174+
2175+
if (resume->mode == RESUME_SHOW_PATCH && new_value != resume->sub_mode)
2176+
return error(_("--show-current-patch=%s is incompatible with "
2177+
"--show-current-patch=%s"),
2178+
arg, valid_modes[resume->sub_mode]);
2179+
2180+
resume->mode = RESUME_SHOW_PATCH;
2181+
resume->sub_mode = new_value;
2182+
return 0;
2183+
}
2184+
21312185
static int git_am_config(const char *k, const char *v, void *cb)
21322186
{
21332187
int status;
@@ -2145,7 +2199,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
21452199
int binary = -1;
21462200
int keep_cr = -1;
21472201
int patch_format = PATCH_FORMAT_UNKNOWN;
2148-
enum resume_mode resume = RESUME_FALSE;
2202+
struct resume_mode resume = { .mode = RESUME_FALSE };
21492203
int in_progress;
21502204
int ret = 0;
21512205

@@ -2214,24 +2268,26 @@ int cmd_am(int argc, const char **argv, const char *prefix)
22142268
PARSE_OPT_NOARG),
22152269
OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
22162270
N_("override error message when patch failure occurs")),
2217-
OPT_CMDMODE(0, "continue", &resume,
2271+
OPT_CMDMODE(0, "continue", &resume.mode,
22182272
N_("continue applying patches after resolving a conflict"),
22192273
RESUME_RESOLVED),
2220-
OPT_CMDMODE('r', "resolved", &resume,
2274+
OPT_CMDMODE('r', "resolved", &resume.mode,
22212275
N_("synonyms for --continue"),
22222276
RESUME_RESOLVED),
2223-
OPT_CMDMODE(0, "skip", &resume,
2277+
OPT_CMDMODE(0, "skip", &resume.mode,
22242278
N_("skip the current patch"),
22252279
RESUME_SKIP),
2226-
OPT_CMDMODE(0, "abort", &resume,
2280+
OPT_CMDMODE(0, "abort", &resume.mode,
22272281
N_("restore the original branch and abort the patching operation."),
22282282
RESUME_ABORT),
2229-
OPT_CMDMODE(0, "quit", &resume,
2283+
OPT_CMDMODE(0, "quit", &resume.mode,
22302284
N_("abort the patching operation but keep HEAD where it is."),
22312285
RESUME_QUIT),
2232-
OPT_CMDMODE(0, "show-current-patch", &resume,
2233-
N_("show the patch being applied."),
2234-
RESUME_SHOW_PATCH),
2286+
{ OPTION_CALLBACK, 0, "show-current-patch", &resume.mode,
2287+
"(diff|raw)",
2288+
N_("show the patch being applied"),
2289+
PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
2290+
parse_opt_show_current_patch, RESUME_SHOW_PATCH },
22352291
OPT_BOOL(0, "committer-date-is-author-date",
22362292
&state.committer_date_is_author_date,
22372293
N_("lie about committer date")),
@@ -2281,12 +2337,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
22812337
* intend to feed us a patch but wanted to continue
22822338
* unattended.
22832339
*/
2284-
if (argc || (resume == RESUME_FALSE && !isatty(0)))
2340+
if (argc || (resume.mode == RESUME_FALSE && !isatty(0)))
22852341
die(_("previous rebase directory %s still exists but mbox given."),
22862342
state.dir);
22872343

2288-
if (resume == RESUME_FALSE)
2289-
resume = RESUME_APPLY;
2344+
if (resume.mode == RESUME_FALSE)
2345+
resume.mode = RESUME_APPLY;
22902346

22912347
if (state.signoff == SIGNOFF_EXPLICIT)
22922348
am_append_signoff(&state);
@@ -2300,7 +2356,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23002356
* stray directories.
23012357
*/
23022358
if (file_exists(state.dir) && !state.rebasing) {
2303-
if (resume == RESUME_ABORT || resume == RESUME_QUIT) {
2359+
if (resume.mode == RESUME_ABORT || resume.mode == RESUME_QUIT) {
23042360
am_destroy(&state);
23052361
am_state_release(&state);
23062362
return 0;
@@ -2311,7 +2367,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23112367
state.dir);
23122368
}
23132369

2314-
if (resume)
2370+
if (resume.mode)
23152371
die(_("Resolve operation not in progress, we are not resuming."));
23162372

23172373
for (i = 0; i < argc; i++) {
@@ -2329,7 +2385,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23292385
argv_array_clear(&paths);
23302386
}
23312387

2332-
switch (resume) {
2388+
switch (resume.mode) {
23332389
case RESUME_FALSE:
23342390
am_run(&state, 0);
23352391
break;
@@ -2350,7 +2406,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23502406
am_destroy(&state);
23512407
break;
23522408
case RESUME_SHOW_PATCH:
2353-
ret = show_patch(&state);
2409+
ret = show_patch(&state, resume.sub_mode);
23542410
break;
23552411
default:
23562412
BUG("invalid resume value");

contrib/completion/git-completion.bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ __git_count_arguments ()
11971197

11981198
__git_whitespacelist="nowarn warn error error-all fix"
11991199
__git_patchformat="mbox stgit stgit-series hg mboxrd"
1200+
__git_showcurrentpatch="diff raw"
12001201
__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
12011202

12021203
_git_am ()
@@ -1215,6 +1216,10 @@ _git_am ()
12151216
__gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
12161217
return
12171218
;;
1219+
--show-current-patch=*)
1220+
__gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
1221+
return
1222+
;;
12181223
--*)
12191224
__gitcomp_builtin am "" \
12201225
"$__git_am_inprogress_options"

parse-options.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static enum parse_opt_result opt_command_mode_error(
6161
*/
6262
for (that = all_opts; that->type != OPTION_END; that++) {
6363
if (that == opt ||
64-
that->type != OPTION_CMDMODE ||
64+
!(that->flags & PARSE_OPT_CMDMODE) ||
6565
that->value != opt->value ||
6666
that->defval != *(int *)opt->value)
6767
continue;
@@ -95,6 +95,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
9595
if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
9696
return error(_("%s takes no value"), optname(opt, flags));
9797

98+
/*
99+
* Giving the same mode option twice, although unnecessary,
100+
* is not a grave error, so let it pass.
101+
*/
102+
if ((opt->flags & PARSE_OPT_CMDMODE) &&
103+
*(int *)opt->value && *(int *)opt->value != opt->defval)
104+
return opt_command_mode_error(opt, all_opts, flags);
105+
98106
switch (opt->type) {
99107
case OPTION_LOWLEVEL_CALLBACK:
100108
return opt->ll_callback(p, opt, NULL, unset);
@@ -130,16 +138,6 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
130138
*(int *)opt->value = unset ? 0 : opt->defval;
131139
return 0;
132140

133-
case OPTION_CMDMODE:
134-
/*
135-
* Giving the same mode option twice, although is unnecessary,
136-
* is not a grave error, so let it pass.
137-
*/
138-
if (*(int *)opt->value && *(int *)opt->value != opt->defval)
139-
return opt_command_mode_error(opt, all_opts, flags);
140-
*(int *)opt->value = opt->defval;
141-
return 0;
142-
143141
case OPTION_STRING:
144142
if (unset)
145143
*(const char **)opt->value = NULL;

parse-options.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ enum parse_opt_type {
1818
OPTION_BITOP,
1919
OPTION_COUNTUP,
2020
OPTION_SET_INT,
21-
OPTION_CMDMODE,
2221
/* options with arguments (usually) */
2322
OPTION_STRING,
2423
OPTION_INTEGER,
@@ -47,7 +46,8 @@ enum parse_opt_option_flags {
4746
PARSE_OPT_LITERAL_ARGHELP = 64,
4847
PARSE_OPT_SHELL_EVAL = 256,
4948
PARSE_OPT_NOCOMPLETE = 512,
50-
PARSE_OPT_COMP_ARG = 1024
49+
PARSE_OPT_COMP_ARG = 1024,
50+
PARSE_OPT_CMDMODE = 2048
5151
};
5252

5353
enum parse_opt_result {
@@ -168,8 +168,8 @@ struct option {
168168
#define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
169169
#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
170170
(h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
171-
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
172-
(h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
171+
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
172+
(h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
173173
#define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
174174
#define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
175175
N_("n"), (h), PARSE_OPT_NONEG }

t/helper/test-parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ int cmd__parse_options(int argc, const char **argv)
121121
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
122122
OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
123123
OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
124+
OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
125+
OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
124126
OPT_CALLBACK('L', "length", &integer, "str",
125127
"get length of <str>", length_callback),
126128
OPT_FILENAME('F', "file", &file, "set file to <file>"),

t/t0040-parse-options.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ usage: test-tool parse-options <options>
2323
-j <n> get a integer, too
2424
-m, --magnitude <n> get a magnitude
2525
--set23 set integer to 23
26+
--mode1 set integer to 1 (cmdmode option)
27+
--mode2 set integer to 2 (cmdmode option)
2628
-L, --length <str> get length of <str>
2729
-F, --file <file> set file to <file>
2830
@@ -324,6 +326,22 @@ test_expect_success 'OPT_NEGBIT() works' '
324326
test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
325327
'
326328

329+
test_expect_success 'OPT_CMDMODE() works' '
330+
test-tool parse-options --expect="integer: 1" --mode1
331+
'
332+
333+
test_expect_success 'OPT_CMDMODE() detects incompatibility' '
334+
test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
335+
test_must_be_empty output &&
336+
test_i18ngrep "incompatible with --mode" output.err
337+
'
338+
339+
test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' '
340+
test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
341+
test_must_be_empty output &&
342+
test_i18ngrep "incompatible with something else" output.err
343+
'
344+
327345
test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
328346
test-tool parse-options --expect="boolean: 6" + + + + + +
329347
'

t/t4150-am.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,26 @@ test_expect_success 'am --show-current-patch' '
666666
test_cmp .git/rebase-apply/0001 actual.patch
667667
'
668668

669+
test_expect_success 'am --show-current-patch=raw' '
670+
git am --show-current-patch=raw >actual.patch &&
671+
test_cmp .git/rebase-apply/0001 actual.patch
672+
'
673+
674+
test_expect_success 'am --show-current-patch=diff' '
675+
git am --show-current-patch=diff >actual.patch &&
676+
test_cmp .git/rebase-apply/patch actual.patch
677+
'
678+
679+
test_expect_success 'am accepts repeated --show-current-patch' '
680+
git am --show-current-patch --show-current-patch=raw >actual.patch &&
681+
test_cmp .git/rebase-apply/0001 actual.patch
682+
'
683+
684+
test_expect_success 'am detects incompatible --show-current-patch' '
685+
test_must_fail git am --show-current-patch=raw --show-current-patch=diff &&
686+
test_must_fail git am --show-current-patch --show-current-patch=diff
687+
'
688+
669689
test_expect_success 'am --skip works' '
670690
echo goodbye >expected &&
671691
git am --skip &&

0 commit comments

Comments
 (0)