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()
This commit is contained in:
Junio C Hamano 2020-03-09 11:21:19 -07:00
commit 0e0d717537
8 changed files with 140 additions and 39 deletions

View File

@ -16,7 +16,7 @@ SYNOPSIS
[--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet]
[--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>]
[(<mbox> | <Maildir>)...]
'git am' (--continue | --skip | --abort | --quit | --show-current-patch)
'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)])
DESCRIPTION
-----------
@ -176,9 +176,11 @@ default. You can use `--no-utf8` to override this.
Abort the patching operation but keep HEAD and the index
untouched.
--show-current-patch::
Show the entire e-mail message "git am" has stopped at, because
of conflicts.
--show-current-patch[=(diff|raw)]::
Show the message at which `git am` has stopped due to
conflicts. If `raw` is specified, show the raw contents of
the e-mail message; if `diff`, show the diff portion only.
Defaults to `raw`.
DISCUSSION
----------

View File

@ -81,6 +81,11 @@ enum signoff_type {
SIGNOFF_EXPLICIT /* --signoff was set on the command-line */
};
enum show_patch_type {
SHOW_PATCH_RAW = 0,
SHOW_PATCH_DIFF = 1,
};
struct am_state {
/* state directory path */
char *dir;
@ -1763,7 +1768,7 @@ static void am_run(struct am_state *state, int resume)
linelen(state->msg), state->msg);
if (advice_amworkdir)
advise(_("Use 'git am --show-current-patch' to see the failed patch"));
advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
die_user_resolve(state);
}
@ -2061,7 +2066,7 @@ static void am_abort(struct am_state *state)
am_destroy(state);
}
static int show_patch(struct am_state *state)
static int show_patch(struct am_state *state, enum show_patch_type sub_mode)
{
struct strbuf sb = STRBUF_INIT;
const char *patch_path;
@ -2078,7 +2083,17 @@ static int show_patch(struct am_state *state)
return ret;
}
patch_path = am_path(state, msgnum(state));
switch (sub_mode) {
case SHOW_PATCH_RAW:
patch_path = am_path(state, msgnum(state));
break;
case SHOW_PATCH_DIFF:
patch_path = am_path(state, "patch");
break;
default:
BUG("invalid mode for --show-current-patch");
}
len = strbuf_read_file(&sb, patch_path, 0);
if (len < 0)
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
return 0;
}
enum resume_mode {
enum resume_type {
RESUME_FALSE = 0,
RESUME_APPLY,
RESUME_RESOLVED,
@ -2128,6 +2143,45 @@ enum resume_mode {
RESUME_SHOW_PATCH
};
struct resume_mode {
enum resume_type mode;
enum show_patch_type sub_mode;
};
static int parse_opt_show_current_patch(const struct option *opt, const char *arg, int unset)
{
int *opt_value = opt->value;
struct resume_mode *resume = container_of(opt_value, struct resume_mode, mode);
/*
* Please update $__git_showcurrentpatch in git-completion.bash
* when you add new options
*/
const char *valid_modes[] = {
[SHOW_PATCH_DIFF] = "diff",
[SHOW_PATCH_RAW] = "raw"
};
int new_value = SHOW_PATCH_RAW;
if (arg) {
for (new_value = 0; new_value < ARRAY_SIZE(valid_modes); new_value++) {
if (!strcmp(arg, valid_modes[new_value]))
break;
}
if (new_value >= ARRAY_SIZE(valid_modes))
return error(_("Invalid value for --show-current-patch: %s"), arg);
}
if (resume->mode == RESUME_SHOW_PATCH && new_value != resume->sub_mode)
return error(_("--show-current-patch=%s is incompatible with "
"--show-current-patch=%s"),
arg, valid_modes[resume->sub_mode]);
resume->mode = RESUME_SHOW_PATCH;
resume->sub_mode = new_value;
return 0;
}
static int git_am_config(const char *k, const char *v, void *cb)
{
int status;
@ -2145,7 +2199,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
int binary = -1;
int keep_cr = -1;
int patch_format = PATCH_FORMAT_UNKNOWN;
enum resume_mode resume = RESUME_FALSE;
struct resume_mode resume = { .mode = RESUME_FALSE };
int in_progress;
int ret = 0;
@ -2214,24 +2268,26 @@ int cmd_am(int argc, const char **argv, const char *prefix)
PARSE_OPT_NOARG),
OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
N_("override error message when patch failure occurs")),
OPT_CMDMODE(0, "continue", &resume,
OPT_CMDMODE(0, "continue", &resume.mode,
N_("continue applying patches after resolving a conflict"),
RESUME_RESOLVED),
OPT_CMDMODE('r', "resolved", &resume,
OPT_CMDMODE('r', "resolved", &resume.mode,
N_("synonyms for --continue"),
RESUME_RESOLVED),
OPT_CMDMODE(0, "skip", &resume,
OPT_CMDMODE(0, "skip", &resume.mode,
N_("skip the current patch"),
RESUME_SKIP),
OPT_CMDMODE(0, "abort", &resume,
OPT_CMDMODE(0, "abort", &resume.mode,
N_("restore the original branch and abort the patching operation."),
RESUME_ABORT),
OPT_CMDMODE(0, "quit", &resume,
OPT_CMDMODE(0, "quit", &resume.mode,
N_("abort the patching operation but keep HEAD where it is."),
RESUME_QUIT),
OPT_CMDMODE(0, "show-current-patch", &resume,
N_("show the patch being applied."),
RESUME_SHOW_PATCH),
{ OPTION_CALLBACK, 0, "show-current-patch", &resume.mode,
"(diff|raw)",
N_("show the patch being applied"),
PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
parse_opt_show_current_patch, RESUME_SHOW_PATCH },
OPT_BOOL(0, "committer-date-is-author-date",
&state.committer_date_is_author_date,
N_("lie about committer date")),
@ -2281,12 +2337,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
* intend to feed us a patch but wanted to continue
* unattended.
*/
if (argc || (resume == RESUME_FALSE && !isatty(0)))
if (argc || (resume.mode == RESUME_FALSE && !isatty(0)))
die(_("previous rebase directory %s still exists but mbox given."),
state.dir);
if (resume == RESUME_FALSE)
resume = RESUME_APPLY;
if (resume.mode == RESUME_FALSE)
resume.mode = RESUME_APPLY;
if (state.signoff == SIGNOFF_EXPLICIT)
am_append_signoff(&state);
@ -2300,7 +2356,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
* stray directories.
*/
if (file_exists(state.dir) && !state.rebasing) {
if (resume == RESUME_ABORT || resume == RESUME_QUIT) {
if (resume.mode == RESUME_ABORT || resume.mode == RESUME_QUIT) {
am_destroy(&state);
am_state_release(&state);
return 0;
@ -2311,7 +2367,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
state.dir);
}
if (resume)
if (resume.mode)
die(_("Resolve operation not in progress, we are not resuming."));
for (i = 0; i < argc; i++) {
@ -2329,7 +2385,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
argv_array_clear(&paths);
}
switch (resume) {
switch (resume.mode) {
case RESUME_FALSE:
am_run(&state, 0);
break;
@ -2350,7 +2406,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
am_destroy(&state);
break;
case RESUME_SHOW_PATCH:
ret = show_patch(&state);
ret = show_patch(&state, resume.sub_mode);
break;
default:
BUG("invalid resume value");

View File

@ -1197,6 +1197,7 @@ __git_count_arguments ()
__git_whitespacelist="nowarn warn error error-all fix"
__git_patchformat="mbox stgit stgit-series hg mboxrd"
__git_showcurrentpatch="diff raw"
__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
_git_am ()
@ -1215,6 +1216,10 @@ _git_am ()
__gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
return
;;
--show-current-patch=*)
__gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
return
;;
--*)
__gitcomp_builtin am "" \
"$__git_am_inprogress_options"

View File

@ -61,7 +61,7 @@ static enum parse_opt_result opt_command_mode_error(
*/
for (that = all_opts; that->type != OPTION_END; that++) {
if (that == opt ||
that->type != OPTION_CMDMODE ||
!(that->flags & PARSE_OPT_CMDMODE) ||
that->value != opt->value ||
that->defval != *(int *)opt->value)
continue;
@ -95,6 +95,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
return error(_("%s takes no value"), optname(opt, flags));
/*
* Giving the same mode option twice, although unnecessary,
* is not a grave error, so let it pass.
*/
if ((opt->flags & PARSE_OPT_CMDMODE) &&
*(int *)opt->value && *(int *)opt->value != opt->defval)
return opt_command_mode_error(opt, all_opts, flags);
switch (opt->type) {
case OPTION_LOWLEVEL_CALLBACK:
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,
*(int *)opt->value = unset ? 0 : opt->defval;
return 0;
case OPTION_CMDMODE:
/*
* Giving the same mode option twice, although is unnecessary,
* is not a grave error, so let it pass.
*/
if (*(int *)opt->value && *(int *)opt->value != opt->defval)
return opt_command_mode_error(opt, all_opts, flags);
*(int *)opt->value = opt->defval;
return 0;
case OPTION_STRING:
if (unset)
*(const char **)opt->value = NULL;

View File

@ -18,7 +18,6 @@ enum parse_opt_type {
OPTION_BITOP,
OPTION_COUNTUP,
OPTION_SET_INT,
OPTION_CMDMODE,
/* options with arguments (usually) */
OPTION_STRING,
OPTION_INTEGER,
@ -47,7 +46,8 @@ enum parse_opt_option_flags {
PARSE_OPT_LITERAL_ARGHELP = 64,
PARSE_OPT_SHELL_EVAL = 256,
PARSE_OPT_NOCOMPLETE = 512,
PARSE_OPT_COMP_ARG = 1024
PARSE_OPT_COMP_ARG = 1024,
PARSE_OPT_CMDMODE = 2048
};
enum parse_opt_result {
@ -168,8 +168,8 @@ struct option {
#define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
#define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
(h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
#define OPT_CMDMODE(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
(h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
#define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
#define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
N_("n"), (h), PARSE_OPT_NONEG }

View File

@ -121,6 +121,8 @@ int cmd__parse_options(int argc, const char **argv)
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
OPT_CALLBACK('L', "length", &integer, "str",
"get length of <str>", length_callback),
OPT_FILENAME('F', "file", &file, "set file to <file>"),

View File

@ -23,6 +23,8 @@ usage: test-tool parse-options <options>
-j <n> get a integer, too
-m, --magnitude <n> get a magnitude
--set23 set integer to 23
--mode1 set integer to 1 (cmdmode option)
--mode2 set integer to 2 (cmdmode option)
-L, --length <str> get length of <str>
-F, --file <file> set file to <file>
@ -324,6 +326,22 @@ test_expect_success 'OPT_NEGBIT() works' '
test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
'
test_expect_success 'OPT_CMDMODE() works' '
test-tool parse-options --expect="integer: 1" --mode1
'
test_expect_success 'OPT_CMDMODE() detects incompatibility' '
test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
test_must_be_empty output &&
test_i18ngrep "incompatible with --mode" output.err
'
test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' '
test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
test_must_be_empty output &&
test_i18ngrep "incompatible with something else" output.err
'
test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
test-tool parse-options --expect="boolean: 6" + + + + + +
'

View File

@ -666,6 +666,26 @@ test_expect_success 'am --show-current-patch' '
test_cmp .git/rebase-apply/0001 actual.patch
'
test_expect_success 'am --show-current-patch=raw' '
git am --show-current-patch=raw >actual.patch &&
test_cmp .git/rebase-apply/0001 actual.patch
'
test_expect_success 'am --show-current-patch=diff' '
git am --show-current-patch=diff >actual.patch &&
test_cmp .git/rebase-apply/patch actual.patch
'
test_expect_success 'am accepts repeated --show-current-patch' '
git am --show-current-patch --show-current-patch=raw >actual.patch &&
test_cmp .git/rebase-apply/0001 actual.patch
'
test_expect_success 'am detects incompatible --show-current-patch' '
test_must_fail git am --show-current-patch=raw --show-current-patch=diff &&
test_must_fail git am --show-current-patch --show-current-patch=diff
'
test_expect_success 'am --skip works' '
echo goodbye >expected &&
git am --skip &&