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

Re: [PATCH v17 3/3] am: support --allow-empty to record specific empty patches

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 7, 2021, 18:23 UTC
Message-ID
<xmqqpmq88eai.fsf@gitster.g>
In-Reply-To
<ea2dc088b37670cd7969a57777798b8f5bdd94e4.1638865913.git.gitgitgadget@gmail.com>

""徐沛文 (Aleen)" via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 39 quoted lines
> From: =?UTF-8?q?=E5=BE=90=E6=B2=9B=E6=96=87=20=28Aleen=29?=
>  <aleen42@vip.qq.com>
>
> This option helps to record specific empty patches in the middle
> of an am session. However, it is a valid resume value only when:
>
>     1. index has not changed
>     2. lacking a branch
>
> Signed-off-by: 徐沛文 (Aleen) <aleen42@vip.qq.com>
> ---
>  Documentation/git-am.txt |  7 +++++-
>  builtin/am.c             | 37 +++++++++++++++++++++-------
>  t/t4150-am.sh            | 53 ++++++++++++++++++++++++++++++++++++++++
>  t/t7512-status-help.sh   |  1 +
>  wt-status.c              |  3 +++
>  5 files changed, 91 insertions(+), 10 deletions(-)
>
> diff --git a/Documentation/git-am.txt b/Documentation/git-am.txt
> index 7676bd58ae7..8d3aa552a20 100644
> --- a/Documentation/git-am.txt
> +++ b/Documentation/git-am.txt
> @@ -18,7 +18,7 @@ SYNOPSIS
>  	 [--quoted-cr=<action>]
>  	 [--empty=(stop|drop|keep)]
>  	 [(<mbox> | <Maildir>)...]
> -'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)])
> +'git am' (--continue | --skip | --abort | --quit | --show-current-patch[=(diff|raw)] | --allow-empty)
>  
>  DESCRIPTION
>  -----------
> @@ -200,6 +200,11 @@ default.   You can use `--no-utf8` to override this.
>  	the e-mail message; if `diff`, show the diff portion only.
>  	Defaults to `raw`.
>  
> +--allow-empty::
> +	After a patch failure on an input e-mail message lacking a patch,
> +	the user can still record the empty patch as an empty commit with
> +	the contents of the e-mail message as its log.

"The user can still do X" is not technically incorrect, but this is not merely a possiblity but does actively does X, so

	After a patch failure on an input e-mail message lacking a patch,
	create an empty commit with the contents of the e-mail
	message as its log message.
or something.
Show 11 quoted lines
> diff --git a/builtin/am.c b/builtin/am.c
> index f45aa33366f..89a29ccac89 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -1825,7 +1825,8 @@ static void am_run(struct am_state *state, int resume)
>  				to_keep = 1;
>  				break;
>  			case STOP_ON_EMPTY_COMMIT:
> -				printf_ln(_("Patch is empty."));
> +				printf_ln(_("Patch is empty.\n"
> +					    "If you want to record it as an empty commit, run \"git am --allow-empty\"."));

It would be better to hide the new part of the message behind advice_if_enabled(), and advise "--skip" first before giving "--allow-empty" as another possibility. It is rather a common mistake to grab patches with the cover letter and see "am" die on the cover letter.

If we were to add an advice message there, that is. I am not sure if it is necessary, especially since we give them in "git status".

Show 23 quoted lines
> @@ -1898,21 +1899,34 @@ next:
>  /**
>   * Resume the current am session after patch application failure. The user did
>   * all the hard work, and we do not have to do any patch application. Just
> - * trust and commit what the user has in the index and working tree.
> + * trust and commit what the user has in the index and working tree. If `allow_empty`
> + * is true, commit as an empty commit when index has not changed and lacking a patch.
>   */
> -static void am_resolve(struct am_state *state)
> +static void am_resolve(struct am_state *state, int allow_empty)
>  {
> +	int index_changed;
> +
>  	validate_resume_state(state);
>  
>  	say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
>  
> -	if (!repo_index_has_changes(the_repository, NULL, NULL)) {
> -		printf_ln(_("No changes - did you forget to use 'git add'?\n"
> -			"If there is nothing left to stage, chances are that something else\n"
> -			"already introduced the same changes; you might want to skip this patch."));
> +	index_changed = repo_index_has_changes(the_repository, NULL, NULL);
> +	if (allow_empty && (index_changed || !is_empty_or_missing_file(am_path(state, "patch")))) {
Overlong line.
> +		printf_ln(_("Invalid resume value."));

It is unclear to the user what "resume value" means. I am guessing that this is saying that the user gave us the "--allow-empty" option, but it is an inappropriate option for the situation. So

	"--allow-empty is not appropriate in this situation"

would be a slight improvement, but it is not clear what makes it inappropriate, iow, what the situation is, to the users.

Stepping back a bit, if we disect that overlong condition:
	if (allow_empty &&
	    (index_changed || !is_empty_or_missing_file(am_path(state, "patch")))) {

it seems to be computing something not quite right. What we want with "allow" empty is (1) if we have recorded changes to the index, that is perfectly fine. We'll create a non-empty commit, and (2) if we do not have any change in the index because the input did not have a patch, it is OK to create an empty commit out of the current index. But the condition is there only to complain about "--allow-empty" given when either (1) we have changes, or (2) when the input had a patch. These are conditions that we can internally ignore allow_empty settings and let the existing code handle the message as before this patch.

>  		die_user_resolve(state);

And I do not see how "when you have resolved with this problem, do one of these things" message would help in this situation. Perhaps we can get rid of the above block. We still need to compute index_changed, and make "no changes--we will die" conditional.

> +	if (!index_changed) {
> +		if (allow_empty)
> +			printf_ln(_("No changes - record it as an empty commit."));

A command to the user to "record it as an empty commit" is not what we want to give. Perhaps "recorded it as ..." would work better as a report of what we did for the user.

Also style:
		if (allow_empty) {
			printf_ln(...);
		} else {
                	... original multi-statement block come here ...
		}

That is, when one arm of if/else if/.../else cascade needs {}, put {} to all of them.

I think there is a logic error here once we remove the way too aggressive "you may not say --allow-empty when there is a patch" check we saw earlier.

When we see no changes added to the index and "--allow-empty" given, we shouldn't be blindly creating an empty commit without checking if we didn't have any patch. If the last failure that gave the control back from "am" to the user was a patch that did not apply, we do not want to create an empty commit, do we? So something like

	if (!repo_index_has_changes(the_repository, NULL, NULL)) {
		if (allow_empty &&
		    is_empty_or_missing_file(am_path(state, "patch"))) {
			printf_ln(_("No changes - recorded an empty commit."));
		} else {
			... original block ...
perhaps?
Show 7 quoted lines
> +		else {
> +			printf_ln(_("No changes - did you forget to use 'git add'?\n"
> +				    "If there is nothing left to stage, chances are that something else\n"
> +				    "already introduced the same changes; you might want to skip this patch."));
> +			die_user_resolve(state);
> +		}
> +	}
Show 7 quoted lines
> @@ -2237,7 +2251,8 @@ enum resume_type {
>  	RESUME_SKIP,
>  	RESUME_ABORT,
>  	RESUME_QUIT,
> -	RESUME_SHOW_PATCH
> +	RESUME_SHOW_PATCH,
> +	RESUME_ALLOW_EMPTY
Advice on the trailing comma applies here equally.
>  };
Show 11 quoted lines
> diff --git a/t/t7512-status-help.sh b/t/t7512-status-help.sh
> index 7f2956d77ad..9309becfe03 100755
> --- a/t/t7512-status-help.sh
> +++ b/t/t7512-status-help.sh
> @@ -658,6 +658,7 @@ test_expect_success 'status in an am session: empty patch' '
>  On branch am_empty
>  You are in the middle of an am session.
>  The current patch is empty.
> +  (use "git am --allow-empty" to record this patch as an empty commit)
>    (use "git am --skip" to skip this patch)
>    (use "git am --abort" to restore the original branch)

You do not want to add the new one the first. "--skip" should come first, and perhaps this new one, and "--abort" at the end.

The general rule of thumb is to give common ones early before the less common ones and ones, and give ones with more severe consequence the last.

Show 14 quoted lines
> diff --git a/wt-status.c b/wt-status.c
> index 5d215f4e4f1..d578a0e9192 100644
> --- a/wt-status.c
> +++ b/wt-status.c
> @@ -1227,6 +1227,9 @@ static void show_am_in_progress(struct wt_status *s,
>  		if (!s->state.am_empty_patch)
>  			status_printf_ln(s, color,
>  				_("  (fix conflicts and then run \"git am --continue\")"));
> +		else
> +			status_printf_ln(s, color,
> +				_("  (use \"git am --allow-empty\" to record this patch as an empty commit)"));
>  		status_printf_ln(s, color,
>  			_("  (use \"git am --skip\" to skip this patch)"));
>  		status_printf_ln(s, color,
Previous: 徐沛文 (Aleen) via GitGitGadgetNext: Junio C Hamano
Message 113 of 129 in “am: support --always option to am empty commits”
  1. 0/2 am: support --always option to am empty commitsAleen via GitGitGadget, Nov 12, 2021
  2. 1/2 doc: git-format-patch: specify the option --alwaysAleen via GitGitGadget, Nov 12, 2021
  3. 2/2 am: support --always option to am empty commitsAleen via GitGitGadget, Nov 12, 2021
  4. René ScharfeNov 12, 2021
  5. Junio C HamanoNov 12, 2021
  6. Aleen 徐沛文Nov 12, 2021
  7. René ScharfeNov 12, 2021
  8. Junio C HamanoNov 12, 2021
  9. AleenNov 12, 2021
  10. 0/4 am: support --allow-empty option to am empty commitsAleen via GitGitGadget, Nov 12, 2021
  11. 1/4 doc: git-format-patch: specify the option --alwaysAleen via GitGitGadget, Nov 12, 2021
  12. Junio C HamanoNov 12, 2021
  13. 2/4 am: support --always option to am empty commitsAleen via GitGitGadget, Nov 12, 2021
  14. Junio C HamanoNov 12, 2021
  15. 3/4 test: am: add the case when not passing the --always optionAleen via GitGitGadget, Nov 12, 2021
  16. 4/4 chore: am: rename the --always option to --allow-emptyAleen via GitGitGadget, Nov 12, 2021
  17. 0/2 am: support --empty-commit=(die|skip|asis) option to am empty commitsAleen via GitGitGadget, Nov 15, 2021
  18. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 15, 2021
  19. 2/2 am: support --empty-commit option to handle empty patchesAleen via GitGitGadget, Nov 15, 2021
  20. Aleen 徐沛文Nov 15, 2021
  21. 0/2 am: support --empty-commit=(die|skip|asis) option to am empty commitsAleen via GitGitGadget, Nov 16, 2021
  22. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 16, 2021
  23. 2/2 am: support --empty-commit option to handle empty patchesAleen via GitGitGadget, Nov 16, 2021
  24. Phillip WoodNov 16, 2021
  25. Aleen 徐沛文Nov 16, 2021
  26. Junio C HamanoNov 17, 2021
  27. 0/2 am: support --empty-commit=(die|skip|asis) option to am empty commitsAleen via GitGitGadget, Nov 17, 2021
  28. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 17, 2021
  29. 2/2 am: support --empty option to handle empty patchesAleen via GitGitGadget, Nov 17, 2021
  30. 0/3 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 18, 2021
  31. 1/3 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 18, 2021
  32. 2/3 am: support --empty option to handle empty patchesAleen via GitGitGadget, Nov 18, 2021
  33. Junio C HamanoNov 19, 2021
  34. Bagas SanjayaNov 19, 2021
  35. Ævar Arnfjörð BjarmasonNov 19, 2021
  36. Eric SunshineNov 19, 2021
  37. Junio C HamanoNov 19, 2021
  38. Junio C HamanoNov 19, 2021
  39. 3/3 am: throw an error when passing --empty option without valueAleen via GitGitGadget, Nov 18, 2021
  40. Junio C HamanoNov 19, 2021
  41. Aleen 徐沛文Nov 19, 2021
  42. Junio C HamanoNov 18, 2021
  43. Aleen 徐沛文Nov 19, 2021
  44. Junio C HamanoNov 19, 2021
  45. Aleen 徐沛文Nov 19, 2021
  46. AleenNov 19, 2021
  47. Junio C HamanoNov 19, 2021
  48. Aleen 徐沛文Nov 19, 2021
  49. Junio C HamanoNov 19, 2021
  50. Johannes SchindelinNov 22, 2021
  51. Aleen 徐沛文Nov 19, 2021
  52. 0/2 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 19, 2021
  53. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 19, 2021
  54. 2/2 am: support --empty=<option> to handle empty patchesAleen via GitGitGadget, Nov 19, 2021
  55. Junio C HamanoNov 19, 2021
  56. Junio C HamanoNov 19, 2021
  57. 0/2 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 22, 2021
  58. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 22, 2021
  59. 2/2 am: support --empty=<option> to handle empty patchesAleen via GitGitGadget, Nov 22, 2021
  60. Junio C HamanoNov 22, 2021
  61. Aleen 徐沛文Nov 22, 2021
  62. 0/2 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 22, 2021
  63. 1/2 doc: git-format-patch: describe the option --alwaysAleen 徐沛文 via GitGitGadget, Nov 22, 2021
  64. 2/2 am: support --empty=<option> to handle empty patchesAleen 徐沛文 via GitGitGadget, Nov 22, 2021
  65. Aleen 徐沛文Nov 22, 2021
  66. 0/2 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 22, 2021
  67. 1/2 doc: git-format-patch: describe the option --alwaysAleen via GitGitGadget, Nov 22, 2021
  68. Johannes SchindelinNov 22, 2021
  69. Aleen 徐沛文Nov 23, 2021
  70. Johannes SchindelinNov 23, 2021
  71. 2/2 am: support --empty=<option> to handle empty patchesAleen via GitGitGadget, Nov 22, 2021
  72. 0/2 am: support --empty=(die|drop|keep) option to handle empty patchesAleen via GitGitGadget, Nov 23, 2021
  73. 1/2 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Nov 23, 2021
  74. Johannes SchindelinNov 23, 2021
  75. Junio C HamanoNov 23, 2021
  76. 2/2 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Nov 23, 2021
  77. Elijah NewrenNov 26, 2021
  78. Aleen 徐沛文Nov 29, 2021
  79. Aleen 徐沛文Nov 29, 2021
  80. Elijah NewrenNov 29, 2021
  81. Aleen 徐沛文Nov 30, 2021
  82. Junio C HamanoNov 29, 2021
  83. Elijah NewrenNov 29, 2021
  84. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Nov 30, 2021
  85. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  86. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  87. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  88. Junio C HamanoNov 30, 2021
  89. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Nov 30, 2021
  90. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  91. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  92. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Nov 30, 2021
  93. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 1, 2021
  94. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 1, 2021
  95. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 1, 2021
  96. Johannes SchindelinDec 3, 2021
  97. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 1, 2021
  98. Junio C HamanoDec 2, 2021
  99. Aleen 徐沛文Dec 6, 2021
  100. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 6, 2021
  101. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 6, 2021
  102. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 6, 2021
  103. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 6, 2021
  104. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 7, 2021
  105. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  106. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  107. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  108. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 7, 2021
  109. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  110. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  111. Junio C HamanoDec 7, 2021
  112. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 7, 2021
  113. Junio C HamanoDec 7, 2021
  114. Junio C HamanoDec 7, 2021
  115. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 8, 2021
  116. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 8, 2021
  117. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 8, 2021
  118. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 8, 2021
  119. Junio C HamanoDec 8, 2021
  120. Aleen 徐沛文Dec 8, 2021
  121. Junio C HamanoDec 8, 2021
  122. 0/3 am: support --empty=(die|drop|keep) option and --allow-empty option to handle empty patchesAleen via GitGitGadget, Dec 9, 2021
  123. 1/3 doc: git-format-patch: describe the option --always徐沛文 (Aleen) via GitGitGadget, Dec 9, 2021
  124. Bagas SanjayaDec 9, 2021
  125. Aleen 徐沛文Dec 10, 2021
  126. Bagas SanjayaDec 10, 2021
  127. Junio C HamanoDec 11, 2021
  128. 2/3 am: support --empty=<option> to handle empty patches徐沛文 (Aleen) via GitGitGadget, Dec 9, 2021
  129. 3/3 am: support --allow-empty to record specific empty patches徐沛文 (Aleen) via GitGitGadget, Dec 9, 2021

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.