@@ -1870,6 +1870,23 @@ static int count_commands(struct todo_list *todo_list)
1870
1870
return count ;
1871
1871
}
1872
1872
1873
+ static int get_item_line_offset (struct todo_list * todo_list , int index )
1874
+ {
1875
+ return index < todo_list -> nr ?
1876
+ todo_list -> items [index ].offset_in_buf : todo_list -> buf .len ;
1877
+ }
1878
+
1879
+ static const char * get_item_line (struct todo_list * todo_list , int index )
1880
+ {
1881
+ return todo_list -> buf .buf + get_item_line_offset (todo_list , index );
1882
+ }
1883
+
1884
+ static int get_item_line_length (struct todo_list * todo_list , int index )
1885
+ {
1886
+ return get_item_line_offset (todo_list , index + 1 )
1887
+ - get_item_line_offset (todo_list , index );
1888
+ }
1889
+
1873
1890
static ssize_t strbuf_read_file_or_whine (struct strbuf * sb , const char * path )
1874
1891
{
1875
1892
int fd ;
@@ -2244,29 +2261,27 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
2244
2261
fd = hold_lock_file_for_update (& todo_lock , todo_path , 0 );
2245
2262
if (fd < 0 )
2246
2263
return error_errno (_ ("could not lock '%s'" ), todo_path );
2247
- offset = next < todo_list -> nr ?
2248
- todo_list -> items [next ].offset_in_buf : todo_list -> buf .len ;
2264
+ offset = get_item_line_offset (todo_list , next );
2249
2265
if (write_in_full (fd , todo_list -> buf .buf + offset ,
2250
2266
todo_list -> buf .len - offset ) < 0 )
2251
2267
return error_errno (_ ("could not write to '%s'" ), todo_path );
2252
2268
if (commit_lock_file (& todo_lock ) < 0 )
2253
2269
return error (_ ("failed to finalize '%s'" ), todo_path );
2254
2270
2255
- if (is_rebase_i (opts )) {
2256
- const char * done_path = rebase_path_done ();
2257
- int fd = open (done_path , O_CREAT | O_WRONLY | O_APPEND , 0666 );
2258
- int prev_offset = !next ? 0 :
2259
- todo_list -> items [next - 1 ].offset_in_buf ;
2271
+ if (is_rebase_i (opts ) && next > 0 ) {
2272
+ const char * done = rebase_path_done ();
2273
+ int fd = open (done , O_CREAT | O_WRONLY | O_APPEND , 0666 );
2274
+ int ret = 0 ;
2260
2275
2261
- if (fd >= 0 && offset > prev_offset &&
2262
- write_in_full ( fd , todo_list -> buf . buf + prev_offset ,
2263
- offset - prev_offset ) < 0 ) {
2264
- close ( fd );
2265
- return error_errno ( _ ( "could not write to '%s'" ),
2266
- done_path );
2267
- }
2268
- if ( fd >= 0 )
2269
- close ( fd ) ;
2276
+ if (fd < 0 )
2277
+ return 0 ;
2278
+ if ( write_in_full ( fd , get_item_line ( todo_list , next - 1 ),
2279
+ get_item_line_length ( todo_list , next - 1 ))
2280
+ < 0 )
2281
+ ret = error_errno ( _ ( "could not write to '%s'" ), done );
2282
+ if ( close ( fd ) < 0 )
2283
+ ret = error_errno ( _ ( "failed to finalize '%s'" ), done );
2284
+ return ret ;
2270
2285
}
2271
2286
return 0 ;
2272
2287
}
@@ -3297,8 +3312,7 @@ int skip_unnecessary_picks(void)
3297
3312
oid = & item -> commit -> object .oid ;
3298
3313
}
3299
3314
if (i > 0 ) {
3300
- int offset = i < todo_list .nr ?
3301
- todo_list .items [i ].offset_in_buf : todo_list .buf .len ;
3315
+ int offset = get_item_line_offset (& todo_list , i );
3302
3316
const char * done_path = rebase_path_done ();
3303
3317
3304
3318
fd = open (done_path , O_CREAT | O_WRONLY | O_APPEND , 0666 );
@@ -3478,12 +3492,10 @@ int rearrange_squash(void)
3478
3492
continue ;
3479
3493
3480
3494
while (cur >= 0 ) {
3481
- int offset = todo_list .items [cur ].offset_in_buf ;
3482
- int end_offset = cur + 1 < todo_list .nr ?
3483
- todo_list .items [cur + 1 ].offset_in_buf :
3484
- todo_list .buf .len ;
3485
- char * bol = todo_list .buf .buf + offset ;
3486
- char * eol = todo_list .buf .buf + end_offset ;
3495
+ const char * bol =
3496
+ get_item_line (& todo_list , cur );
3497
+ const char * eol =
3498
+ get_item_line (& todo_list , cur + 1 );
3487
3499
3488
3500
/* replace 'pick', by 'fixup' or 'squash' */
3489
3501
command = todo_list .items [cur ].command ;
0 commit comments