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

Re: [PATCH] Introduce a hook to run after formatting patches

From
Philip Oakley <philipoakley@iee.org>
Date
Nov 15, 2014, 08:27 UTC
Message-ID
<B4E02729E7DE4F90BC6F1A065A7D4D5D@PhilipOakley>
In-Reply-To
<1416012460-4459-1-git-send-email-sbeller@google.com>
From: "Stefan Beller" <sbeller@google.com>
Show 49 quoted lines
> 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.

surely s/adding/checking/ as described in the commit message. We wouldn't want unthinking sign-offs being applied;-)

Show 81 quoted lines
> +
> 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
>
> --
Philip 
Previous: Stefan BellerNext: Junio C Hamano
Message 2 of 12 in “Introduce a hook to run after formatting patches”
  1. Introduce a hook to run after formatting patchesStefan Beller, Nov 15, 2014
  2. Philip OakleyNov 15, 2014
  3. Junio C HamanoNov 16, 2014
  4. Stefan BellerNov 17, 2014
  5. Junio C HamanoNov 17, 2014
  6. Junio C HamanoNov 17, 2014
  7. Christian CouderNov 18, 2014
  8. Stefan BellerNov 20, 2014
  9. Junio C HamanoNov 20, 2014
  10. Christian CouderNov 21, 2014
  11. Stefan BellerNov 18, 2014
  12. Junio C HamanoNov 18, 2014

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.