Git development
 help / color / mirror / Atom feed
From: Jed Brown <jed@59A2.org>
To: git@vger.kernel.org
Cc: madcoder@debian.org, Jed Brown <jed@59A2.org>
Subject: [PATCH 1/2] Added the --notmuch option to git format-patch.
Date: Wed, 25 Nov 2009 03:50:46 +0100	[thread overview]
Message-ID: <1259117447-23052-1-git-send-email-jed@59A2.org> (raw)
In-Reply-To: <87d4375ne0.fsf@59A2.org>

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

  reply	other threads:[~2009-11-25  2:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-25  2:49 git send-email --notmuch expr Jed Brown
2009-11-25  2:50 ` Jed Brown [this message]
2009-11-25  2:50   ` [PATCH 2/2] Documentation for format-patch --notmuch Jed Brown
2009-11-25 10:16 ` git send-email --notmuch expr Jakub Narebski
2009-11-25 13:06   ` Jed Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1259117447-23052-1-git-send-email-jed@59A2.org \
    --to=jed@59a2.org \
    --cc=git@vger.kernel.org \
    --cc=madcoder@debian.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox