git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] am: add --show-patch

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 26, 2018, 09:55 UTC
Message-ID
<20180126095520.919-2-pclouds@gmail.com>
In-Reply-To
<20180126095520.919-1-pclouds@gmail.com>

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. Bonus point for automatic pager.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-am.txt               |  5 ++++-
 builtin/am.c                           | 23 ++++++++++++++++++++---
 contrib/completion/git-completion.bash |  2 +-
 t/t4150-am.sh                          |  5 +++++
 4 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-am.txt b/Documentation/git-am.txt
index 12879e4029..724095e921 100644
--- a/Documentation/git-am.txt
+++ b/Documentation/git-am.txt
@@ -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)
+'git am' (--continue | --skip | --abort | --show-patch)
 
 DESCRIPTION
 -----------
@@ -167,6 +167,9 @@ default.   You can use `--no-utf8` to override this.
 --abort::
 	Restore the original branch and abort the patching operation.
 
+--show-patch::
+	Show the patch being applied.
+
 DISCUSSION
 ----------
 
diff --git a/builtin/am.c b/builtin/am.c
index acfe9d3c8c..aaaf32886a 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1831,8 +1831,7 @@ static void am_run(struct am_state *state, int resume)
 			git_config_get_bool("advice.amworkdir", &advice_amworkdir);
 
 			if (advice_amworkdir)
-				printf_ln(_("The copy of the patch that failed is found in: %s"),
-						am_path(state, "patch"));
+				printf_ln(_("Use 'git am --show-patch' to see the failed patch"));
 
 			die_user_resolve(state);
 		}
@@ -2121,6 +2120,17 @@ static void am_abort(struct am_state *state)
 	am_destroy(state);
 }
 
+static void show_patch(struct am_state *state)
+{
+	struct strbuf sb = STRBUF_INIT;
+
+	if (strbuf_read_file(&sb, am_path(state, msgnum(state)), 0) >= 0) {
+		setup_pager();
+		write_in_full(1, sb.buf, sb.len);
+	}
+	strbuf_release(&sb);
+}
+
 /**
  * parse_options() callback that validates and sets opt->value to the
  * PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -2149,7 +2159,8 @@ enum resume_mode {
 	RESUME_APPLY,
 	RESUME_RESOLVED,
 	RESUME_SKIP,
-	RESUME_ABORT
+	RESUME_ABORT,
+	RESUME_SHOW_PATCH
 };
 
 static int git_am_config(const char *k, const char *v, void *cb)
@@ -2249,6 +2260,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
 		OPT_CMDMODE(0, "abort", &resume,
 			N_("restore the original branch and abort the patching operation."),
 			RESUME_ABORT),
+		OPT_CMDMODE(0, "show-patch", &resume,
+			N_("show the patch being applied."),
+			RESUME_SHOW_PATCH),
 		OPT_BOOL(0, "committer-date-is-author-date",
 			&state.committer_date_is_author_date,
 			N_("lie about committer date")),
@@ -2359,6 +2373,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
 	case RESUME_ABORT:
 		am_abort(&state);
 		break;
+	case RESUME_SHOW_PATCH:
+		show_patch(&state);
+		break;
 	default:
 		die("BUG: invalid resume value");
 	}
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3683c772c5..1e9105f6d5 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1077,7 +1077,7 @@ _git_am ()
 {
 	__git_find_repo_path
 	if [ -d "$__git_repo_path"/rebase-apply ]; then
-		__gitcomp "--skip --continue --resolved --abort"
+		__gitcomp "--skip --continue --resolved --abort --show-patch"
 		return
 	fi
 	case "$cur" in
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index 73b67b4280..eff6455f42 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -662,6 +662,11 @@ test_expect_success 'am pauses on conflict' '
 	test -d .git/rebase-apply
 '
 
+test_expect_success 'am --show-patch' '
+	git am --show-patch >actual.patch &&
+	test_cmp .git/rebase-apply/0001 actual.patch
+'
+
 test_expect_success 'am --skip works' '
 	echo goodbye >expected &&
 	git am --skip &&
-- 
2.16.1.205.g271f633410
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 2 of 40 in “Add "git rebase --show-patch"”
  1. 0/2 Add "git rebase --show-patch"Nguyễn Thái Ngọc Duy, Jan 26, 2018
  2. 1/2 am: add --show-patchNguyễn Thái Ngọc Duy, Jan 26, 2018
  3. 2/2 rebase: add --show-patchNguyễn Thái Ngọc Duy, Jan 26, 2018
  4. Phillip WoodJan 26, 2018
  5. Duy NguyenJan 26, 2018
  6. Phillip WoodJan 30, 2018
  7. Eric SunshineJan 26, 2018
  8. Duy NguyenJan 27, 2018
  9. Tim LandscheidtJan 26, 2018
  10. Duy NguyenJan 27, 2018
  11. Ævar Arnfjörð BjarmasonJan 27, 2018
  12. Johannes SchindelinJan 29, 2018
  13. Duy NguyenJan 30, 2018
  14. Johannes SchindelinJan 30, 2018
  15. Junio C HamanoJan 30, 2018
  16. Tim LandscheidtFeb 1, 2018
  17. 0/3 Add "git rebase --show-patch"Nguyễn Thái Ngọc Duy, Jan 31, 2018
  18. 1/3 am: add --show-current-patchNguyễn Thái Ngọc Duy, Jan 31, 2018
  19. Eric SunshineJan 31, 2018
  20. Junio C HamanoJan 31, 2018
  21. Duy NguyenFeb 2, 2018
  22. Eric SunshineFeb 2, 2018
  23. Duy NguyenFeb 2, 2018
  24. Junio C HamanoFeb 2, 2018
  25. gitignore.txt: elaborate shell glob syntaxNguyễn Thái Ngọc Duy, Jan 31, 2018
  26. Junio C HamanoJan 31, 2018
  27. Duy NguyenFeb 1, 2018
  28. 2/3 rebase: add --show-current-patchNguyễn Thái Ngọc Duy, Jan 31, 2018
  29. 3/3 rebase: introduce and use pseudo-ref ORIG_COMMITNguyễn Thái Ngọc Duy, Jan 31, 2018
  30. Junio C HamanoJan 31, 2018
  31. Duy NguyenFeb 1, 2018
  32. Junio C HamanoFeb 2, 2018
  33. Phillip WoodFeb 1, 2018
  34. 0/3 Add "git rebase --show-current-patch"Nguyễn Thái Ngọc Duy, Feb 11, 2018
  35. 1/3 am: add --show-current-patchNguyễn Thái Ngọc Duy, Feb 11, 2018
  36. 2/3 rebase: add --show-current-patchNguyễn Thái Ngọc Duy, Feb 11, 2018
  37. 3/3 rebase: introduce and use pseudo-ref REBASE_HEADNguyễn Thái Ngọc Duy, Feb 11, 2018
  38. Junio C HamanoFeb 22, 2018
  39. Tim LandscheidtFeb 22, 2018
  40. Duy NguyenFeb 23, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.