git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Beller <sbeller@google.com>
To: git@vger.kernel.org
Cc: Stefan Beller <sbeller@google.com>
Subject: [PATCH] Introduce a hook to run after formatting patches
Date: Fri, 14 Nov 2014 16:47:40 -0800	[thread overview]
Message-ID: <1416012460-4459-1-git-send-email-sbeller@google.com> (raw)

This comes in handy if you want to post-process formatted patches.
One examplary use case would be removing ChangeIds, which are used
in Gerrit, a program sitting on top of Git, used for tracking
different versions of a patch.

Another use case would be checking if all your commits are signed off,
or have another kind of property.

So in my case a hook like the following will help a lot.

	# Run with on formatted patches. The first argument is the filename to the patch.
	sed --in-place '/^Change-Id:/d' $1

Signed-off-by: Stefan Beller <sbeller@google.com>
---

 Hi Git people,
 I haven't sent a patch for some time now, but I intend to change that
 soon, as I'll be overtaking the transactions series from Ronnie Sahlberg.

 The patch series I intend to overtake has been reviewed on this list
 as well as https://code-review.googlesource.com/#/q/project:git
 using Gerrit. Gerrit uses Change-Ids, which I want to reliably 
 remove before sending them on the list. And for reliability 
 you better trust a machine than a human like me.

 Documentation/githooks.txt |  9 +++++++++
 builtin/log.c              | 17 +++++++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index 9ef2469..b4f06a9 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -69,6 +69,15 @@ and is invoked after the patch is applied and a commit is made.
 This hook is meant primarily for notification, and cannot affect
 the outcome of 'git am'.
 
+post-format-patch
+~~~~~~~~~~~~
+
+This hook is called after format-patch created a patch and it is 
+invoked with the filename of the patch as the first parameter.
+
+This hook can be used to alter the created patch, such as removing
+or adding Sign-Offs or similar information.
+
 pre-commit
 ~~~~~~~~~~
 
diff --git a/builtin/log.c b/builtin/log.c
index 734aab3..863fcef 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -761,7 +761,8 @@ static const char *output_directory = NULL;
 static int outdir_offset;
 
 static int reopen_stdout(struct commit *commit, const char *subject,
-			 struct rev_info *rev, int quiet)
+			 struct rev_info *rev, int quiet,
+			 struct strbuf *choosen_filename)
 {
 	struct strbuf filename = STRBUF_INIT;
 	int suffix_len = strlen(rev->patch_suffix) + 1;
@@ -788,6 +789,11 @@ static int reopen_stdout(struct commit *commit, const char *subject,
 	if (freopen(filename.buf, "w", stdout) == NULL)
 		return error(_("Cannot open patch file %s"), filename.buf);
 
+	if (choosen_filename) {
+		strbuf_reset(choosen_filename);
+		strbuf_addstr(choosen_filename, filename.buf);
+	}
+
 	strbuf_release(&filename);
 	return 0;
 }
@@ -921,7 +927,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 	committer = git_committer_info(0);
 
 	if (!use_stdout &&
-	    reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
+	    reopen_stdout(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet, NULL))
 		return;
 
 	log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
@@ -1176,6 +1182,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	const char *in_reply_to = NULL;
 	struct patch_ids ids;
 	struct strbuf buf = STRBUF_INIT;
+	struct strbuf filename = STRBUF_INIT;
 	int use_patch_format = 0;
 	int quiet = 0;
 	int reroll_count = -1;
@@ -1531,7 +1538,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		}
 
 		if (!use_stdout &&
-		    reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet))
+		    reopen_stdout(rev.numbered_files ? NULL : commit, NULL, &rev, quiet, &filename))
 			die(_("Failed to create output files"));
 		shown = log_tree_commit(&rev, commit);
 		free_commit_buffer(commit);
@@ -1552,8 +1559,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			else
 				print_signature();
 		}
-		if (!use_stdout)
+		if (!use_stdout) {
 			fclose(stdout);
+			run_hook_le(NULL, "post-format-patch", filename.buf, NULL);
+		}
 	}
 	free(list);
 	free(branch_name);
-- 
2.2.0.rc1.24.g562add4

             reply	other threads:[~2014-11-15  0:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-15  0:47 Stefan Beller [this message]
2014-11-15 10:44 ` [PATCH] Introduce a hook to run after formatting patches Philip Oakley
2014-11-16 18:40 ` Junio C Hamano
2014-11-17 19:01   ` Stefan Beller
2014-11-17 19:06   ` Junio C Hamano
2014-11-17 19:20     ` Junio C Hamano
2014-11-18  6:40       ` Christian Couder
2014-11-20 23:26         ` Stefan Beller
2014-11-20 23:33           ` Junio C Hamano
2014-11-21  4:31             ` Christian Couder
2014-11-18  2:30     ` Stefan Beller
2014-11-18 17:02       ` Junio C Hamano

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=1416012460-4459-1-git-send-email-sbeller@google.com \
    --to=sbeller@google.com \
    --cc=git@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).