@@ -99,6 +99,7 @@ struct rebase_options {
99
99
int allow_empty_message ;
100
100
int rebase_merges , rebase_cousins ;
101
101
char * strategy , * strategy_opts ;
102
+ struct strbuf git_format_patch_opt ;
102
103
};
103
104
104
105
static int is_interactive (struct rebase_options * opts )
@@ -380,6 +381,15 @@ static int run_specific_rebase(struct rebase_options *opts)
380
381
add_var (& script_snippet , "rebase_root" , opts -> root ? "t" : "" );
381
382
add_var (& script_snippet , "squash_onto" ,
382
383
opts -> squash_onto ? oid_to_hex (opts -> squash_onto ) : "" );
384
+ add_var (& script_snippet , "git_format_patch_opt" ,
385
+ opts -> git_format_patch_opt .buf );
386
+
387
+ if (is_interactive (opts ) &&
388
+ !(opts -> flags & REBASE_INTERACTIVE_EXPLICIT )) {
389
+ strbuf_addstr (& script_snippet ,
390
+ "GIT_EDITOR=:; export GIT_EDITOR; " );
391
+ opts -> autosquash = 0 ;
392
+ }
383
393
384
394
switch (opts -> type ) {
385
395
case REBASE_AM :
@@ -432,7 +442,8 @@ static int run_specific_rebase(struct rebase_options *opts)
432
442
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
433
443
434
444
static int reset_head (struct object_id * oid , const char * action ,
435
- const char * switch_to_branch , int detach_head )
445
+ const char * switch_to_branch , int detach_head ,
446
+ const char * reflog_orig_head , const char * reflog_head )
436
447
{
437
448
struct object_id head_oid ;
438
449
struct tree_desc desc ;
@@ -507,20 +518,26 @@ static int reset_head(struct object_id *oid, const char *action,
507
518
old_orig = & oid_old_orig ;
508
519
if (!get_oid ("HEAD" , & oid_orig )) {
509
520
orig = & oid_orig ;
510
- strbuf_addstr (& msg , "updating ORIG_HEAD" );
511
- update_ref (msg .buf , "ORIG_HEAD" , orig , old_orig , 0 ,
521
+ if (!reflog_orig_head ) {
522
+ strbuf_addstr (& msg , "updating ORIG_HEAD" );
523
+ reflog_orig_head = msg .buf ;
524
+ }
525
+ update_ref (reflog_orig_head , "ORIG_HEAD" , orig , old_orig , 0 ,
512
526
UPDATE_REFS_MSG_ON_ERR );
513
527
} else if (old_orig )
514
528
delete_ref (NULL , "ORIG_HEAD" , old_orig , 0 );
515
- strbuf_setlen (& msg , prefix_len );
516
- strbuf_addstr (& msg , "updating HEAD" );
529
+ if (!reflog_head ) {
530
+ strbuf_setlen (& msg , prefix_len );
531
+ strbuf_addstr (& msg , "updating HEAD" );
532
+ reflog_head = msg .buf ;
533
+ }
517
534
if (!switch_to_branch )
518
- ret = update_ref (msg . buf , "HEAD" , oid , orig , REF_NO_DEREF ,
535
+ ret = update_ref (reflog_head , "HEAD" , oid , orig , REF_NO_DEREF ,
519
536
UPDATE_REFS_MSG_ON_ERR );
520
537
else {
521
538
ret = create_symref ("HEAD" , switch_to_branch , msg .buf );
522
539
if (!ret )
523
- ret = update_ref (msg . buf , "HEAD" , oid , NULL , 0 ,
540
+ ret = update_ref (reflog_head , "HEAD" , oid , NULL , 0 ,
524
541
UPDATE_REFS_MSG_ON_ERR );
525
542
}
526
543
@@ -623,6 +640,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
623
640
return 0 ;
624
641
}
625
642
643
+ static void NORETURN error_on_missing_default_upstream (void )
644
+ {
645
+ struct branch * current_branch = branch_get (NULL );
646
+
647
+ printf (_ ("%s\n"
648
+ "Please specify which branch you want to rebase against.\n"
649
+ "See git-rebase(1) for details.\n"
650
+ "\n"
651
+ " git rebase '<branch>'\n"
652
+ "\n" ),
653
+ current_branch ? _ ("There is no tracking information for "
654
+ "the current branch." ) :
655
+ _ ("You are not currently on a branch." ));
656
+
657
+ if (current_branch ) {
658
+ const char * remote = current_branch -> remote_name ;
659
+
660
+ if (!remote )
661
+ remote = _ ("<remote>" );
662
+
663
+ printf (_ ("If you wish to set tracking information for this "
664
+ "branch you can do so with:\n"
665
+ "\n"
666
+ " git branch --set-upstream-to=%s/<branch> %s\n"
667
+ "\n" ),
668
+ remote , current_branch -> name );
669
+ }
670
+ exit (1 );
671
+ }
672
+
626
673
int cmd_rebase (int argc , const char * * argv , const char * prefix )
627
674
{
628
675
struct rebase_options options = {
@@ -631,6 +678,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
631
678
.git_am_opt = STRBUF_INIT ,
632
679
.allow_rerere_autoupdate = -1 ,
633
680
.allow_empty_message = 1 ,
681
+ .git_format_patch_opt = STRBUF_INIT ,
634
682
};
635
683
const char * branch_name ;
636
684
int ret , flags , total_argc , in_progress = 0 ;
@@ -871,7 +919,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
871
919
rerere_clear (& merge_rr );
872
920
string_list_clear (& merge_rr , 1 );
873
921
874
- if (reset_head (NULL , "reset" , NULL , 0 ) < 0 )
922
+ if (reset_head (NULL , "reset" , NULL , 0 , NULL , NULL ) < 0 )
875
923
die (_ ("could not discard worktree changes" ));
876
924
if (read_basic_state (& options ))
877
925
exit (1 );
@@ -887,7 +935,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
887
935
if (read_basic_state (& options ))
888
936
exit (1 );
889
937
if (reset_head (& options .orig_head , "reset" ,
890
- options .head_name , 0 ) < 0 )
938
+ options .head_name , 0 , NULL , NULL ) < 0 )
891
939
die (_ ("could not move back to %s" ),
892
940
oid_to_hex (& options .orig_head ));
893
941
ret = finish_rebase (& options );
@@ -1033,6 +1081,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1033
1081
if (options .root && !options .onto_name )
1034
1082
imply_interactive (& options , "--root without --onto" );
1035
1083
1084
+ if (isatty (2 ) && options .flags & REBASE_NO_QUIET )
1085
+ strbuf_addstr (& options .git_format_patch_opt , " --progress" );
1086
+
1036
1087
switch (options .type ) {
1037
1088
case REBASE_MERGE :
1038
1089
case REBASE_INTERACTIVE :
@@ -1049,6 +1100,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1049
1100
break ;
1050
1101
}
1051
1102
1103
+ if (options .git_am_opt .len ) {
1104
+ const char * p ;
1105
+
1106
+ /* all am options except -q are compatible only with --am */
1107
+ strbuf_reset (& buf );
1108
+ strbuf_addbuf (& buf , & options .git_am_opt );
1109
+ strbuf_addch (& buf , ' ' );
1110
+ while ((p = strstr (buf .buf , " -q " )))
1111
+ strbuf_splice (& buf , p - buf .buf , 4 , " " , 1 );
1112
+ strbuf_trim (& buf );
1113
+
1114
+ if (is_interactive (& options ) && buf .len )
1115
+ die (_ ("error: cannot combine interactive options "
1116
+ "(--interactive, --exec, --rebase-merges, "
1117
+ "--preserve-merges, --keep-empty, --root + "
1118
+ "--onto) with am options (%s)" ), buf .buf );
1119
+ if (options .type == REBASE_MERGE && buf .len )
1120
+ die (_ ("error: cannot combine merge options (--merge, "
1121
+ "--strategy, --strategy-option) with am options "
1122
+ "(%s)" ), buf .buf );
1123
+ }
1124
+
1052
1125
if (options .signoff ) {
1053
1126
if (options .type == REBASE_PRESERVE_MERGES )
1054
1127
die ("cannot combine '--signoff' with "
@@ -1057,10 +1130,37 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1057
1130
options .flags |= REBASE_FORCE ;
1058
1131
}
1059
1132
1133
+ if (options .type == REBASE_PRESERVE_MERGES )
1134
+ /*
1135
+ * Note: incompatibility with --signoff handled in signoff block above
1136
+ * Note: incompatibility with --interactive is just a strong warning;
1137
+ * git-rebase.txt caveats with "unless you know what you are doing"
1138
+ */
1139
+ if (options .rebase_merges )
1140
+ die (_ ("error: cannot combine '--preserve_merges' with "
1141
+ "'--rebase-merges'" ));
1142
+
1143
+ if (options .rebase_merges ) {
1144
+ if (strategy_options .nr )
1145
+ die (_ ("error: cannot combine '--rebase_merges' with "
1146
+ "'--strategy-option'" ));
1147
+ if (options .strategy )
1148
+ die (_ ("error: cannot combine '--rebase_merges' with "
1149
+ "'--strategy'" ));
1150
+ }
1151
+
1060
1152
if (!options .root ) {
1061
- if (argc < 1 )
1062
- die ("TODO: handle @{upstream}" );
1063
- else {
1153
+ if (argc < 1 ) {
1154
+ struct branch * branch ;
1155
+
1156
+ branch = branch_get (NULL );
1157
+ options .upstream_name = branch_get_upstream (branch ,
1158
+ NULL );
1159
+ if (!options .upstream_name )
1160
+ error_on_missing_default_upstream ();
1161
+ if (fork_point < 0 )
1162
+ fork_point = 1 ;
1163
+ } else {
1064
1164
options .upstream_name = argv [0 ];
1065
1165
argc -- ;
1066
1166
argv ++ ;
@@ -1199,7 +1299,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1199
1299
write_file (autostash , "%s" , buf .buf );
1200
1300
printf (_ ("Created autostash: %s\n" ), buf .buf );
1201
1301
if (reset_head (& head -> object .oid , "reset --hard" ,
1202
- NULL , 0 ) < 0 )
1302
+ NULL , 0 , NULL , NULL ) < 0 )
1203
1303
die (_ ("could not reset --hard" ));
1204
1304
printf (_ ("HEAD is now at %s" ),
1205
1305
find_unique_abbrev (& head -> object .oid ,
@@ -1253,7 +1353,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1253
1353
strbuf_addf (& buf , "rebase: checkout %s" ,
1254
1354
options .switch_to );
1255
1355
if (reset_head (& oid , "checkout" ,
1256
- options .head_name , 0 ) < 0 ) {
1356
+ options .head_name , 0 ,
1357
+ NULL , NULL ) < 0 ) {
1257
1358
ret = !!error (_ ("could not switch to "
1258
1359
"%s" ),
1259
1360
options .switch_to );
@@ -1318,10 +1419,29 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1318
1419
"it...\n" ));
1319
1420
1320
1421
strbuf_addf (& msg , "rebase: checkout %s" , options .onto_name );
1321
- if (reset_head (& options .onto -> object .oid , "checkout" , NULL , 1 ))
1422
+ if (reset_head (& options .onto -> object .oid , "checkout" , NULL , 1 ,
1423
+ NULL , msg .buf ))
1322
1424
die (_ ("Could not detach HEAD" ));
1323
1425
strbuf_release (& msg );
1324
1426
1427
+ /*
1428
+ * If the onto is a proper descendant of the tip of the branch, then
1429
+ * we just fast-forwarded.
1430
+ */
1431
+ strbuf_reset (& msg );
1432
+ if (!oidcmp (& merge_base , & options .orig_head )) {
1433
+ printf (_ ("Fast-forwarded %s to %s. \n" ),
1434
+ branch_name , options .onto_name );
1435
+ strbuf_addf (& msg , "rebase finished: %s onto %s" ,
1436
+ options .head_name ? options .head_name : "detached HEAD" ,
1437
+ oid_to_hex (& options .onto -> object .oid ));
1438
+ reset_head (NULL , "Fast-forwarded" , options .head_name , 0 ,
1439
+ "HEAD" , msg .buf );
1440
+ strbuf_release (& msg );
1441
+ ret = !!finish_rebase (& options );
1442
+ goto cleanup ;
1443
+ }
1444
+
1325
1445
strbuf_addf (& revisions , "%s..%s" ,
1326
1446
options .root ? oid_to_hex (& options .onto -> object .oid ) :
1327
1447
(options .restrict_revision ?
0 commit comments