@@ -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 );
@@ -459,13 +490,160 @@ static int reset_head(struct object_id *oid, const char *action,
459
490
return ret ;
460
491
}
461
492
493
+ static int move_to_original_branch (struct rebase_options * opts )
494
+ {
495
+ struct strbuf orig_head_reflog = STRBUF_INIT , head_reflog = STRBUF_INIT ;
496
+ int ret ;
497
+
498
+ if (!opts -> head_name )
499
+ return 0 ; /* nothing to move back to */
500
+
501
+ if (!opts -> onto )
502
+ BUG ("move_to_original_branch without onto" );
503
+
504
+ strbuf_addf (& orig_head_reflog , "rebase finished: %s onto %s" ,
505
+ opts -> head_name , oid_to_hex (& opts -> onto -> object .oid ));
506
+ strbuf_addf (& head_reflog , "rebase finished: returning to %s" ,
507
+ opts -> head_name );
508
+ ret = reset_head (NULL , "checkout" , opts -> head_name ,
509
+ RESET_HEAD_REFS_ONLY ,
510
+ orig_head_reflog .buf , head_reflog .buf );
511
+
512
+ strbuf_release (& orig_head_reflog );
513
+ strbuf_release (& head_reflog );
514
+ return ret ;
515
+ }
516
+
462
517
static const char * resolvemsg =
463
518
N_ ("Resolve all conflicts manually, mark them as resolved with\n"
464
519
"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
465
520
"You can instead skip this commit: run \"git rebase --skip\".\n"
466
521
"To abort and get back to the state before \"git rebase\", run "
467
522
"\"git rebase --abort\"." );
468
523
524
+ static int run_am (struct rebase_options * opts )
525
+ {
526
+ struct child_process am = CHILD_PROCESS_INIT ;
527
+ struct child_process format_patch = CHILD_PROCESS_INIT ;
528
+ struct strbuf revisions = STRBUF_INIT ;
529
+ int status ;
530
+ char * rebased_patches ;
531
+
532
+ am .git_cmd = 1 ;
533
+ argv_array_push (& am .args , "am" );
534
+
535
+ if (opts -> action && !strcmp ("continue" , opts -> action )) {
536
+ argv_array_push (& am .args , "--resolved" );
537
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
538
+ if (opts -> gpg_sign_opt )
539
+ argv_array_push (& am .args , opts -> gpg_sign_opt );
540
+ status = run_command (& am );
541
+ if (status )
542
+ return status ;
543
+
544
+ discard_cache ();
545
+ return move_to_original_branch (opts );
546
+ }
547
+ if (opts -> action && !strcmp ("skip" , opts -> action )) {
548
+ argv_array_push (& am .args , "--skip" );
549
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
550
+ status = run_command (& am );
551
+ if (status )
552
+ return status ;
553
+
554
+ discard_cache ();
555
+ return move_to_original_branch (opts );
556
+ }
557
+ if (opts -> action && !strcmp ("show-current-patch" , opts -> action )) {
558
+ argv_array_push (& am .args , "--show-current-patch" );
559
+ return run_command (& am );
560
+ }
561
+
562
+ strbuf_addf (& revisions , "%s...%s" ,
563
+ oid_to_hex (opts -> root ?
564
+ /* this is now equivalent to ! -z "$upstream" */
565
+ & opts -> onto -> object .oid :
566
+ & opts -> upstream -> object .oid ),
567
+ oid_to_hex (& opts -> orig_head ));
568
+
569
+ rebased_patches = xstrdup (git_path ("rebased-patches" ));
570
+ format_patch .out = open (rebased_patches ,
571
+ O_WRONLY | O_CREAT | O_TRUNC , 0666 );
572
+ if (format_patch .out < 0 ) {
573
+ status = error_errno (_ ("could not write '%s'" ),
574
+ rebased_patches );
575
+ free (rebased_patches );
576
+ argv_array_clear (& am .args );
577
+ return status ;
578
+ }
579
+
580
+ format_patch .git_cmd = 1 ;
581
+ argv_array_pushl (& format_patch .args , "format-patch" , "-k" , "--stdout" ,
582
+ "--full-index" , "--cherry-pick" , "--right-only" ,
583
+ "--src-prefix=a/" , "--dst-prefix=b/" , "--no-renames" ,
584
+ "--no-cover-letter" , "--pretty=mboxrd" , NULL );
585
+ if (opts -> git_format_patch_opt .len )
586
+ argv_array_split (& format_patch .args ,
587
+ opts -> git_format_patch_opt .buf );
588
+ argv_array_push (& format_patch .args , revisions .buf );
589
+ if (opts -> restrict_revision )
590
+ argv_array_pushf (& format_patch .args , "^%s" ,
591
+ oid_to_hex (& opts -> restrict_revision -> object .oid ));
592
+
593
+ status = run_command (& format_patch );
594
+ if (status ) {
595
+ unlink (rebased_patches );
596
+ free (rebased_patches );
597
+ argv_array_clear (& am .args );
598
+
599
+ reset_head (& opts -> orig_head , "checkout" , opts -> head_name , 0 ,
600
+ "HEAD" , NULL );
601
+ error (_ ("\ngit encountered an error while preparing the "
602
+ "patches to replay\n"
603
+ "these revisions:\n"
604
+ "\n %s\n\n"
605
+ "As a result, git cannot rebase them." ),
606
+ opts -> revisions );
607
+
608
+ strbuf_release (& revisions );
609
+ return status ;
610
+ }
611
+ strbuf_release (& revisions );
612
+
613
+ am .in = open (rebased_patches , O_RDONLY );
614
+ if (am .in < 0 ) {
615
+ status = error_errno (_ ("could not read '%s'" ),
616
+ rebased_patches );
617
+ free (rebased_patches );
618
+ argv_array_clear (& am .args );
619
+ return status ;
620
+ }
621
+
622
+ argv_array_pushv (& am .args , opts -> git_am_opts .argv );
623
+ argv_array_push (& am .args , "--rebasing" );
624
+ argv_array_pushf (& am .args , "--resolvemsg=%s" , resolvemsg );
625
+ argv_array_push (& am .args , "--patch-format=mboxrd" );
626
+ if (opts -> allow_rerere_autoupdate > 0 )
627
+ argv_array_push (& am .args , "--rerere-autoupdate" );
628
+ else if (opts -> allow_rerere_autoupdate == 0 )
629
+ argv_array_push (& am .args , "--no-rerere-autoupdate" );
630
+ if (opts -> gpg_sign_opt )
631
+ argv_array_push (& am .args , opts -> gpg_sign_opt );
632
+ status = run_command (& am );
633
+ unlink (rebased_patches );
634
+ free (rebased_patches );
635
+
636
+ if (!status ) {
637
+ discard_cache ();
638
+ return move_to_original_branch (opts );
639
+ }
640
+
641
+ if (is_directory (opts -> state_dir ))
642
+ write_basic_state (opts );
643
+
644
+ return status ;
645
+ }
646
+
469
647
static int run_specific_rebase (struct rebase_options * opts )
470
648
{
471
649
const char * argv [] = { NULL , NULL };
@@ -546,6 +724,11 @@ static int run_specific_rebase(struct rebase_options *opts)
546
724
goto finished_rebase ;
547
725
}
548
726
727
+ if (opts -> type == REBASE_AM ) {
728
+ status = run_am (opts );
729
+ goto finished_rebase ;
730
+ }
731
+
549
732
add_var (& script_snippet , "GIT_DIR" , absolute_path (get_git_dir ()));
550
733
add_var (& script_snippet , "state_dir" , opts -> state_dir );
551
734
0 commit comments