Skip to content

Commit 4a86187

Browse files
jpassarogitster
authored andcommitted
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 <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: John Passaro <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56740f9 commit 4a86187

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

builtin/commit.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "commit-reach.h"
3939
#include "commit-graph.h"
4040
#include "pretty.h"
41+
#include "trailer.h"
4142

4243
static const char * const builtin_commit_usage[] = {
4344
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,
10301031
fclose(s->fp);
10311032

10321033
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))
10411035
die(_("unable to pass trailers to --trailers"));
10421036
strvec_clear(&trailer_args);
10431037
}

trailer.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,3 +1170,15 @@ void trailer_iterator_release(struct trailer_iterator *iter)
11701170
strbuf_release(&iter->val);
11711171
strbuf_release(&iter->key);
11721172
}
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "list.h"
55
#include "strbuf.h"
66

7+
struct strvec;
8+
79
enum trailer_where {
810
WHERE_DEFAULT,
911
WHERE_END,
@@ -158,4 +160,11 @@ int trailer_iterator_advance(struct trailer_iterator *iter);
158160
*/
159161
void trailer_iterator_release(struct trailer_iterator *iter);
160162

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+
161170
#endif /* TRAILER_H */

0 commit comments

Comments
 (0)