@@ -98,6 +98,7 @@ struct rebase_options {
98
98
int allow_empty_message ;
99
99
int rebase_merges , rebase_cousins ;
100
100
char * strategy , * strategy_opts ;
101
+ struct strbuf git_format_patch_opt ;
101
102
};
102
103
103
104
static int is_interactive (struct rebase_options * opts )
@@ -379,6 +380,15 @@ static int run_specific_rebase(struct rebase_options *opts)
379
380
add_var (& script_snippet , "rebase_root" , opts -> root ? "t" : "" );
380
381
add_var (& script_snippet , "squash_onto" ,
381
382
opts -> squash_onto ? oid_to_hex (opts -> squash_onto ) : "" );
383
+ add_var (& script_snippet , "git_format_patch_opt" ,
384
+ opts -> git_format_patch_opt .buf );
385
+
386
+ if (is_interactive (opts ) &&
387
+ !(opts -> flags & REBASE_INTERACTIVE_EXPLICIT )) {
388
+ strbuf_addstr (& script_snippet ,
389
+ "GIT_EDITOR=:; export GIT_EDITOR; " );
390
+ opts -> autosquash = 0 ;
391
+ }
382
392
383
393
switch (opts -> type ) {
384
394
case REBASE_AM :
@@ -431,7 +441,8 @@ static int run_specific_rebase(struct rebase_options *opts)
431
441
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
432
442
433
443
static int reset_head (struct object_id * oid , const char * action ,
434
- const char * switch_to_branch , int detach_head )
444
+ const char * switch_to_branch , int detach_head ,
445
+ const char * reflog_orig_head , const char * reflog_head )
435
446
{
436
447
struct object_id head_oid ;
437
448
struct tree_desc desc ;
@@ -506,20 +517,26 @@ static int reset_head(struct object_id *oid, const char *action,
506
517
old_orig = & oid_old_orig ;
507
518
if (!get_oid ("HEAD" , & oid_orig )) {
508
519
orig = & oid_orig ;
509
- strbuf_addstr (& msg , "updating ORIG_HEAD" );
510
- update_ref (msg .buf , "ORIG_HEAD" , orig , old_orig , 0 ,
520
+ if (!reflog_orig_head ) {
521
+ strbuf_addstr (& msg , "updating ORIG_HEAD" );
522
+ reflog_orig_head = msg .buf ;
523
+ }
524
+ update_ref (reflog_orig_head , "ORIG_HEAD" , orig , old_orig , 0 ,
511
525
UPDATE_REFS_MSG_ON_ERR );
512
526
} else if (old_orig )
513
527
delete_ref (NULL , "ORIG_HEAD" , old_orig , 0 );
514
- strbuf_setlen (& msg , prefix_len );
515
- strbuf_addstr (& msg , "updating HEAD" );
528
+ if (!reflog_head ) {
529
+ strbuf_setlen (& msg , prefix_len );
530
+ strbuf_addstr (& msg , "updating HEAD" );
531
+ reflog_head = msg .buf ;
532
+ }
516
533
if (!switch_to_branch )
517
- ret = update_ref (msg . buf , "HEAD" , oid , orig , REF_NO_DEREF ,
534
+ ret = update_ref (reflog_head , "HEAD" , oid , orig , REF_NO_DEREF ,
518
535
UPDATE_REFS_MSG_ON_ERR );
519
536
else {
520
537
ret = create_symref ("HEAD" , switch_to_branch , msg .buf );
521
538
if (!ret )
522
- ret = update_ref (msg . buf , "HEAD" , oid , NULL , 0 ,
539
+ ret = update_ref (reflog_head , "HEAD" , oid , NULL , 0 ,
523
540
UPDATE_REFS_MSG_ON_ERR );
524
541
}
525
542
@@ -622,6 +639,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
622
639
return 0 ;
623
640
}
624
641
642
+ static void NORETURN error_on_missing_default_upstream (void )
643
+ {
644
+ struct branch * current_branch = branch_get (NULL );
645
+
646
+ printf (_ ("%s\n"
647
+ "Please specify which branch you want to rebase against.\n"
648
+ "See git-rebase(1) for details.\n"
649
+ "\n"
650
+ " git rebase '<branch>'\n"
651
+ "\n" ),
652
+ current_branch ? _ ("There is no tracking information for "
653
+ "the current branch." ) :
654
+ _ ("You are not currently on a branch." ));
655
+
656
+ if (current_branch ) {
657
+ const char * remote = current_branch -> remote_name ;
658
+
659
+ if (!remote )
660
+ remote = _ ("<remote>" );
661
+
662
+ printf (_ ("If you wish to set tracking information for this "
663
+ "branch you can do so with:\n"
664
+ "\n"
665
+ " git branch --set-upstream-to=%s/<branch> %s\n"
666
+ "\n" ),
667
+ remote , current_branch -> name );
668
+ }
669
+ exit (1 );
670
+ }
671
+
625
672
int cmd_rebase (int argc , const char * * argv , const char * prefix )
626
673
{
627
674
struct rebase_options options = {
@@ -630,6 +677,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
630
677
.git_am_opt = STRBUF_INIT ,
631
678
.allow_rerere_autoupdate = -1 ,
632
679
.allow_empty_message = 1 ,
680
+ .git_format_patch_opt = STRBUF_INIT ,
633
681
};
634
682
const char * branch_name ;
635
683
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