@@ -2494,60 +2494,47 @@ int sequencer_make_script(int keep_empty, FILE *out,
2494
2494
}
2495
2495
2496
2496
2497
- int transform_todo_ids (int shorten_ids )
2497
+ int transform_todos (int shorten_ids )
2498
2498
{
2499
2499
const char * todo_file = rebase_path_todo ();
2500
2500
struct todo_list todo_list = TODO_LIST_INIT ;
2501
- int fd , res , i ;
2502
- FILE * out ;
2501
+ struct strbuf buf = STRBUF_INIT ;
2502
+ struct todo_item * item ;
2503
+ int i ;
2503
2504
2504
- strbuf_reset (& todo_list .buf );
2505
- fd = open (todo_file , O_RDONLY );
2506
- if (fd < 0 )
2507
- return error_errno (_ ("could not open '%s'" ), todo_file );
2508
- if (strbuf_read (& todo_list .buf , fd , 0 ) < 0 ) {
2509
- close (fd );
2505
+ if (strbuf_read_file (& todo_list .buf , todo_file , 0 ) < 0 )
2510
2506
return error (_ ("could not read '%s'." ), todo_file );
2511
- }
2512
- close (fd );
2513
2507
2514
- res = parse_insn_buffer (todo_list .buf .buf , & todo_list );
2515
- if (res ) {
2508
+ if (parse_insn_buffer (todo_list .buf .buf , & todo_list )) {
2516
2509
todo_list_release (& todo_list );
2517
2510
return error (_ ("unusable todo list: '%s'" ), todo_file );
2518
2511
}
2519
2512
2520
- out = fopen (todo_file , "w" );
2521
- if (!out ) {
2522
- todo_list_release (& todo_list );
2523
- return error (_ ("unable to open '%s' for writing" ), todo_file );
2524
- }
2525
- for (i = 0 ; i < todo_list .nr ; i ++ ) {
2526
- struct todo_item * item = todo_list .items + i ;
2527
- int bol = item -> offset_in_buf ;
2528
- const char * p = todo_list .buf .buf + bol ;
2529
- int eol = i + 1 < todo_list .nr ?
2530
- todo_list .items [i + 1 ].offset_in_buf :
2531
- todo_list .buf .len ;
2532
-
2533
- if (item -> command >= TODO_EXEC && item -> command != TODO_DROP )
2534
- fwrite (p , eol - bol , 1 , out );
2535
- else {
2536
- const char * id = shorten_ids ?
2537
- short_commit_name (item -> commit ) :
2538
- oid_to_hex (& item -> commit -> object .oid );
2539
- int len ;
2540
-
2541
- p += strspn (p , " \t" ); /* left-trim command */
2542
- len = strcspn (p , " \t" ); /* length of command */
2543
-
2544
- fprintf (out , "%.*s %s %.*s\n" ,
2545
- len , p , id , item -> arg_len , item -> arg );
2513
+ for (item = todo_list .items , i = 0 ; i < todo_list .nr ; i ++ , item ++ ) {
2514
+ /* if the item is not a command write it and continue */
2515
+ if (item -> command >= TODO_COMMENT ) {
2516
+ strbuf_addf (& buf , "%.*s\n" , item -> arg_len , item -> arg );
2517
+ continue ;
2518
+ }
2519
+
2520
+ /* add command to the buffer */
2521
+ strbuf_addstr (& buf , command_to_string (item -> command ));
2522
+
2523
+ /* add commit id */
2524
+ if (item -> commit ) {
2525
+ const char * oid = shorten_ids ?
2526
+ short_commit_name (item -> commit ) :
2527
+ oid_to_hex (& item -> commit -> object .oid );
2528
+
2529
+ strbuf_addf (& buf , " %s" , oid );
2546
2530
}
2531
+ /* add all the rest */
2532
+ strbuf_addf (& buf , " %.*s\n" , item -> arg_len , item -> arg );
2547
2533
}
2548
- fclose (out );
2534
+
2535
+ i = write_message (buf .buf , buf .len , todo_file , 0 );
2549
2536
todo_list_release (& todo_list );
2550
- return 0 ;
2537
+ return i ;
2551
2538
}
2552
2539
2553
2540
enum check_level {
0 commit comments