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

Re: [PATCH] format-patch: learn to fill comment section of email from notes

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 23, 2010, 21:56 UTC
Message-ID
<7vmxyzehug.fsf@alter.siamese.dyndns.org>
In-Reply-To
<6cf9010742df96e0c68ef8adc1ab392c08525bc2.1266946262.git.trast@student.ethz.ch>
Thomas Rast <trast@student.ethz.ch> writes:
> Teach git-format-patch a new option --comment-notes (with config
> format.commentNotes) which takes a notes ref argument.  These notes
> are then added to the patch email between the --- separator and the
> diffstat/diff.
Hmmm, why do I find this an ugly hack?

You already have a nice "format_display_notes()" infrastructure to allow users to get notes from arbitrary notes namespaces, yet this limits the user to a single notes namespace. What was the infrastructure built for if not used in places like this uniformly?

If the answer is "because notes.displayref is a configuration and it is cumbersome to change every time", then I don't have a sympathy ;-) as that is exactly why I said config without command line override is a bad thing.

Show 16 quoted lines
> diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
> index 9674f9d..afe7e41 100644
> --- a/Documentation/git-format-patch.txt
> +++ b/Documentation/git-format-patch.txt
> @@ -198,6 +198,16 @@ you can use `--suffix=-patch` to get `0001-description-of-my-change-patch`.
>  	range are always formatted as creation patches, independently
>  	of this flag.
>  
> +--comment-notes=<ref>::
> +	Use the notes from <ref> to fill the comment section of the
> +	email, i.e., the part between the `\---` separator and the
> +	patch.  See linkgit:git-notes[1].
> ++
> +Warning: the code currently does not guard against a line in the notes
> +that starts with `diff`, which will be treated as the start of the
> +patch by 'git-am'.

It is customary to indent the material after --- by one place (or more) so it probably is a good idea to do that for notes as well, if we are going to put it after the three dash lines. Notice that diffstat output is already indented that way ;-).

Show 16 quoted lines
> @@ -457,6 +458,7 @@ int log_tree_diff_flush(struct rev_info *opt)
>  	}
>  
>  	if (opt->loginfo && !opt->no_commit_id) {
> +		const unsigned char *sha1 = opt->loginfo->commit->object.sha1;
>  		/* When showing a verbose header (i.e. log message),
>  		 * and not in --pretty=oneline format, we would want
>  		 * an extra newline between the end of log and the
> @@ -467,10 +469,20 @@ int log_tree_diff_flush(struct rev_info *opt)
>  		    opt->verbose_header &&
>  		    opt->commit_format != CMIT_FMT_ONELINE) {
>  			int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
> -			if ((pch & opt->diffopt.output_format) == pch)
> +			if ((pch & opt->diffopt.output_format) == pch
> +			    || (opt->commit_format == CMIT_FMT_EMAIL
> +				&& opt->show_notes))

This adds a lot of logic to a code that used to be simple "if we have more stuff to add after message, delimit with "---\n"". Perhaps the whole body of the "if" statement ll. 460- should be made into a static helper function with a single callsite that a clever compiler would inline.

Also I think you would emit "---" even if the commit in question does not happen to have note with this code.

I've attached a "how about doing it this way" weatherbaloon patch to be applied on top of this patch at the end---I didn't bother to indent the notes text nor change it to use format_display_notes(), but hopefully you will agree the code structure would be easier to follow this way.

Show 12 quoted lines
> diff --git a/pretty.c b/pretty.c
> index 6ba3da8..10d7812 100644
> --- a/pretty.c
> +++ b/pretty.c
> @@ -1095,7 +1095,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
>  	if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
>  		strbuf_addch(sb, '\n');
>  
> -	if (context->show_notes)
> +	if (context->show_notes && fmt != CMIT_FMT_EMAIL)
>  		format_display_notes(commit->object.sha1, sb, encoding,
>  				     NOTES_SHOW_HEADER | NOTES_INDENT);

This is a good fix to prevent notes from getting injected above the separator line, regardless of your series, I think.

 log-tree.c |   71 +++++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 47 insertions(+), 24 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index 9baf306..af65d33 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -445,6 +445,52 @@ void show_log(struct rev_info *opt)
 	strbuf_release(&msgbuf);
 }
 
+static void log_tree_after_message(struct rev_info *opt)
+{
+	const int pch = (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_PATCH);
+	struct strbuf sb = STRBUF_INIT;
+
+	/*
+	 * When showing a verbose header (i.e. log message),
+	 * and not in --pretty=oneline format, we would want
+	 * an extra newline between the end of log and the
+	 * output for readability.
+	 */
+	if (! ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
+	       opt->verbose_header &&
+	       opt->commit_format != CMIT_FMT_ONELINE) )
+		return;
+
+	/*
+	 * Prepare notes if any...
+	 */
+	if (opt->commit_format == CMIT_FMT_EMAIL && opt->show_notes)
+		format_note(NULL, opt->loginfo->commit->object.sha1,
+			    &sb, NULL, 0);
+
+	/*
+	 * Will we have something other than the message itself?  If
+	 * so we would need three-dashes.
+	 */
+	if (((pch & opt->diffopt.output_format) == pch) || sb.len)
+		printf("---");
+
+	/*
+	 * Then the promised newline...
+	 */
+	putchar('\n');
+
+	/*
+	 * And finally the notes...
+	 */
+	if (sb.len) {
+		putchar('\n');
+		fwrite(sb.buf, 1, sb.len, stdout);
+		strbuf_release(&sb);
+		putchar('\n');
+	}
+}
+
 int log_tree_diff_flush(struct rev_info *opt)
 {
 	diffcore_std(&opt->diffopt);
@@ -458,31 +504,8 @@ int log_tree_diff_flush(struct rev_info *opt)
 	}
 
 	if (opt->loginfo && !opt->no_commit_id) {
-		const unsigned char *sha1 = opt->loginfo->commit->object.sha1;
-		/* When showing a verbose header (i.e. log message),
-		 * and not in --pretty=oneline format, we would want
-		 * an extra newline between the end of log and the
-		 * output for readability.
-		 */
 		show_log(opt);
-		if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
-		    opt->verbose_header &&
-		    opt->commit_format != CMIT_FMT_ONELINE) {
-			int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
-			if ((pch & opt->diffopt.output_format) == pch
-			    || (opt->commit_format == CMIT_FMT_EMAIL
-				&& opt->show_notes))
-				printf("---");
-			putchar('\n');
-		}
-		if (opt->commit_format == CMIT_FMT_EMAIL && opt->show_notes) {
-			struct strbuf sb = STRBUF_INIT;
-			putchar('\n');
-			format_note(NULL, sha1, &sb, NULL, 0);
-			fwrite(sb.buf, 1, sb.len, stdout);
-			strbuf_release(&sb);
-			putchar('\n');
-		}
+		log_tree_after_message(opt);
 	}
 	diff_flush(&opt->diffopt);
 	return 1;
Previous: Stephen BoydNext: Thomas Rast
Message 30 of 66 in “several notes refs, post-rewrite, notes rewriting”
  1. 00/12 several notes refs, post-rewrite, notes rewritingThomas Rast, Feb 20, 2010
  2. 01/12 Support showing notes from more than one notes treeThomas Rast, Feb 20, 2010
  3. Junio C HamanoFeb 21, 2010
  4. 02/12 Documentation: document post-rewrite hookThomas Rast, Feb 20, 2010
  5. 03/12 commit --amend: invoke post-rewrite hookThomas Rast, Feb 20, 2010
  6. Junio C HamanoFeb 21, 2010
  7. 04/12 rebase: invoke post-rewrite hookThomas Rast, Feb 20, 2010
  8. 05/12 rebase -i: invoke post-rewrite hookThomas Rast, Feb 20, 2010
  9. 06/12 notes: implement 'git notes copy --stdin'Thomas Rast, Feb 20, 2010
  10. Junio C HamanoFeb 21, 2010
  11. 07/12 notes: implement helpers needed for note copying during rewriteThomas Rast, Feb 20, 2010
  12. Junio C HamanoFeb 21, 2010
  13. 08/12 rebase: support automatic notes copyingThomas Rast, Feb 20, 2010
  14. 09/12 commit --amend: copy notes to the new commitThomas Rast, Feb 20, 2010
  15. 10/12 filter-branch: invoke post-rewrite hookThomas Rast, Feb 20, 2010
  16. 11/12 filter-branch: learn how to filter notesThomas Rast, Feb 20, 2010
  17. 12/12 notes: add shorthand --ref to override GIT_NOTES_REFThomas Rast, Feb 20, 2010
  18. Junio C HamanoFeb 21, 2010
  19. Thomas RastFeb 21, 2010
  20. Junio C HamanoFeb 22, 2010
  21. 00/11 several notes refs, post-rewrite, notes rewritingThomas Rast, Feb 23, 2010
  22. 01/11 test-lib: unset GIT_NOTES_REF to stop it from influencing testsThomas Rast, Feb 23, 2010
  23. 02/11 Support showing notes from more than one notes treeThomas Rast, Feb 23, 2010
  24. Junio C HamanoFeb 23, 2010
  25. Thomas RastFeb 23, 2010
  26. format-patch: learn to fill comment section of email from notesThomas Rast, Feb 23, 2010
  27. BROKEN -- format-patch: learn to fill comment section of email from notesThomas Rast, Feb 23, 2010
  28. Thomas RastFeb 23, 2010
  29. Stephen BoydFeb 24, 2010
  30. Junio C HamanoFeb 23, 2010
  31. Thomas RastMar 10, 2010
  32. 03/11 Documentation: document post-rewrite hookThomas Rast, Feb 23, 2010
  33. 04/11 commit --amend: invoke post-rewrite hookThomas Rast, Feb 23, 2010
  34. 05/11 rebase: invoke post-rewrite hookThomas Rast, Feb 23, 2010
  35. 06/11 rebase -i: invoke post-rewrite hookThomas Rast, Feb 23, 2010
  36. Junio C HamanoFeb 24, 2010
  37. 07/11 notes: implement 'git notes copy --stdin'Thomas Rast, Feb 23, 2010
  38. 08/11 notes: implement helpers needed for note copying during rewriteThomas Rast, Feb 23, 2010
  39. 09/11 rebase: support automatic notes copyingThomas Rast, Feb 23, 2010
  40. Junio C HamanoFeb 25, 2010
  41. 00/13 several notes refs, post-rewrite, notes rewritingThomas Rast, Mar 10, 2010
  42. 01/13 test-lib: unset GIT_NOTES_REF to stop it from influencing testsThomas Rast, Mar 10, 2010
  43. Johan HerlandMar 11, 2010
  44. 02/13 Support showing notes from more than one notes treeThomas Rast, Mar 10, 2010
  45. Johan HerlandMar 11, 2010
  46. 03/13 Documentation: document post-rewrite hookThomas Rast, Mar 10, 2010
  47. 04/13 commit --amend: invoke post-rewrite hookThomas Rast, Mar 10, 2010
  48. 05/13 rebase: invoke post-rewrite hookThomas Rast, Mar 10, 2010
  49. 06/13 rebase -i: invoke post-rewrite hookThomas Rast, Mar 10, 2010
  50. 07/13 notes: implement 'git notes copy --stdin'Thomas Rast, Mar 10, 2010
  51. Johan HerlandMar 11, 2010
  52. 08/13 notes: implement helpers needed for note copying during rewriteThomas Rast, Mar 10, 2010
  53. Johan HerlandMar 11, 2010
  54. 09/13 rebase: support automatic notes copyingThomas Rast, Mar 10, 2010
  55. 10/13 commit --amend: copy notes to the new commitThomas Rast, Mar 10, 2010
  56. 11/13 notes: add shorthand --ref to override GIT_NOTES_REFThomas Rast, Mar 10, 2010
  57. Johan HerlandMar 11, 2010
  58. 12/13 notes: track whether notes_trees were changed at allThomas Rast, Mar 10, 2010
  59. Johan HerlandMar 11, 2010
  60. 13/13 git-notes(1): add a section about the meaning of historyThomas Rast, Mar 10, 2010
  61. Johan HerlandMar 11, 2010
  62. Junio C HamanoMar 10, 2010
  63. 10/11 commit --amend: copy notes to the new commitThomas Rast, Feb 23, 2010
  64. 11/11 notes: add shorthand --ref to override GIT_NOTES_REFThomas Rast, Feb 23, 2010
  65. Junio C HamanoFeb 23, 2010
  66. Thomas RastFeb 23, 2010

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.