am: add --show-current-patch
Pointing the user to $GIT_DIR/rebase-apply may encourage them to mess around in there, which is not a good thing. With this, the user does not have to keep the path around somewhere (because after a couple of commands, the path may be out of scrollback buffer) when they need to look at the patch. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Feb 11, 2018 at 16:43 UTC
984913a210bb6d17fb3dd515975930661a00c3b5
4 files changed
+39
-6
Documentation/git-am.txt
+5
-1
@@ -16,7 +16,7 @@ SYNOPSIS
16
[--exclude=<path>] [--include=<path>] [--reject] [-q | --quiet]
17
[--[no-]scissors] [-S[<keyid>]] [--patch-format=<format>]
18
[(<mbox> | <Maildir>)...]
19
-'git am' (--continue | --skip | --abort)
19
+'git am' (--continue | --skip | --abort | --show-current-patch)
20
21
DESCRIPTION
22
-----------
@@ -167,6 +167,10 @@ default. You can use `--no-utf8` to override this.
167
--abort::
168
Restore the original branch and abort the patching operation.
169
170
+--show-current-patch::
171
+ Show the patch being applied when "git am" is stopped because
172
+ of conflicts.
173
+
174
DISCUSSION
175
----------
176
builtin/am.c
+28
-4
@@ -1831,8 +1831,7 @@ static void am_run(struct am_state *state, int resume)
1831
git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1832
1833
if (advice_amworkdir)
1834
- printf_ln(_("The copy of the patch that failed is found in: %s"),
1835
- am_path(state, "patch"));
1834
+ printf_ln(_("Use 'git am --show-current-patch' to see the failed patch"));
1835
1836
die_user_resolve(state);
1837
}
@@ -2121,6 +2120,23 @@ static void am_abort(struct am_state *state)
2120
am_destroy(state);
2121
}
2122
2123
+static int show_patch(struct am_state *state)
2124
+{
2125
+ struct strbuf sb = STRBUF_INIT;
2126
+ const char *patch_path;
2127
+ int len;
2128
+
2129
+ patch_path = am_path(state, msgnum(state));
2130
+ len = strbuf_read_file(&sb, patch_path, 0);
2131
+ if (len < 0)
2132
+ die_errno(_("failed to read '%s'"), patch_path);
2133
+
2134
+ setup_pager();
2135
+ write_in_full(1, sb.buf, sb.len);
2136
+ strbuf_release(&sb);
2137
+ return 0;
2138
+}
2139
+
2140
/**
2141
* parse_options() callback that validates and sets opt->value to the
2142
* PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -2149,7 +2165,8 @@ enum resume_mode {
2165
RESUME_APPLY,
2166
RESUME_RESOLVED,
2167
RESUME_SKIP,
2152
- RESUME_ABORT
2168
+ RESUME_ABORT,
2169
+ RESUME_SHOW_PATCH
2170
};
2171
2172
static int git_am_config(const char *k, const char *v, void *cb)
@@ -2171,6 +2188,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2188
int patch_format = PATCH_FORMAT_UNKNOWN;
2189
enum resume_mode resume = RESUME_FALSE;
2190
int in_progress;
2191
+ int ret = 0;
2192
2193
const char * const usage[] = {
2194
N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
@@ -2249,6 +2267,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2267
OPT_CMDMODE(0, "abort", &resume,
2268
N_("restore the original branch and abort the patching operation."),
2269
RESUME_ABORT),
2270
+ OPT_CMDMODE(0, "show-current-patch", &resume,
2271
+ N_("show the patch being applied."),
2272
+ RESUME_SHOW_PATCH),
2273
OPT_BOOL(0, "committer-date-is-author-date",
2274
&state.committer_date_is_author_date,
2275
N_("lie about committer date")),
@@ -2359,11 +2380,14 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2380
case RESUME_ABORT:
2381
am_abort(&state);
2382
break;
2383
+ case RESUME_SHOW_PATCH:
2384
+ ret = show_patch(&state);
2385
+ break;
2386
default:
2387
die("BUG: invalid resume value");
2388
}
2389
2390
am_state_release(&state);
2391
2368
- return 0;
2392
+ return ret;
2393
}
contrib/completion/git-completion.bash
+1
-1
@@ -1077,7 +1077,7 @@ _git_am ()
1077
{
1078
__git_find_repo_path
1079
if [ -d "$__git_repo_path"/rebase-apply ]; then
1080
- __gitcomp "--skip --continue --resolved --abort"
1080
+ __gitcomp "--skip --continue --resolved --abort --show-current-patch"
1081
return
1082
fi
1083
case "$cur" in
t/t4150-am.sh
+5
@@ -662,6 +662,11 @@ test_expect_success 'am pauses on conflict' '
662
test -d .git/rebase-apply
663
'
664
665
+test_expect_success 'am --show-current-patch' '
666
+ git am --show-current-patch >actual.patch &&
667
+ test_cmp .git/rebase-apply/0001 actual.patch
668
+'
669
+
670
test_expect_success 'am --skip works' '
671
echo goodbye >expected &&
672
git am --skip &&