builtin/commit: refactor --trailer logic
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> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
John Passaro committed
May 5, 2024 at 18:49 UTC
4a8618785e3df20287d4c18707118d34581a298b
3 files changed
+23
-8
builtin/commit.c
+2
-8
@@ -38,6 +38,7 @@
38
#include "commit-reach.h"
39
#include "commit-graph.h"
40
#include "pretty.h"
41
+#include "trailer.h"
42
43
static const char * const builtin_commit_usage[] = {
44
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,
1031
fclose(s->fp);
1032
1033
if (trailer_args.nr) {
1033
- struct child_process run_trailer = CHILD_PROCESS_INIT;
1034
-
1035
- strvec_pushl(&run_trailer.args, "interpret-trailers",
1036
- "--in-place", "--no-divider",
1037
- git_path_commit_editmsg(), NULL);
1038
- strvec_pushv(&run_trailer.args, trailer_args.v);
1039
- run_trailer.git_cmd = 1;
1040
- if (run_command(&run_trailer))
1034
+ if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
1035
die(_("unable to pass trailers to --trailers"));
1036
strvec_clear(&trailer_args);
1037
}
trailer.c
+12
@@ -1170,3 +1170,15 @@ void trailer_iterator_release(struct trailer_iterator *iter)
1170
strbuf_release(&iter->val);
1171
strbuf_release(&iter->key);
1172
}
1173
+
1174
+int amend_file_with_trailers(const char *path, const struct strvec *trailer_args)
1175
+{
1176
+ struct child_process run_trailer = CHILD_PROCESS_INIT;
1177
+
1178
+ run_trailer.git_cmd = 1;
1179
+ strvec_pushl(&run_trailer.args, "interpret-trailers",
1180
+ "--in-place", "--no-divider",
1181
+ path, NULL);
1182
+ strvec_pushv(&run_trailer.args, trailer_args->v);
1183
+ return run_command(&run_trailer);
1184
+}
trailer.h
+9
@@ -4,6 +4,8 @@
4
#include "list.h"
5
#include "strbuf.h"
6
7
+struct strvec;
8
+
9
enum trailer_where {
10
WHERE_DEFAULT,
11
WHERE_END,
@@ -158,4 +160,11 @@ int trailer_iterator_advance(struct trailer_iterator *iter);
160
*/
161
void trailer_iterator_release(struct trailer_iterator *iter);
162
163
+/*
164
+ * Augment a file to add trailers to it by running git-interpret-trailers.
165
+ * This calls run_command() and its return value is the same (i.e. 0 for
166
+ * success, various non-zero for other errors). See run-command.h.
167
+ */
168
+int amend_file_with_trailers(const char *path, const struct strvec *trailer_args);
169
+
170
#endif /* TRAILER_H */