diff mbox series

[v4,2/3] builtin/commit.c: refactor --trailer logic

Message ID 8f53a54bbfe9a952ae5e86216681eef2a2e916eb.1714488111.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series builtin/tag.c: add --trailer option | expand

Commit Message

John Passaro April 30, 2024, 2:41 p.m. UTC
From: John Passaro <john.a.passaro@gmail.com>

git-commit adds user trailers to the commit message by passing its
`--trailer` arguments to a child process running `git-interpret-trailers
--in-place`. This logic is broadly useful, not just for git-commit but
for other commands constructing message bodies (e.g. git-tag).

Let's move this logic from git-commit to a new function in the trailer
API, so that it can be re-used in other commands.

Helped-by: Patrick Steinhardt <ps@pks.im>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: John Passaro <john.a.passaro@gmail.com>
---
 builtin/commit.c | 10 ++--------
 trailer.c        | 11 +++++++++++
 trailer.h        |  9 +++++++++
 3 files changed, 22 insertions(+), 8 deletions(-)

Comments

Patrick Steinhardt May 2, 2024, 6:27 a.m. UTC | #1
On Tue, Apr 30, 2024 at 02:41:50PM +0000, John Passaro via GitGitGadget wrote:
> From: John Passaro <john.a.passaro@gmail.com>
[snip]
> diff --git a/trailer.c b/trailer.c
> index c72ae687099..ae0597d919e 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -1170,3 +1170,14 @@ void trailer_iterator_release(struct trailer_iterator *iter)
>  	strbuf_release(&iter->val);
>  	strbuf_release(&iter->key);
>  }
> +
> +int amend_file_with_trailers(const char *path, const struct strvec *trailer_args) {

Nit: the opening brace should go on the next line.

Other than that this patch looks good to me.

Patrick

> +	struct child_process run_trailer = CHILD_PROCESS_INIT;
> +
> +	run_trailer.git_cmd = 1;
> +	strvec_pushl(&run_trailer.args, "interpret-trailers",
> +		     "--in-place", "--no-divider",
> +		     path, NULL);
> +	strvec_pushv(&run_trailer.args, trailer_args->v);
> +	return run_command(&run_trailer);
> +}
> diff --git a/trailer.h b/trailer.h
> index 9f42aa75994..c364405267a 100644
> --- a/trailer.h
> +++ b/trailer.h
> @@ -4,6 +4,8 @@
>  #include "list.h"
>  #include "strbuf.h"
>  
> +struct strvec;
> +
>  enum trailer_where {
>  	WHERE_DEFAULT,
>  	WHERE_END,
> @@ -158,4 +160,11 @@ int trailer_iterator_advance(struct trailer_iterator *iter);
>   */
>  void trailer_iterator_release(struct trailer_iterator *iter);
>  
> +/*
> + * Augment a file to add trailers to it by running git-interpret-trailers.
> + * This calls run_command() and its return value is the same (i.e. 0 for
> + * success, various non-zero for other errors). See run-command.h.
> + */
> +int amend_file_with_trailers(const char *path, const struct strvec *trailer_args);
> +
>  #endif /* TRAILER_H */
> -- 
> gitgitgadget
>
diff mbox series

Patch

diff --git a/builtin/commit.c b/builtin/commit.c
index 5a3248370db..63cd090b6f2 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -38,6 +38,7 @@ 
 #include "commit-reach.h"
 #include "commit-graph.h"
 #include "pretty.h"
+#include "trailer.h"
 
 static const char * const builtin_commit_usage[] = {
 	N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
@@ -1030,14 +1031,7 @@  static int prepare_to_commit(const char *index_file, const char *prefix,
 	fclose(s->fp);
 
 	if (trailer_args.nr) {
-		struct child_process run_trailer = CHILD_PROCESS_INIT;
-
-		strvec_pushl(&run_trailer.args, "interpret-trailers",
-			     "--in-place", "--no-divider",
-			     git_path_commit_editmsg(), NULL);
-		strvec_pushv(&run_trailer.args, trailer_args.v);
-		run_trailer.git_cmd = 1;
-		if (run_command(&run_trailer))
+		if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
 			die(_("unable to pass trailers to --trailers"));
 		strvec_clear(&trailer_args);
 	}
diff --git a/trailer.c b/trailer.c
index c72ae687099..ae0597d919e 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1170,3 +1170,14 @@  void trailer_iterator_release(struct trailer_iterator *iter)
 	strbuf_release(&iter->val);
 	strbuf_release(&iter->key);
 }
+
+int amend_file_with_trailers(const char *path, const struct strvec *trailer_args) {
+	struct child_process run_trailer = CHILD_PROCESS_INIT;
+
+	run_trailer.git_cmd = 1;
+	strvec_pushl(&run_trailer.args, "interpret-trailers",
+		     "--in-place", "--no-divider",
+		     path, NULL);
+	strvec_pushv(&run_trailer.args, trailer_args->v);
+	return run_command(&run_trailer);
+}
diff --git a/trailer.h b/trailer.h
index 9f42aa75994..c364405267a 100644
--- a/trailer.h
+++ b/trailer.h
@@ -4,6 +4,8 @@ 
 #include "list.h"
 #include "strbuf.h"
 
+struct strvec;
+
 enum trailer_where {
 	WHERE_DEFAULT,
 	WHERE_END,
@@ -158,4 +160,11 @@  int trailer_iterator_advance(struct trailer_iterator *iter);
  */
 void trailer_iterator_release(struct trailer_iterator *iter);
 
+/*
+ * Augment a file to add trailers to it by running git-interpret-trailers.
+ * This calls run_command() and its return value is the same (i.e. 0 for
+ * success, various non-zero for other errors). See run-command.h.
+ */
+int amend_file_with_trailers(const char *path, const struct strvec *trailer_args);
+
 #endif /* TRAILER_H */