Skip to content

Commit ba50274

Browse files
FirstLoveLifegitster
authored andcommitted
rebase: support --trailer
Implement a new `--trailer <text>` option for `git rebase` (support merge backend only now), which appends arbitrary trailer lines to each rebased commit message. Reject early if used with the apply backend (git am) since it lacks message‑filter/trailer hook. Automatically set REBASE_FORCE when any trailer is supplied. And reject invalid input before user edit the interactive file. Signed-off-by: Li Chen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb58ca4 commit ba50274

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

Documentation/git-rebase.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,13 @@ See also INCOMPATIBLE OPTIONS below.
521521
that if `--interactive` is given then only commits marked to be
522522
picked, edited or reworded will have the trailer added.
523523
+
524+
--trailer <trailer>::
525+
Append the given trailer line(s) to every rebased commit
526+
message, processed via linkgit:git-interpret-trailers[1].
527+
When this option is present *rebase automatically enables*
528+
`--force-rebase` so that fast‑forwarded commits are also
529+
rewritten.
530+
524531
See also INCOMPATIBLE OPTIONS below.
525532

526533
-i::

builtin/rebase.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "reset.h"
3737
#include "trace2.h"
3838
#include "hook.h"
39+
#include "trailer.h"
40+
#include "parse-options.h"
3941

4042
static char const * const builtin_rebase_usage[] = {
4143
N_("git rebase [-i] [options] [--exec <cmd>] "
@@ -113,6 +115,7 @@ struct rebase_options {
113115
enum action action;
114116
char *reflog_action;
115117
int signoff;
118+
struct strvec trailer_args;
116119
int allow_rerere_autoupdate;
117120
int keep_empty;
118121
int autosquash;
@@ -143,6 +146,7 @@ struct rebase_options {
143146
.flags = REBASE_NO_QUIET, \
144147
.git_am_opts = STRVEC_INIT, \
145148
.exec = STRING_LIST_INIT_NODUP, \
149+
.trailer_args = STRVEC_INIT, \
146150
.git_format_patch_opt = STRBUF_INIT, \
147151
.fork_point = -1, \
148152
.reapply_cherry_picks = -1, \
@@ -166,6 +170,7 @@ static void rebase_options_release(struct rebase_options *opts)
166170
free(opts->strategy);
167171
string_list_clear(&opts->strategy_opts, 0);
168172
strbuf_release(&opts->git_format_patch_opt);
173+
strvec_clear(&opts->trailer_args);
169174
}
170175

171176
static struct replay_opts get_replay_opts(const struct rebase_options *opts)
@@ -177,6 +182,10 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
177182
sequencer_init_config(&replay);
178183

179184
replay.signoff = opts->signoff;
185+
186+
for (size_t i = 0; i < opts->trailer_args.nr; i++)
187+
strvec_push(&replay.trailer_args, opts->trailer_args.v[i]);
188+
180189
replay.allow_ff = !(opts->flags & REBASE_FORCE);
181190
if (opts->allow_rerere_autoupdate)
182191
replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
@@ -435,6 +444,8 @@ static int read_basic_state(struct rebase_options *opts)
435444
struct strbuf head_name = STRBUF_INIT;
436445
struct strbuf buf = STRBUF_INIT;
437446
struct object_id oid;
447+
const char trailer_state_name[] = "trailer";
448+
const char *path = state_dir_path(trailer_state_name, opts);
438449

439450
if (!read_oneliner(&head_name, state_dir_path("head-name", opts),
440451
READ_ONELINER_WARN_MISSING) ||
@@ -503,11 +514,31 @@ static int read_basic_state(struct rebase_options *opts)
503514

504515
strbuf_release(&buf);
505516

517+
if (strbuf_read_file(&buf, path, 0) >= 0) {
518+
const char *p = buf.buf, *end = buf.buf + buf.len;
519+
520+
while (p < end) {
521+
char *nl = memchr(p, '\n', end - p);
522+
if (!nl)
523+
die("nl shouldn't be NULL");
524+
*nl = '\0';
525+
526+
if (*p)
527+
strvec_push(&opts->trailer_args, p);
528+
529+
p = nl + 1;
530+
}
531+
strbuf_release(&buf);
532+
}
533+
strbuf_release(&buf);
534+
506535
return 0;
507536
}
508537

509538
static int rebase_write_basic_state(struct rebase_options *opts)
510539
{
540+
const char trailer_state_name[] = "trailer";
541+
511542
write_file(state_dir_path("head-name", opts), "%s",
512543
opts->head_name ? opts->head_name : "detached HEAD");
513544
write_file(state_dir_path("onto", opts), "%s",
@@ -529,6 +560,22 @@ static int rebase_write_basic_state(struct rebase_options *opts)
529560
if (opts->signoff)
530561
write_file(state_dir_path("signoff", opts), "--signoff");
531562

563+
/*
564+
* save opts->trailer_args into state_dir/trailer
565+
*/
566+
if (opts->trailer_args.nr) {
567+
struct strbuf buf = STRBUF_INIT;
568+
size_t i;
569+
570+
for (i = 0; i < opts->trailer_args.nr; i++) {
571+
strbuf_addstr(&buf, opts->trailer_args.v[i]);
572+
strbuf_addch(&buf, '\n');
573+
}
574+
write_file(state_dir_path(trailer_state_name, opts),
575+
"%s", buf.buf);
576+
strbuf_release(&buf);
577+
}
578+
532579
return 0;
533580
}
534581

@@ -1085,6 +1132,37 @@ static int check_exec_cmd(const char *cmd)
10851132
return 0;
10861133
}
10871134

1135+
static int validate_trailer_args_after_config(const struct strvec *cli_args,
1136+
struct strbuf *err)
1137+
{
1138+
size_t i;
1139+
1140+
for (i = 0; i < cli_args->nr; i++) {
1141+
const char *raw = cli_args->v[i];
1142+
const char *txt; // Key[:=]Val
1143+
const char *sep;
1144+
1145+
if (!skip_prefix(raw, "--trailer=", &txt))
1146+
txt = raw;
1147+
1148+
if (!*txt) {
1149+
strbuf_addstr(err, _("empty --trailer argument"));
1150+
return -1;
1151+
}
1152+
1153+
sep = strpbrk(txt, ":=");
1154+
1155+
/* there must be key bfore seperator */
1156+
if (sep && sep == txt) {
1157+
strbuf_addf(err,
1158+
_("invalid trailer '%s': missing key before separator"),
1159+
txt);
1160+
return -1;
1161+
}
1162+
}
1163+
return 0;
1164+
}
1165+
10881166
int cmd_rebase(int argc,
10891167
const char **argv,
10901168
const char *prefix,
@@ -1133,6 +1211,7 @@ int cmd_rebase(int argc,
11331211
.flags = PARSE_OPT_NOARG,
11341212
.defval = REBASE_DIFFSTAT,
11351213
},
1214+
OPT_STRVEC(0, "trailer", &options.trailer_args, N_("trailer"), N_("add custom trailer(s)")),
11361215
OPT_BOOL(0, "signoff", &options.signoff,
11371216
N_("add a Signed-off-by trailer to each commit")),
11381217
OPT_BOOL(0, "committer-date-is-author-date",
@@ -1283,6 +1362,17 @@ int cmd_rebase(int argc,
12831362
builtin_rebase_options,
12841363
builtin_rebase_usage, 0);
12851364

1365+
/* if add --trailer,force rebase */
1366+
if (options.trailer_args.nr) {
1367+
struct strbuf err = STRBUF_INIT;
1368+
1369+
if (validate_trailer_args_after_config(&options.trailer_args, &err))
1370+
die("%s", err.buf);
1371+
1372+
options.flags |= REBASE_FORCE;
1373+
strbuf_release(&err);
1374+
}
1375+
12861376
if (preserve_merges_selected)
12871377
die(_("--preserve-merges was replaced by --rebase-merges\n"
12881378
"Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
@@ -1540,6 +1630,14 @@ int cmd_rebase(int argc,
15401630
if (options.root && !options.onto_name)
15411631
imply_merge(&options, "--root without --onto");
15421632

1633+
/*
1634+
* The apply‑based backend (git am) cannot append trailers because
1635+
* it lacks a message‑filter facility. Reject early, before any
1636+
* state (index, HEAD, etc.) is modified.
1637+
*/
1638+
if (options.trailer_args.nr)
1639+
imply_merge(&options, "--trailer");
1640+
15431641
if (isatty(2) && options.flags & REBASE_NO_QUIET)
15441642
strbuf_addstr(&options.git_format_patch_opt, " --progress");
15451643

sequencer.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ void replay_opts_release(struct replay_opts *opts)
422422
free(opts->revs);
423423
replay_ctx_release(ctx);
424424
free(opts->ctx);
425+
strvec_clear(&opts->trailer_args);
425426
}
426427

427428
int sequencer_remove_state(struct replay_opts *opts)
@@ -2529,6 +2530,18 @@ static int do_pick_commit(struct repository *r,
25292530
_("dropping %s %s -- patch contents already upstream\n"),
25302531
oid_to_hex(&commit->object.oid), msg.subject);
25312532
} /* else allow == 0 and there's nothing special to do */
2533+
2534+
if (!res && opts->trailer_args.nr && !drop_commit) {
2535+
const char *trailer_file =
2536+
msg_file ? msg_file : git_path_merge_msg(r);
2537+
2538+
if (amend_file_with_trailers(trailer_file,
2539+
&opts->trailer_args)) {
2540+
res = error(_("unable to add trailers to commit message"));
2541+
goto leave;
2542+
}
2543+
}
2544+
25322545
if (!opts->no_commit && !drop_commit) {
25332546
if (author || command == TODO_REVERT || (flags & AMEND_MSG))
25342547
res = do_commit(r, msg_file, author, reflog_action,

sequencer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "strbuf.h"
55
#include "strvec.h"
66
#include "wt-status.h"
7+
#include <stddef.h>
78

89
struct commit;
910
struct index_state;
@@ -44,6 +45,7 @@ struct replay_opts {
4445
int record_origin;
4546
int no_commit;
4647
int signoff;
48+
struct strvec trailer_args;
4749
int allow_ff;
4850
int allow_rerere_auto;
4951
int allow_empty;
@@ -86,6 +88,7 @@ struct replay_opts {
8688
.action = -1, \
8789
.xopts = STRVEC_INIT, \
8890
.ctx = replay_ctx_new(), \
91+
.trailer_args = STRVEC_INIT, \
8992
}
9093

9194
/*

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ integration_tests = [
373373
't3436-rebase-more-options.sh',
374374
't3437-rebase-fixup-options.sh',
375375
't3438-rebase-broken-files.sh',
376+
't3440-rebase-trailer.sh',
376377
't3500-cherry.sh',
377378
't3501-revert-cherry-pick.sh',
378379
't3502-cherry-pick-merge.sh',

t/t3440-rebase-trailer.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/sh
2+
#
3+
4+
test_description='git rebase --trailer integration tests
5+
We verify that --trailer on the merge/interactive/exec/root backends,
6+
and that it is rejected early when the apply backend is requested.'
7+
8+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10+
11+
. ./test-lib.sh
12+
. "$TEST_DIRECTORY"/lib-rebase.sh # test_commit_message, helpers
13+
14+
create_expect() {
15+
cat >"$1" <<-EOF
16+
$2
17+
18+
Reviewed-by: Dev <[email protected]>
19+
EOF
20+
}
21+
22+
test_expect_success 'setup repo with a small history' '
23+
git commit --allow-empty -m "Initial empty commit" &&
24+
test_commit first file a &&
25+
test_commit second file &&
26+
git checkout -b conflict-branch first &&
27+
test_commit file-2 file-2 &&
28+
test_commit conflict file &&
29+
test_commit third file &&
30+
ident="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" &&
31+
create_expect initial-signed "Initial empty commit" &&
32+
create_expect first-signed "first" &&
33+
create_expect second-signed "second" &&
34+
create_expect file2-signed "file-2" &&
35+
create_expect third-signed "third" &&
36+
create_expect conflict-signed "conflict"
37+
'
38+
39+
test_expect_success 'apply backend is rejected with --trailer' '
40+
git reset --hard third &&
41+
head_before=$(git rev-parse HEAD) &&
42+
test_expect_code 128 \
43+
git rebase --apply --trailer "Reviewed-by: Dev <[email protected]>" \
44+
HEAD^ 2>err &&
45+
test_grep "requires the merge backend" err &&
46+
test_cmp_rev HEAD $head_before
47+
'
48+
49+
test_expect_success 'reject empty --trailer argument' '
50+
git reset --hard third &&
51+
test_expect_code 128 git rebase -m --trailer "" HEAD^ 2>err &&
52+
test_grep "empty --trailer" err
53+
'
54+
55+
test_expect_success 'reject trailer with missing key before separator' '
56+
git reset --hard third &&
57+
test_expect_code 128 git rebase -m --trailer ": no-key" HEAD^ 2>err &&
58+
test_grep "missing key before separator" err
59+
'
60+
61+
test_expect_success 'CLI trailer duplicates allowed; replace policy keeps last' '
62+
git reset --hard third &&
63+
git -c trailer.Bug.ifexists=replace -c trailer.Bug.ifmissing=add rebase -m --trailer "Bug: 123" --trailer "Bug: 456" HEAD~1 &&
64+
git cat-file commit HEAD | grep "^Bug: 456" &&
65+
git cat-file commit HEAD | grep -v "^Bug: 123"
66+
'
67+
68+
test_expect_success 'multiple Signed-off-by trailers all preserved' '
69+
git reset --hard third &&
70+
git rebase -m \
71+
--trailer "Signed-off-by: Dev A <[email protected]>" \
72+
--trailer "Signed-off-by: Dev B <[email protected]>" HEAD~1 &&
73+
git cat-file commit HEAD | grep -c "^Signed-off-by:" >count &&
74+
test "$(cat count)" = 2 # two new commits
75+
'
76+
77+
test_expect_success 'rebase -m --trailer adds trailer after conflicts' '
78+
git reset --hard third &&
79+
test_must_fail git rebase -m \
80+
--trailer "Reviewed-by: Dev <[email protected]>" \
81+
second third &&
82+
git checkout --theirs file &&
83+
git add file &&
84+
git rebase --continue &&
85+
test_commit_message HEAD~2 file2-signed
86+
'
87+
88+
test_expect_success 'rebase --root --trailer updates every commit' '
89+
git checkout first &&
90+
git rebase --root --keep-empty \
91+
--trailer "Reviewed-by: Dev <[email protected]>" &&
92+
test_commit_message HEAD first-signed &&
93+
test_commit_message HEAD^ initial-signed
94+
'
95+
test_done

0 commit comments

Comments
 (0)