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 <sbel...@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

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

Reply via email to