3636#include "reset.h"
3737#include "trace2.h"
3838#include "hook.h"
39+ #include "trailer.h"
3940
4041static char const * const builtin_rebase_usage [] = {
4142 N_ ("git rebase [-i] [options] [--exec <cmd>] "
@@ -113,6 +114,7 @@ struct rebase_options {
113114 enum action action ;
114115 char * reflog_action ;
115116 int signoff ;
117+ struct strvec trailer_args ;
116118 int allow_rerere_autoupdate ;
117119 int keep_empty ;
118120 int autosquash ;
@@ -143,6 +145,7 @@ struct rebase_options {
143145 .flags = REBASE_NO_QUIET, \
144146 .git_am_opts = STRVEC_INIT, \
145147 .exec = STRING_LIST_INIT_NODUP, \
148+ .trailer_args = STRVEC_INIT, \
146149 .git_format_patch_opt = STRBUF_INIT, \
147150 .fork_point = -1, \
148151 .reapply_cherry_picks = -1, \
@@ -166,6 +169,7 @@ static void rebase_options_release(struct rebase_options *opts)
166169 free (opts -> strategy );
167170 string_list_clear (& opts -> strategy_opts , 0 );
168171 strbuf_release (& opts -> git_format_patch_opt );
172+ strvec_clear (& opts -> trailer_args );
169173}
170174
171175static struct replay_opts get_replay_opts (const struct rebase_options * opts )
@@ -177,6 +181,10 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
177181 sequencer_init_config (& replay );
178182
179183 replay .signoff = opts -> signoff ;
184+
185+ for (size_t i = 0 ; i < opts -> trailer_args .nr ; i ++ )
186+ strvec_push (& replay .trailer_args , opts -> trailer_args .v [i ]);
187+
180188 replay .allow_ff = !(opts -> flags & REBASE_FORCE );
181189 if (opts -> allow_rerere_autoupdate )
182190 replay .allow_rerere_auto = opts -> allow_rerere_autoupdate ;
@@ -500,6 +508,23 @@ static int read_basic_state(struct rebase_options *opts)
500508 opts -> gpg_sign_opt = xstrdup (buf .buf );
501509 }
502510
511+ strbuf_reset (& buf );
512+
513+ if (strbuf_read_file (& buf , state_dir_path ("trailer" , opts ), 0 ) >= 0 ) {
514+ const char * p = buf .buf , * end = buf .buf + buf .len ;
515+
516+ while (p < end ) {
517+ char * nl = memchr (p , '\n' , end - p );
518+ if (!nl )
519+ die ("nl shouldn't be NULL" );
520+ * nl = '\0' ;
521+
522+ if (* p )
523+ strvec_push (& opts -> trailer_args , p );
524+
525+ p = nl + 1 ;
526+ }
527+ }
503528 strbuf_release (& buf );
504529
505530 return 0 ;
@@ -528,6 +553,21 @@ static int rebase_write_basic_state(struct rebase_options *opts)
528553 if (opts -> signoff )
529554 write_file (state_dir_path ("signoff" , opts ), "--signoff" );
530555
556+ /*
557+ * save opts->trailer_args into state_dir/trailer
558+ */
559+ if (opts -> trailer_args .nr ) {
560+ struct strbuf buf = STRBUF_INIT ;
561+
562+ for (size_t i = 0 ; i < opts -> trailer_args .nr ; i ++ ) {
563+ strbuf_addstr (& buf , opts -> trailer_args .v [i ]);
564+ strbuf_addch (& buf , '\n' );
565+ }
566+ write_file (state_dir_path ("trailer" , opts ),
567+ "%s" , buf .buf );
568+ strbuf_release (& buf );
569+ }
570+
531571 return 0 ;
532572}
533573
@@ -1132,6 +1172,8 @@ int cmd_rebase(int argc,
11321172 .flags = PARSE_OPT_NOARG ,
11331173 .defval = REBASE_DIFFSTAT ,
11341174 },
1175+ OPT_STRVEC (0 , "trailer" , & options .trailer_args , N_ ("trailer" ),
1176+ N_ ("add custom trailer(s)" )),
11351177 OPT_BOOL (0 , "signoff" , & options .signoff ,
11361178 N_ ("add a Signed-off-by trailer to each commit" )),
11371179 OPT_BOOL (0 , "committer-date-is-author-date" ,
@@ -1285,6 +1327,11 @@ int cmd_rebase(int argc,
12851327 builtin_rebase_options ,
12861328 builtin_rebase_usage , 0 );
12871329
1330+ if (options .trailer_args .nr ) {
1331+ validate_trailer_args_after_config (& options .trailer_args );
1332+ options .flags |= REBASE_FORCE ;
1333+ }
1334+
12881335 if (preserve_merges_selected )
12891336 die (_ ("--preserve-merges was replaced by --rebase-merges\n"
12901337 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
@@ -1542,6 +1589,9 @@ int cmd_rebase(int argc,
15421589 if (options .root && !options .onto_name )
15431590 imply_merge (& options , "--root without --onto" );
15441591
1592+ if (options .trailer_args .nr )
1593+ imply_merge (& options , "--trailer" );
1594+
15451595 if (isatty (2 ) && options .flags & REBASE_NO_QUIET )
15461596 strbuf_addstr (& options .git_format_patch_opt , " --progress" );
15471597
0 commit comments