[PATCH 1/2] Added the --notmuch option to git format-patch.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The typical use case for this is

  git format-patch --notmuch id:<MESSAGE-ID>

which will format your patch with all threading, references, and To, Cc
fields appropriate for a reply to the given message.
---
 builtin-log.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/builtin-log.c b/builtin-log.c
index 33fa6ea..9a44955 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -754,6 +754,53 @@ static const char *clean_message_id(const char *msg_id)
 	return xmemdupz(a, z - a);
 }
 
+static int get_notmuch_reply(struct strbuf *buf, struct string_list *ref_message_ids, const char *query)
+{
+	struct child_process notmuch;
+	struct strbuf notmuch_out;
+	char *p, *other_headers, *rstart;
+	ssize_t len;
+	const char *argv[] = {"notmuch", "reply", "--format=headers-only", query, NULL};
+
+	memset(&notmuch, 0, sizeof(notmuch));
+
+	notmuch.argv	 = argv;
+	notmuch.no_stdin = 1;
+	notmuch.out	 = -1;
+
+	if (start_command(&notmuch))
+		return error("could not run notmuch.");
+
+	strbuf_init(&notmuch_out, 4096);
+	len = strbuf_read(&notmuch_out, notmuch.out, 4096);
+	close(notmuch.out);
+
+	if (finish_command(&notmuch) || !len || len < 0)
+		return error("notmuch did not return any headers");
+
+	/* Harvest the referenced message IDs, all on the first line */
+	p = notmuch_out.buf;
+
+	if (!strncmp(p, "References: ", sizeof("References: ")))
+		return error("notmuch response malformed");
+
+	other_headers = strstr(p, "To: ");
+	if (!other_headers)
+		return error("notmuch provided no other headers");
+
+	while ((rstart = strchr(p, '<')) && (p = strchr(rstart, '>')) && p < other_headers-1) {
+		if (!p)
+			error("notmuch returned malformed references");
+		p++;
+		*p++ = 0;
+		string_list_append(clean_message_id(rstart), ref_message_ids);
+	}
+	/* Add everything after the first line */
+	strbuf_addstr(buf, other_headers);
+	strbuf_release(&notmuch_out);
+	return 0;
+}
+
 static const char *set_outdir(const char *prefix, const char *output_directory)
 {
 	if (output_directory && is_absolute_path(output_directory))
@@ -893,7 +940,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	int boundary_count = 0;
 	int no_binary_diff = 0;
 	struct commit *origin = NULL, *head = NULL;
-	const char *in_reply_to = NULL;
+	const char *in_reply_to = NULL, *notmuch = NULL;
 	struct patch_ids ids;
 	char *add_signoff = NULL;
 	struct strbuf buf = STRBUF_INIT;
@@ -940,6 +987,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			    PARSE_OPT_NONEG, cc_callback },
 		OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
 			    "make first mail a reply to <message-id>"),
+		OPT_STRING(0, "notmuch", &notmuch, "query",
+			    "make first mail a reply to messages matched by <query>"),
 		{ OPTION_CALLBACK, 0, "attach", &rev, "boundary",
 			    "attach the patch", PARSE_OPT_OPTARG,
 			    attach_callback },
@@ -1015,8 +1064,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		strbuf_addch(&buf, '\n');
 	}
 
-	rev.extra_headers = strbuf_detach(&buf, NULL);
-
 	if (start_number < 0)
 		start_number = 1;
 
@@ -1135,12 +1182,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		numbered = 1;
 	if (numbered)
 		rev.total = total + start_number - 1;
-	if (in_reply_to || thread || cover_letter)
+	if (in_reply_to || thread || cover_letter || notmuch)
 		rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
 	if (in_reply_to) {
 		const char *msgid = clean_message_id(in_reply_to);
 		string_list_append(msgid, rev.ref_message_ids);
 	}
+	if (notmuch) {
+		get_notmuch_reply(&buf, rev.ref_message_ids, notmuch);
+	}
+
+	rev.extra_headers = strbuf_detach(&buf, NULL);
+
 	rev.numbered_files = numbered_files;
 	rev.patch_suffix = fmt_patch_suffix;
 	if (cover_letter) {
-- 
1.6.5.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]