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

[PATCH 2/5] log: Refactor duplicated code to headerize recipient lists

From
ZWZev Weiss <zev@bewilderbeest.net>
Date
Jan 19, 2023, 22:38 UTC
Message-ID
<20230119223858.29262-3-zev@bewilderbeest.net>
In-Reply-To
<20230119223858.29262-1-zev@bewilderbeest.net>

The To and Cc headers are handled identically (the only difference is the header name tag), so we might as well reuse the same code for both instead of duplicating it.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
---
 builtin/log.c | 23 ++---------------------
 log-tree.c    | 15 +++++++++++++++
 log-tree.h    |  2 ++
 3 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index 057e299c245c..ad9d7ebc6d73 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -2028,27 +2028,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		strbuf_addch(&buf, '\n');
 	}
 
-	if (extra_to.nr)
-		strbuf_addstr(&buf, "To: ");
-	for (i = 0; i < extra_to.nr; i++) {
-		if (i)
-			strbuf_addstr(&buf, "    ");
-		strbuf_addstr(&buf, extra_to.items[i].string);
-		if (i + 1 < extra_to.nr)
-			strbuf_addch(&buf, ',');
-		strbuf_addch(&buf, '\n');
-	}
-
-	if (extra_cc.nr)
-		strbuf_addstr(&buf, "Cc: ");
-	for (i = 0; i < extra_cc.nr; i++) {
-		if (i)
-			strbuf_addstr(&buf, "    ");
-		strbuf_addstr(&buf, extra_cc.items[i].string);
-		if (i + 1 < extra_cc.nr)
-			strbuf_addch(&buf, ',');
-		strbuf_addch(&buf, '\n');
-	}
+	recipients_to_header_buf("To", &buf, &extra_to);
+	recipients_to_header_buf("Cc", &buf, &extra_cc);
 
 	rev.extra_headers = to_free = strbuf_detach(&buf, NULL);
 
diff --git a/log-tree.c b/log-tree.c
index 1dd5fcbf7be4..0e8863fe545a 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -426,6 +426,21 @@ void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
 	}
 }
 
+void recipients_to_header_buf(const char *hdr, struct strbuf *buf,
+			      const struct string_list *recipients)
+{
+	for (int i = 0; i < recipients->nr; i++) {
+		if (!i)
+			strbuf_addf(buf, "%s: ", hdr);
+		else
+			strbuf_addstr(buf, "    ");
+		strbuf_addstr(buf, recipients->items[i].string);
+		if (i + 1 < recipients->nr)
+			strbuf_addch(buf, ',');
+		strbuf_addch(buf, '\n');
+	}
+}
+
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 			     const char **extra_headers_p,
 			     int *need_8bit_cte_p,
diff --git a/log-tree.h b/log-tree.h
index e7e4641cf83c..227edc564121 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -25,6 +25,8 @@ void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
 #define format_decorations(strbuf, commit, color) \
 			     format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
 void show_decorations(struct rev_info *opt, struct commit *commit);
+void recipients_to_header_buf(const char *hdr, struct strbuf *buf,
+			      const struct string_list *recipients);
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 			     const char **extra_headers_p,
 			     int *need_8bit_cte_p,
-- 
2.39.1.236.ga8a28b9eace8
Previous: Calvin WanNext: Junio C Hamano
Message 5 of 14 in “format-patch: Add --{to,cc}-cmd support”
  1. 0/5 format-patch: Add --{to,cc}-cmd supportZev Weiss, Jan 19, 2023
  2. 3/5 log: Push to/cc handling down into show_log()Zev Weiss, Jan 19, 2023
  3. Junio C HamanoJan 20, 2023
  4. Calvin WanFeb 1, 2023
  5. 2/5 log: Refactor duplicated code to headerize recipient listsZev Weiss, Jan 19, 2023
  6. Junio C HamanoJan 25, 2023
  7. 4/5 pretty: Add name_and_address_only parameterZev Weiss, Jan 19, 2023
  8. Junio C HamanoJan 25, 2023
  9. Calvin WanFeb 1, 2023
  10. 1/5 t4014: Add test checking cover-letter To headerZev Weiss, Jan 19, 2023
  11. Calvin WanFeb 1, 2023
  12. 5/5 format-patch: Add support for --{to,cc}-cmd flagsZev Weiss, Jan 19, 2023
  13. Calvin WanFeb 1, 2023
  14. Calvin WanFeb 1, 2023

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.