@@ -211,17 +211,29 @@ static GIT_PATH_FUNC(rebase_path_keep_redundant_commits, "rebase-merge/keep_redu
211
211
* A 'struct replay_ctx' represents the private state of the sequencer.
212
212
*/
213
213
struct replay_ctx {
214
+ /*
215
+ * The list of completed fixup and squash commands in the
216
+ * current chain.
217
+ */
218
+ struct strbuf current_fixups ;
214
219
/*
215
220
* Stores the reflog message that will be used when creating a
216
221
* commit. Points to a static buffer and should not be free()'d.
217
222
*/
218
223
const char * reflog_message ;
224
+ /*
225
+ * The number of completed fixup and squash commands in the
226
+ * current chain.
227
+ */
228
+ int current_fixup_count ;
219
229
};
220
230
221
231
struct replay_ctx * replay_ctx_new (void )
222
232
{
223
233
struct replay_ctx * ctx = xcalloc (1 , sizeof (* ctx ));
224
234
235
+ strbuf_init (& ctx -> current_fixups , 0 );
236
+
225
237
return ctx ;
226
238
}
227
239
@@ -384,17 +396,24 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
384
396
return buf .buf ;
385
397
}
386
398
399
+ static void replay_ctx_release (struct replay_ctx * ctx )
400
+ {
401
+ strbuf_release (& ctx -> current_fixups );
402
+ }
403
+
387
404
void replay_opts_release (struct replay_opts * opts )
388
405
{
406
+ struct replay_ctx * ctx = opts -> ctx ;
407
+
389
408
free (opts -> gpg_sign );
390
409
free (opts -> reflog_action );
391
410
free (opts -> default_strategy );
392
411
free (opts -> strategy );
393
412
strvec_clear (& opts -> xopts );
394
- strbuf_release (& opts -> current_fixups );
395
413
if (opts -> revs )
396
414
release_revisions (opts -> revs );
397
415
free (opts -> revs );
416
+ replay_ctx_release (ctx );
398
417
free (opts -> ctx );
399
418
}
400
419
@@ -1876,10 +1895,10 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
1876
1895
}
1877
1896
1878
1897
/* Does the current fixup chain contain a squash command? */
1879
- static int seen_squash (struct replay_opts * opts )
1898
+ static int seen_squash (struct replay_ctx * ctx )
1880
1899
{
1881
- return starts_with (opts -> current_fixups .buf , "squash" ) ||
1882
- strstr (opts -> current_fixups .buf , "\nsquash" );
1900
+ return starts_with (ctx -> current_fixups .buf , "squash" ) ||
1901
+ strstr (ctx -> current_fixups .buf , "\nsquash" );
1883
1902
}
1884
1903
1885
1904
static void update_comment_bufs (struct strbuf * buf1 , struct strbuf * buf2 , int n )
@@ -1955,6 +1974,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
1955
1974
enum todo_command command , struct replay_opts * opts ,
1956
1975
unsigned flag )
1957
1976
{
1977
+ struct replay_ctx * ctx = opts -> ctx ;
1958
1978
const char * fixup_msg ;
1959
1979
size_t commented_len = 0 , fixup_off ;
1960
1980
/*
@@ -1963,21 +1983,21 @@ static int append_squash_message(struct strbuf *buf, const char *body,
1963
1983
* squashing commit messages.
1964
1984
*/
1965
1985
if (starts_with (body , "amend!" ) ||
1966
- ((command == TODO_SQUASH || seen_squash (opts )) &&
1986
+ ((command == TODO_SQUASH || seen_squash (ctx )) &&
1967
1987
(starts_with (body , "squash!" ) || starts_with (body , "fixup!" ))))
1968
1988
commented_len = commit_subject_length (body );
1969
1989
1970
1990
strbuf_addf (buf , "\n%c " , comment_line_char );
1971
1991
strbuf_addf (buf , _ (nth_commit_msg_fmt ),
1972
- ++ opts -> current_fixup_count + 1 );
1992
+ ++ ctx -> current_fixup_count + 1 );
1973
1993
strbuf_addstr (buf , "\n\n" );
1974
1994
strbuf_add_commented_lines (buf , body , commented_len , comment_line_char );
1975
1995
/* buf->buf may be reallocated so store an offset into the buffer */
1976
1996
fixup_off = buf -> len ;
1977
1997
strbuf_addstr (buf , body + commented_len );
1978
1998
1979
1999
/* fixup -C after squash behaves like squash */
1980
- if (is_fixup_flag (command , flag ) && !seen_squash (opts )) {
2000
+ if (is_fixup_flag (command , flag ) && !seen_squash (ctx )) {
1981
2001
/*
1982
2002
* We're replacing the commit message so we need to
1983
2003
* append the Signed-off-by: trailer if the user
@@ -2011,12 +2031,13 @@ static int update_squash_messages(struct repository *r,
2011
2031
struct replay_opts * opts ,
2012
2032
unsigned flag )
2013
2033
{
2034
+ struct replay_ctx * ctx = opts -> ctx ;
2014
2035
struct strbuf buf = STRBUF_INIT ;
2015
2036
int res = 0 ;
2016
2037
const char * message , * body ;
2017
2038
const char * encoding = get_commit_output_encoding ();
2018
2039
2019
- if (opts -> current_fixup_count > 0 ) {
2040
+ if (ctx -> current_fixup_count > 0 ) {
2020
2041
struct strbuf header = STRBUF_INIT ;
2021
2042
char * eol ;
2022
2043
@@ -2029,10 +2050,10 @@ static int update_squash_messages(struct repository *r,
2029
2050
2030
2051
strbuf_addf (& header , "%c " , comment_line_char );
2031
2052
strbuf_addf (& header , _ (combined_commit_msg_fmt ),
2032
- opts -> current_fixup_count + 2 );
2053
+ ctx -> current_fixup_count + 2 );
2033
2054
strbuf_splice (& buf , 0 , eol - buf .buf , header .buf , header .len );
2034
2055
strbuf_release (& header );
2035
- if (is_fixup_flag (command , flag ) && !seen_squash (opts ))
2056
+ if (is_fixup_flag (command , flag ) && !seen_squash (ctx ))
2036
2057
update_squash_message_for_fixup (& buf );
2037
2058
} else {
2038
2059
struct object_id head ;
@@ -2079,7 +2100,7 @@ static int update_squash_messages(struct repository *r,
2079
2100
} else if (command == TODO_FIXUP ) {
2080
2101
strbuf_addf (& buf , "\n%c " , comment_line_char );
2081
2102
strbuf_addf (& buf , _ (skip_nth_commit_msg_fmt ),
2082
- ++ opts -> current_fixup_count + 1 );
2103
+ ++ ctx -> current_fixup_count + 1 );
2083
2104
strbuf_addstr (& buf , "\n\n" );
2084
2105
strbuf_add_commented_lines (& buf , body , strlen (body ),
2085
2106
comment_line_char );
@@ -2093,12 +2114,12 @@ static int update_squash_messages(struct repository *r,
2093
2114
strbuf_release (& buf );
2094
2115
2095
2116
if (!res ) {
2096
- strbuf_addf (& opts -> current_fixups , "%s%s %s" ,
2097
- opts -> current_fixups .len ? "\n" : "" ,
2117
+ strbuf_addf (& ctx -> current_fixups , "%s%s %s" ,
2118
+ ctx -> current_fixups .len ? "\n" : "" ,
2098
2119
command_to_string (command ),
2099
2120
oid_to_hex (& commit -> object .oid ));
2100
- res = write_message (opts -> current_fixups .buf ,
2101
- opts -> current_fixups .len ,
2121
+ res = write_message (ctx -> current_fixups .buf ,
2122
+ ctx -> current_fixups .len ,
2102
2123
rebase_path_current_fixups (), 0 );
2103
2124
}
2104
2125
@@ -2176,6 +2197,7 @@ static int do_pick_commit(struct repository *r,
2176
2197
struct replay_opts * opts ,
2177
2198
int final_fixup , int * check_todo )
2178
2199
{
2200
+ struct replay_ctx * ctx = opts -> ctx ;
2179
2201
unsigned int flags = should_edit (opts ) ? EDIT_MSG : 0 ;
2180
2202
const char * msg_file = should_edit (opts ) ? NULL : git_path_merge_msg (r );
2181
2203
struct object_id head ;
@@ -2456,8 +2478,8 @@ static int do_pick_commit(struct repository *r,
2456
2478
unlink (rebase_path_fixup_msg ());
2457
2479
unlink (rebase_path_squash_msg ());
2458
2480
unlink (rebase_path_current_fixups ());
2459
- strbuf_reset (& opts -> current_fixups );
2460
- opts -> current_fixup_count = 0 ;
2481
+ strbuf_reset (& ctx -> current_fixups );
2482
+ ctx -> current_fixup_count = 0 ;
2461
2483
}
2462
2484
2463
2485
leave :
@@ -3019,6 +3041,8 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
3019
3041
3020
3042
static int read_populate_opts (struct replay_opts * opts )
3021
3043
{
3044
+ struct replay_ctx * ctx = opts -> ctx ;
3045
+
3022
3046
if (is_rebase_i (opts )) {
3023
3047
struct strbuf buf = STRBUF_INIT ;
3024
3048
int ret = 0 ;
@@ -3078,13 +3102,13 @@ static int read_populate_opts(struct replay_opts *opts)
3078
3102
read_strategy_opts (opts , & buf );
3079
3103
strbuf_reset (& buf );
3080
3104
3081
- if (read_oneliner (& opts -> current_fixups ,
3105
+ if (read_oneliner (& ctx -> current_fixups ,
3082
3106
rebase_path_current_fixups (),
3083
3107
READ_ONELINER_SKIP_IF_EMPTY )) {
3084
- const char * p = opts -> current_fixups .buf ;
3085
- opts -> current_fixup_count = 1 ;
3108
+ const char * p = ctx -> current_fixups .buf ;
3109
+ ctx -> current_fixup_count = 1 ;
3086
3110
while ((p = strchr (p , '\n' ))) {
3087
- opts -> current_fixup_count ++ ;
3111
+ ctx -> current_fixup_count ++ ;
3088
3112
p ++ ;
3089
3113
}
3090
3114
}
@@ -5066,6 +5090,7 @@ static int commit_staged_changes(struct repository *r,
5066
5090
struct replay_opts * opts ,
5067
5091
struct todo_list * todo_list )
5068
5092
{
5093
+ struct replay_ctx * ctx = opts -> ctx ;
5069
5094
unsigned int flags = ALLOW_EMPTY | EDIT_MSG ;
5070
5095
unsigned int final_fixup = 0 , is_clean ;
5071
5096
@@ -5102,7 +5127,7 @@ static int commit_staged_changes(struct repository *r,
5102
5127
* the commit message and if there was a squash, let the user
5103
5128
* edit it.
5104
5129
*/
5105
- if (!is_clean || !opts -> current_fixup_count )
5130
+ if (!is_clean || !ctx -> current_fixup_count )
5106
5131
; /* this is not the final fixup */
5107
5132
else if (!oideq (& head , & to_amend ) ||
5108
5133
!file_exists (rebase_path_stopped_sha ())) {
@@ -5111,20 +5136,20 @@ static int commit_staged_changes(struct repository *r,
5111
5136
unlink (rebase_path_fixup_msg ());
5112
5137
unlink (rebase_path_squash_msg ());
5113
5138
unlink (rebase_path_current_fixups ());
5114
- strbuf_reset (& opts -> current_fixups );
5115
- opts -> current_fixup_count = 0 ;
5139
+ strbuf_reset (& ctx -> current_fixups );
5140
+ ctx -> current_fixup_count = 0 ;
5116
5141
}
5117
5142
} else {
5118
5143
/* we are in a fixup/squash chain */
5119
- const char * p = opts -> current_fixups .buf ;
5120
- int len = opts -> current_fixups .len ;
5144
+ const char * p = ctx -> current_fixups .buf ;
5145
+ int len = ctx -> current_fixups .len ;
5121
5146
5122
- opts -> current_fixup_count -- ;
5147
+ ctx -> current_fixup_count -- ;
5123
5148
if (!len )
5124
5149
BUG ("Incorrect current_fixups:\n%s" , p );
5125
5150
while (len && p [len - 1 ] != '\n' )
5126
5151
len -- ;
5127
- strbuf_setlen (& opts -> current_fixups , len );
5152
+ strbuf_setlen (& ctx -> current_fixups , len );
5128
5153
if (write_message (p , len , rebase_path_current_fixups (),
5129
5154
0 ) < 0 )
5130
5155
return error (_ ("could not write file: '%s'" ),
@@ -5141,7 +5166,7 @@ static int commit_staged_changes(struct repository *r,
5141
5166
* actually need to re-commit with a cleaned up commit
5142
5167
* message.
5143
5168
*/
5144
- if (opts -> current_fixup_count > 0 &&
5169
+ if (ctx -> current_fixup_count > 0 &&
5145
5170
!is_fixup (peek_command (todo_list , 0 ))) {
5146
5171
final_fixup = 1 ;
5147
5172
/*
@@ -5214,14 +5239,14 @@ static int commit_staged_changes(struct repository *r,
5214
5239
unlink (rebase_path_fixup_msg ());
5215
5240
unlink (rebase_path_squash_msg ());
5216
5241
}
5217
- if (opts -> current_fixup_count > 0 ) {
5242
+ if (ctx -> current_fixup_count > 0 ) {
5218
5243
/*
5219
5244
* Whether final fixup or not, we just cleaned up the commit
5220
5245
* message...
5221
5246
*/
5222
5247
unlink (rebase_path_current_fixups ());
5223
- strbuf_reset (& opts -> current_fixups );
5224
- opts -> current_fixup_count = 0 ;
5248
+ strbuf_reset (& ctx -> current_fixups );
5249
+ ctx -> current_fixup_count = 0 ;
5225
5250
}
5226
5251
return 0 ;
5227
5252
}
0 commit comments