@@ -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 ;
@@ -870,7 +918,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
870
918
rerere_clear (& merge_rr );
871
919
string_list_clear (& merge_rr , 1 );
872
920
873
- if (reset_head (NULL , "reset" , NULL , 0 ) < 0 )
921
+ if (reset_head (NULL , "reset" , NULL , 0 , NULL , NULL ) < 0 )
874
922
die (_ ("could not discard worktree changes" ));
875
923
if (read_basic_state (& options ))
876
924
exit (1 );
@@ -886,7 +934,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
886
934
if (read_basic_state (& options ))
887
935
exit (1 );
888
936
if (reset_head (& options .orig_head , "reset" ,
889
- options .head_name , 0 ) < 0 )
937
+ options .head_name , 0 , NULL , NULL ) < 0 )
890
938
die (_ ("could not move back to %s" ),
891
939
oid_to_hex (& options .orig_head ));
892
940
ret = finish_rebase (& options );
@@ -1032,6 +1080,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1032
1080
if (options .root && !options .onto_name )
1033
1081
imply_interactive (& options , "--root without --onto" );
1034
1082
1083
+ if (isatty (2 ) && options .flags & REBASE_NO_QUIET )
1084
+ strbuf_addstr (& options .git_format_patch_opt , " --progress" );
1085
+
1035
1086
switch (options .type ) {
1036
1087
case REBASE_MERGE :
1037
1088
case REBASE_INTERACTIVE :
@@ -1048,6 +1099,28 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1048
1099
break ;
1049
1100
}
1050
1101
1102
+ if (options .git_am_opt .len ) {
1103
+ const char * p ;
1104
+
1105
+ /* all am options except -q are compatible only with --am */
1106
+ strbuf_reset (& buf );
1107
+ strbuf_addbuf (& buf , & options .git_am_opt );
1108
+ strbuf_addch (& buf , ' ' );
1109
+ while ((p = strstr (buf .buf , " -q " )))
1110
+ strbuf_splice (& buf , p - buf .buf , 4 , " " , 1 );
1111
+ strbuf_trim (& buf );
1112
+
1113
+ if (is_interactive (& options ) && buf .len )
1114
+ die (_ ("error: cannot combine interactive options "
1115
+ "(--interactive, --exec, --rebase-merges, "
1116
+ "--preserve-merges, --keep-empty, --root + "
1117
+ "--onto) with am options (%s)" ), buf .buf );
1118
+ if (options .type == REBASE_MERGE && buf .len )
1119
+ die (_ ("error: cannot combine merge options (--merge, "
1120
+ "--strategy, --strategy-option) with am options "
1121
+ "(%s)" ), buf .buf );
1122
+ }
1123
+
1051
1124
if (options .signoff ) {
1052
1125
if (options .type == REBASE_PRESERVE_MERGES )
1053
1126
die ("cannot combine '--signoff' with "
@@ -1056,10 +1129,37 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1056
1129
options .flags |= REBASE_FORCE ;
1057
1130
}
1058
1131
1132
+ if (options .type == REBASE_PRESERVE_MERGES )
1133
+ /*
1134
+ * Note: incompatibility with --signoff handled in signoff block above
1135
+ * Note: incompatibility with --interactive is just a strong warning;
1136
+ * git-rebase.txt caveats with "unless you know what you are doing"
1137
+ */
1138
+ if (options .rebase_merges )
1139
+ die (_ ("error: cannot combine '--preserve_merges' with "
1140
+ "'--rebase-merges'" ));
1141
+
1142
+ if (options .rebase_merges ) {
1143
+ if (strategy_options .nr )
1144
+ die (_ ("error: cannot combine '--rebase_merges' with "
1145
+ "'--strategy-option'" ));
1146
+ if (options .strategy )
1147
+ die (_ ("error: cannot combine '--rebase_merges' with "
1148
+ "'--strategy'" ));
1149
+ }
1150
+
1059
1151
if (!options .root ) {
1060
- if (argc < 1 )
1061
- die ("TODO: handle @{upstream}" );
1062
- else {
1152
+ if (argc < 1 ) {
1153
+ struct branch * branch ;
1154
+
1155
+ branch = branch_get (NULL );
1156
+ options .upstream_name = branch_get_upstream (branch ,
1157
+ NULL );
1158
+ if (!options .upstream_name )
1159
+ error_on_missing_default_upstream ();
1160
+ if (fork_point < 0 )
1161
+ fork_point = 1 ;
1162
+ } else {
1063
1163
options .upstream_name = argv [0 ];
1064
1164
argc -- ;
1065
1165
argv ++ ;
@@ -1198,7 +1298,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1198
1298
write_file (autostash , "%s" , buf .buf );
1199
1299
printf (_ ("Created autostash: %s\n" ), buf .buf );
1200
1300
if (reset_head (& head -> object .oid , "reset --hard" ,
1201
- NULL , 0 ) < 0 )
1301
+ NULL , 0 , NULL , NULL ) < 0 )
1202
1302
die (_ ("could not reset --hard" ));
1203
1303
printf (_ ("HEAD is now at %s" ),
1204
1304
find_unique_abbrev (& head -> object .oid ,
@@ -1252,7 +1352,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1252
1352
strbuf_addf (& buf , "rebase: checkout %s" ,
1253
1353
options .switch_to );
1254
1354
if (reset_head (& oid , "checkout" ,
1255
- options .head_name , 0 ) < 0 ) {
1355
+ options .head_name , 0 ,
1356
+ NULL , NULL ) < 0 ) {
1256
1357
ret = !!error (_ ("could not switch to "
1257
1358
"%s" ),
1258
1359
options .switch_to );
@@ -1317,10 +1418,29 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1317
1418
"it...\n" ));
1318
1419
1319
1420
strbuf_addf (& msg , "rebase: checkout %s" , options .onto_name );
1320
- if (reset_head (& options .onto -> object .oid , "checkout" , NULL , 1 ))
1421
+ if (reset_head (& options .onto -> object .oid , "checkout" , NULL , 1 ,
1422
+ NULL , msg .buf ))
1321
1423
die (_ ("Could not detach HEAD" ));
1322
1424
strbuf_release (& msg );
1323
1425
1426
+ /*
1427
+ * If the onto is a proper descendant of the tip of the branch, then
1428
+ * we just fast-forwarded.
1429
+ */
1430
+ strbuf_reset (& msg );
1431
+ if (!oidcmp (& merge_base , & options .orig_head )) {
1432
+ printf (_ ("Fast-forwarded %s to %s. \n" ),
1433
+ branch_name , options .onto_name );
1434
+ strbuf_addf (& msg , "rebase finished: %s onto %s" ,
1435
+ options .head_name ? options .head_name : "detached HEAD" ,
1436
+ oid_to_hex (& options .onto -> object .oid ));
1437
+ reset_head (NULL , "Fast-forwarded" , options .head_name , 0 ,
1438
+ "HEAD" , msg .buf );
1439
+ strbuf_release (& msg );
1440
+ ret = !!finish_rebase (& options );
1441
+ goto cleanup ;
1442
+ }
1443
+
1324
1444
strbuf_addf (& revisions , "%s..%s" ,
1325
1445
options .root ? oid_to_hex (& options .onto -> object .oid ) :
1326
1446
(options .restrict_revision ?
0 commit comments