@@ -246,6 +246,37 @@ static int read_basic_state(struct rebase_options *opts)
246
246
return 0 ;
247
247
}
248
248
249
+ static int write_basic_state (struct rebase_options * opts )
250
+ {
251
+ write_file (state_dir_path ("head-name" , opts ), "%s" ,
252
+ opts -> head_name ? opts -> head_name : "detached HEAD" );
253
+ write_file (state_dir_path ("onto" , opts ), "%s" ,
254
+ opts -> onto ? oid_to_hex (& opts -> onto -> object .oid ) : "" );
255
+ write_file (state_dir_path ("orig-head" , opts ), "%s" ,
256
+ oid_to_hex (& opts -> orig_head ));
257
+ write_file (state_dir_path ("quiet" , opts ), "%s" ,
258
+ opts -> flags & REBASE_NO_QUIET ? "" : "t" );
259
+ if (opts -> flags & REBASE_VERBOSE )
260
+ write_file (state_dir_path ("verbose" , opts ), "%s" , "" );
261
+ if (opts -> strategy )
262
+ write_file (state_dir_path ("strategy" , opts ), "%s" ,
263
+ opts -> strategy );
264
+ if (opts -> strategy_opts )
265
+ write_file (state_dir_path ("strategy_opts" , opts ), "%s" ,
266
+ opts -> strategy_opts );
267
+ if (opts -> allow_rerere_autoupdate >= 0 )
268
+ write_file (state_dir_path ("allow_rerere_autoupdate" , opts ),
269
+ "-%s-rerere-autoupdate" ,
270
+ opts -> allow_rerere_autoupdate ? "" : "-no" );
271
+ if (opts -> gpg_sign_opt )
272
+ write_file (state_dir_path ("gpg_sign_opt" , opts ), "%s" ,
273
+ opts -> gpg_sign_opt );
274
+ if (opts -> signoff )
275
+ write_file (state_dir_path ("strategy" , opts ), "--signoff" );
276
+
277
+ return 0 ;
278
+ }
279
+
249
280
static int apply_autostash (struct rebase_options * opts )
250
281
{
251
282
const char * path = state_dir_path ("autostash" , opts );
@@ -453,13 +484,159 @@ static int reset_head(struct object_id *oid, const char *action,
453
484
return ret ;
454
485
}
455
486
487
+ static int move_to_original_branch (struct rebase_options * opts )
488
+ {
489
+ struct strbuf orig_head_reflog = STRBUF_INIT , head_reflog = STRBUF_INIT ;
490
+ int ret ;
491
+
492
+ if (!opts -> head_name )
493
+ return 0 ; /* nothing to move back to */
494
+
495
+ if (!opts -> onto )
496
+ BUG ("move_to_original_branch without onto" );
497
+
498
+ strbuf_addf (& orig_head_reflog , "rebase finished: %s onto %s" ,
499
+ opts -> head_name , oid_to_hex (& opts -> onto -> object .oid ));
500
+ strbuf_addf (& head_reflog , "rebase finished: returning to %s" ,
501
+ opts -> head_name );
502
+ ret = reset_head (NULL , "checkout" , opts -> head_name , 0 ,
503
+ orig_head_reflog .buf , head_reflog .buf );
504
+
505
+ strbuf_release (& orig_head_reflog );
506
+ strbuf_release (& head_reflog );
507
+ return ret ;
508
+ }
509
+
456
510
static const char * resolvemsg =
457
511
N_ ("Resolve all conflicts manually, mark them as resolved with\n"
458
512
"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
459
513
"You can instead skip this commit: run \"git rebase --skip\".\n"
460
514
"To abort and get back to the state before \"git rebase\", run "
461
515
"\"git rebase --abort\"." );
462
516
517
+ static int run_am (struct rebase_options * opts )
518
+ {
519
+ struct child_process am = CHILD_PROCESS_INIT ;
520
+ struct child_process format_patch = CHILD_PROCESS_INIT ;
521
+ struct strbuf revisions = STRBUF_INIT ;
522
+ int status ;
523
+ char * rebased_patches ;
524
+
525
+ am .git_cmd = 1 ;
526
+ argv_array_push (& am .args , "am" );
527
+
528
+ if (opts -> action && !strcmp ("continue" , opts -> action )) {
529
+ argv_array_push (& am .args , "--resolved" );
530
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
531
+ if (opts -> gpg_sign_opt )
532
+ argv_array_push (& am .args , opts -> gpg_sign_opt );
533
+ status = run_command (& am );
534
+ if (status )
535
+ return status ;
536
+
537
+ discard_cache ();
538
+ return move_to_original_branch (opts );
539
+ }
540
+ if (opts -> action && !strcmp ("skip" , opts -> action )) {
541
+ argv_array_push (& am .args , "--skip" );
542
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
543
+ status = run_command (& am );
544
+ if (status )
545
+ return status ;
546
+
547
+ discard_cache ();
548
+ return move_to_original_branch (opts );
549
+ }
550
+ if (opts -> action && !strcmp ("show-current-patch" , opts -> action )) {
551
+ argv_array_push (& am .args , "--show-current-patch" );
552
+ return run_command (& am );
553
+ }
554
+
555
+ strbuf_addf (& revisions , "%s...%s" ,
556
+ oid_to_hex (opts -> root ?
557
+ /* this is now equivalent to ! -z "$upstream" */
558
+ & opts -> onto -> object .oid :
559
+ & opts -> upstream -> object .oid ),
560
+ oid_to_hex (& opts -> orig_head ));
561
+
562
+ rebased_patches = xstrdup (git_path ("rebased-patches" ));
563
+ format_patch .out = open (rebased_patches ,
564
+ O_WRONLY | O_CREAT | O_TRUNC , 0666 );
565
+ if (format_patch .out < 0 ) {
566
+ status = error_errno (_ ("could not write '%s'" ),
567
+ rebased_patches );
568
+ free (rebased_patches );
569
+ argv_array_clear (& am .args );
570
+ return status ;
571
+ }
572
+
573
+ format_patch .git_cmd = 1 ;
574
+ argv_array_pushl (& format_patch .args , "format-patch" , "-k" , "--stdout" ,
575
+ "--full-index" , "--cherry-pick" , "--right-only" ,
576
+ "--src-prefix=a/" , "--dst-prefix=b/" , "--no-renames" ,
577
+ "--no-cover-letter" , "--pretty=mboxrd" , NULL );
578
+ if (opts -> git_format_patch_opt .len )
579
+ argv_array_split (& format_patch .args ,
580
+ opts -> git_format_patch_opt .buf );
581
+ argv_array_push (& format_patch .args , revisions .buf );
582
+ if (opts -> restrict_revision )
583
+ argv_array_pushf (& format_patch .args , "^%s" ,
584
+ oid_to_hex (& opts -> restrict_revision -> object .oid ));
585
+
586
+ status = run_command (& format_patch );
587
+ if (status ) {
588
+ unlink (rebased_patches );
589
+ free (rebased_patches );
590
+ argv_array_clear (& am .args );
591
+
592
+ reset_head (& opts -> orig_head , "checkout" , opts -> head_name , 0 ,
593
+ "HEAD" , NULL );
594
+ error (_ ("\ngit encountered an error while preparing the "
595
+ "patches to replay\n"
596
+ "these revisions:\n"
597
+ "\n %s\n\n"
598
+ "As a result, git cannot rebase them." ),
599
+ opts -> revisions );
600
+
601
+ strbuf_release (& revisions );
602
+ return status ;
603
+ }
604
+ strbuf_release (& revisions );
605
+
606
+ am .in = open (rebased_patches , O_RDONLY );
607
+ if (am .in < 0 ) {
608
+ status = error_errno (_ ("could not read '%s'" ),
609
+ rebased_patches );
610
+ free (rebased_patches );
611
+ argv_array_clear (& am .args );
612
+ return status ;
613
+ }
614
+
615
+ argv_array_pushv (& am .args , opts -> git_am_opts .argv );
616
+ argv_array_push (& am .args , "--rebasing" );
617
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
618
+ argv_array_push (& am .args , "--patch-format=mboxrd" );
619
+ if (opts -> allow_rerere_autoupdate > 0 )
620
+ argv_array_push (& am .args , "--rerere-autoupdate" );
621
+ else if (opts -> allow_rerere_autoupdate == 0 )
622
+ argv_array_push (& am .args , "--no-rerere-autoupdate" );
623
+ if (opts -> gpg_sign_opt )
624
+ argv_array_push (& am .args , opts -> gpg_sign_opt );
625
+ status = run_command (& am );
626
+ unlink (rebased_patches );
627
+ free (rebased_patches );
628
+
629
+ if (!status ) {
630
+ discard_cache ();
631
+ return move_to_original_branch (opts );
632
+ }
633
+
634
+ if (is_directory (opts -> state_dir ))
635
+ write_basic_state (opts );
636
+
637
+ return status ;
638
+ }
639
+
463
640
static int run_specific_rebase (struct rebase_options * opts )
464
641
{
465
642
const char * argv [] = { NULL , NULL };
@@ -540,6 +717,11 @@ static int run_specific_rebase(struct rebase_options *opts)
540
717
goto finished_rebase ;
541
718
}
542
719
720
+ if (opts -> type == REBASE_AM ) {
721
+ status = run_am (opts );
722
+ goto finished_rebase ;
723
+ }
724
+
543
725
add_var (& script_snippet , "GIT_DIR" , absolute_path (get_git_dir ()));
544
726
add_var (& script_snippet , "state_dir" , opts -> state_dir );
545
727
0 commit comments