22
22
#include "wt-status.h"
23
23
#include "revision.h"
24
24
#include "rerere.h"
25
+ #include "branch.h"
25
26
26
27
static char const * const builtin_rebase_usage [] = {
27
28
N_ ("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
@@ -54,7 +55,7 @@ static int use_builtin_rebase(void)
54
55
cp .git_cmd = 1 ;
55
56
if (capture_command (& cp , & out , 6 )) {
56
57
strbuf_release (& out );
57
- return 0 ;
58
+ return 1 ;
58
59
}
59
60
60
61
strbuf_trim (& out );
@@ -281,8 +282,10 @@ static int apply_autostash(struct rebase_options *opts)
281
282
if (!file_exists (path ))
282
283
return 0 ;
283
284
284
- if (read_one (state_dir_path ( "autostash" , opts ) , & autostash ))
285
+ if (read_one (path , & autostash ))
285
286
return error (_ ("Could not read '%s'" ), path );
287
+ /* Ensure that the hash is not mistaken for a number */
288
+ strbuf_addstr (& autostash , "^0" );
286
289
argv_array_pushl (& stash_apply .args ,
287
290
"stash" , "apply" , autostash .buf , NULL );
288
291
stash_apply .git_cmd = 1 ;
@@ -364,9 +367,124 @@ N_("Resolve all conflicts manually, mark them as resolved with\n"
364
367
"To abort and get back to the state before \"git rebase\", run "
365
368
"\"git rebase --abort\"." );
366
369
370
+ #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
371
+
372
+ #define RESET_HEAD_DETACH (1<<0)
373
+ #define RESET_HEAD_HARD (1<<1)
374
+
367
375
static int reset_head (struct object_id * oid , const char * action ,
368
- const char * switch_to_branch , int detach_head ,
369
- const char * reflog_orig_head , const char * reflog_head );
376
+ const char * switch_to_branch , unsigned flags ,
377
+ const char * reflog_orig_head , const char * reflog_head )
378
+ {
379
+ unsigned detach_head = flags & RESET_HEAD_DETACH ;
380
+ unsigned reset_hard = flags & RESET_HEAD_HARD ;
381
+ struct object_id head_oid ;
382
+ struct tree_desc desc [2 ] = { { NULL }, { NULL } };
383
+ struct lock_file lock = LOCK_INIT ;
384
+ struct unpack_trees_options unpack_tree_opts ;
385
+ struct tree * tree ;
386
+ const char * reflog_action ;
387
+ struct strbuf msg = STRBUF_INIT ;
388
+ size_t prefix_len ;
389
+ struct object_id * orig = NULL , oid_orig ,
390
+ * old_orig = NULL , oid_old_orig ;
391
+ int ret = 0 , nr = 0 ;
392
+
393
+ if (switch_to_branch && !starts_with (switch_to_branch , "refs/" ))
394
+ BUG ("Not a fully qualified branch: '%s'" , switch_to_branch );
395
+
396
+ if (hold_locked_index (& lock , LOCK_REPORT_ON_ERROR ) < 0 ) {
397
+ ret = -1 ;
398
+ goto leave_reset_head ;
399
+ }
400
+
401
+ if ((!oid || !reset_hard ) && get_oid ("HEAD" , & head_oid )) {
402
+ ret = error (_ ("could not determine HEAD revision" ));
403
+ goto leave_reset_head ;
404
+ }
405
+
406
+ if (!oid )
407
+ oid = & head_oid ;
408
+
409
+ memset (& unpack_tree_opts , 0 , sizeof (unpack_tree_opts ));
410
+ setup_unpack_trees_porcelain (& unpack_tree_opts , action );
411
+ unpack_tree_opts .head_idx = 1 ;
412
+ unpack_tree_opts .src_index = the_repository -> index ;
413
+ unpack_tree_opts .dst_index = the_repository -> index ;
414
+ unpack_tree_opts .fn = reset_hard ? oneway_merge : twoway_merge ;
415
+ unpack_tree_opts .update = 1 ;
416
+ unpack_tree_opts .merge = 1 ;
417
+ if (!detach_head )
418
+ unpack_tree_opts .reset = 1 ;
419
+
420
+ if (read_index_unmerged (the_repository -> index ) < 0 ) {
421
+ ret = error (_ ("could not read index" ));
422
+ goto leave_reset_head ;
423
+ }
424
+
425
+ if (!reset_hard && !fill_tree_descriptor (& desc [nr ++ ], & head_oid )) {
426
+ ret = error (_ ("failed to find tree of %s" ), oid_to_hex (oid ));
427
+ goto leave_reset_head ;
428
+ }
429
+
430
+ if (!fill_tree_descriptor (& desc [nr ++ ], oid )) {
431
+ ret = error (_ ("failed to find tree of %s" ), oid_to_hex (oid ));
432
+ goto leave_reset_head ;
433
+ }
434
+
435
+ if (unpack_trees (nr , desc , & unpack_tree_opts )) {
436
+ ret = -1 ;
437
+ goto leave_reset_head ;
438
+ }
439
+
440
+ tree = parse_tree_indirect (oid );
441
+ prime_cache_tree (the_repository -> index , tree );
442
+
443
+ if (write_locked_index (the_repository -> index , & lock , COMMIT_LOCK ) < 0 ) {
444
+ ret = error (_ ("could not write index" ));
445
+ goto leave_reset_head ;
446
+ }
447
+
448
+ reflog_action = getenv (GIT_REFLOG_ACTION_ENVIRONMENT );
449
+ strbuf_addf (& msg , "%s: " , reflog_action ? reflog_action : "rebase" );
450
+ prefix_len = msg .len ;
451
+
452
+ if (!get_oid ("ORIG_HEAD" , & oid_old_orig ))
453
+ old_orig = & oid_old_orig ;
454
+ if (!get_oid ("HEAD" , & oid_orig )) {
455
+ orig = & oid_orig ;
456
+ if (!reflog_orig_head ) {
457
+ strbuf_addstr (& msg , "updating ORIG_HEAD" );
458
+ reflog_orig_head = msg .buf ;
459
+ }
460
+ update_ref (reflog_orig_head , "ORIG_HEAD" , orig , old_orig , 0 ,
461
+ UPDATE_REFS_MSG_ON_ERR );
462
+ } else if (old_orig )
463
+ delete_ref (NULL , "ORIG_HEAD" , old_orig , 0 );
464
+ if (!reflog_head ) {
465
+ strbuf_setlen (& msg , prefix_len );
466
+ strbuf_addstr (& msg , "updating HEAD" );
467
+ reflog_head = msg .buf ;
468
+ }
469
+ if (!switch_to_branch )
470
+ ret = update_ref (reflog_head , "HEAD" , oid , orig ,
471
+ detach_head ? REF_NO_DEREF : 0 ,
472
+ UPDATE_REFS_MSG_ON_ERR );
473
+ else {
474
+ ret = create_symref ("HEAD" , switch_to_branch , msg .buf );
475
+ if (!ret )
476
+ ret = update_ref (reflog_head , "HEAD" , oid , NULL , 0 ,
477
+ UPDATE_REFS_MSG_ON_ERR );
478
+ }
479
+
480
+ leave_reset_head :
481
+ strbuf_release (& msg );
482
+ rollback_lock_file (& lock );
483
+ while (nr )
484
+ free ((void * )desc [-- nr ].buffer );
485
+ return ret ;
486
+ }
487
+
370
488
371
489
static int move_to_original_branch (struct rebase_options * opts )
372
490
{
@@ -697,112 +815,6 @@ static int run_specific_rebase(struct rebase_options *opts)
697
815
return status ? -1 : 0 ;
698
816
}
699
817
700
- #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
701
-
702
- static int reset_head (struct object_id * oid , const char * action ,
703
- const char * switch_to_branch , int detach_head ,
704
- const char * reflog_orig_head , const char * reflog_head )
705
- {
706
- struct object_id head_oid ;
707
- struct tree_desc desc ;
708
- struct lock_file lock = LOCK_INIT ;
709
- struct unpack_trees_options unpack_tree_opts ;
710
- struct tree * tree ;
711
- const char * reflog_action ;
712
- struct strbuf msg = STRBUF_INIT ;
713
- size_t prefix_len ;
714
- struct object_id * orig = NULL , oid_orig ,
715
- * old_orig = NULL , oid_old_orig ;
716
- int ret = 0 ;
717
-
718
- if (switch_to_branch && !starts_with (switch_to_branch , "refs/" ))
719
- BUG ("Not a fully qualified branch: '%s'" , switch_to_branch );
720
-
721
- if (hold_locked_index (& lock , LOCK_REPORT_ON_ERROR ) < 0 )
722
- return -1 ;
723
-
724
- if (!oid ) {
725
- if (get_oid ("HEAD" , & head_oid )) {
726
- rollback_lock_file (& lock );
727
- return error (_ ("could not determine HEAD revision" ));
728
- }
729
- oid = & head_oid ;
730
- }
731
-
732
- memset (& unpack_tree_opts , 0 , sizeof (unpack_tree_opts ));
733
- setup_unpack_trees_porcelain (& unpack_tree_opts , action );
734
- unpack_tree_opts .head_idx = 1 ;
735
- unpack_tree_opts .src_index = the_repository -> index ;
736
- unpack_tree_opts .dst_index = the_repository -> index ;
737
- unpack_tree_opts .fn = oneway_merge ;
738
- unpack_tree_opts .update = 1 ;
739
- unpack_tree_opts .merge = 1 ;
740
- if (!detach_head )
741
- unpack_tree_opts .reset = 1 ;
742
-
743
- if (read_index_unmerged (the_repository -> index ) < 0 ) {
744
- rollback_lock_file (& lock );
745
- return error (_ ("could not read index" ));
746
- }
747
-
748
- if (!fill_tree_descriptor (& desc , oid )) {
749
- error (_ ("failed to find tree of %s" ), oid_to_hex (oid ));
750
- rollback_lock_file (& lock );
751
- free ((void * )desc .buffer );
752
- return -1 ;
753
- }
754
-
755
- if (unpack_trees (1 , & desc , & unpack_tree_opts )) {
756
- rollback_lock_file (& lock );
757
- free ((void * )desc .buffer );
758
- return -1 ;
759
- }
760
-
761
- tree = parse_tree_indirect (oid );
762
- prime_cache_tree (the_repository -> index , tree );
763
-
764
- if (write_locked_index (the_repository -> index , & lock , COMMIT_LOCK ) < 0 )
765
- ret = error (_ ("could not write index" ));
766
- free ((void * )desc .buffer );
767
-
768
- if (ret )
769
- return ret ;
770
-
771
- reflog_action = getenv (GIT_REFLOG_ACTION_ENVIRONMENT );
772
- strbuf_addf (& msg , "%s: " , reflog_action ? reflog_action : "rebase" );
773
- prefix_len = msg .len ;
774
-
775
- if (!get_oid ("ORIG_HEAD" , & oid_old_orig ))
776
- old_orig = & oid_old_orig ;
777
- if (!get_oid ("HEAD" , & oid_orig )) {
778
- orig = & oid_orig ;
779
- if (!reflog_orig_head ) {
780
- strbuf_addstr (& msg , "updating ORIG_HEAD" );
781
- reflog_orig_head = msg .buf ;
782
- }
783
- update_ref (reflog_orig_head , "ORIG_HEAD" , orig , old_orig , 0 ,
784
- UPDATE_REFS_MSG_ON_ERR );
785
- } else if (old_orig )
786
- delete_ref (NULL , "ORIG_HEAD" , old_orig , 0 );
787
- if (!reflog_head ) {
788
- strbuf_setlen (& msg , prefix_len );
789
- strbuf_addstr (& msg , "updating HEAD" );
790
- reflog_head = msg .buf ;
791
- }
792
- if (!switch_to_branch )
793
- ret = update_ref (reflog_head , "HEAD" , oid , orig , REF_NO_DEREF ,
794
- UPDATE_REFS_MSG_ON_ERR );
795
- else {
796
- ret = create_symref ("HEAD" , switch_to_branch , msg .buf );
797
- if (!ret )
798
- ret = update_ref (reflog_head , "HEAD" , oid , NULL , 0 ,
799
- UPDATE_REFS_MSG_ON_ERR );
800
- }
801
-
802
- strbuf_release (& msg );
803
- return ret ;
804
- }
805
-
806
818
static int rebase_config (const char * var , const char * value , void * data )
807
819
{
808
820
struct rebase_options * opts = data ;
@@ -1177,8 +1189,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1177
1189
rerere_clear (& merge_rr );
1178
1190
string_list_clear (& merge_rr , 1 );
1179
1191
1180
- if (reset_head (NULL , "reset" , NULL , 0 , NULL , NULL ) < 0 )
1192
+ if (reset_head (NULL , "reset" , NULL , RESET_HEAD_HARD ,
1193
+ NULL , NULL ) < 0 )
1181
1194
die (_ ("could not discard worktree changes" ));
1195
+ remove_branch_state ();
1182
1196
if (read_basic_state (& options ))
1183
1197
exit (1 );
1184
1198
goto run_rebase ;
@@ -1193,9 +1207,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1193
1207
if (read_basic_state (& options ))
1194
1208
exit (1 );
1195
1209
if (reset_head (& options .orig_head , "reset" ,
1196
- options .head_name , 0 , NULL , NULL ) < 0 )
1210
+ options .head_name , RESET_HEAD_HARD ,
1211
+ NULL , NULL ) < 0 )
1197
1212
die (_ ("could not move back to %s" ),
1198
1213
oid_to_hex (& options .orig_head ));
1214
+ remove_branch_state ();
1199
1215
ret = finish_rebase (& options );
1200
1216
goto cleanup ;
1201
1217
}
@@ -1395,15 +1411,15 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1395
1411
* git-rebase.txt caveats with "unless you know what you are doing"
1396
1412
*/
1397
1413
if (options .rebase_merges )
1398
- die (_ ("error: cannot combine '--preserve_merges ' with "
1414
+ die (_ ("error: cannot combine '--preserve-merges ' with "
1399
1415
"'--rebase-merges'" ));
1400
1416
1401
1417
if (options .rebase_merges ) {
1402
1418
if (strategy_options .nr )
1403
- die (_ ("error: cannot combine '--rebase_merges ' with "
1419
+ die (_ ("error: cannot combine '--rebase-merges ' with "
1404
1420
"'--strategy-option'" ));
1405
1421
if (options .strategy )
1406
- die (_ ("error: cannot combine '--rebase_merges ' with "
1422
+ die (_ ("error: cannot combine '--rebase-merges ' with "
1407
1423
"'--strategy'" ));
1408
1424
}
1409
1425
@@ -1528,7 +1544,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1528
1544
update_index_if_able (& the_index , & lock_file );
1529
1545
rollback_lock_file (& lock_file );
1530
1546
1531
- if (has_unstaged_changes (0 ) || has_uncommitted_changes (0 )) {
1547
+ if (has_unstaged_changes (1 ) || has_uncommitted_changes (1 )) {
1532
1548
const char * autostash =
1533
1549
state_dir_path ("autostash" , & options );
1534
1550
struct child_process stash = CHILD_PROCESS_INIT ;
@@ -1554,10 +1570,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1554
1570
if (safe_create_leading_directories_const (autostash ))
1555
1571
die (_ ("Could not create directory for '%s'" ),
1556
1572
options .state_dir );
1557
- write_file (autostash , "%s" , buf . buf );
1573
+ write_file (autostash , "%s" , oid_to_hex ( & oid ) );
1558
1574
printf (_ ("Created autostash: %s\n" ), buf .buf );
1559
1575
if (reset_head (& head -> object .oid , "reset --hard" ,
1560
- NULL , 0 , NULL , NULL ) < 0 )
1576
+ NULL , RESET_HEAD_HARD , NULL , NULL ) < 0 )
1561
1577
die (_ ("could not reset --hard" ));
1562
1578
printf (_ ("HEAD is now at %s" ),
1563
1579
find_unique_abbrev (& head -> object .oid ,
@@ -1677,8 +1693,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1677
1693
"it...\n" ));
1678
1694
1679
1695
strbuf_addf (& msg , "rebase: checkout %s" , options .onto_name );
1680
- if (reset_head (& options .onto -> object .oid , "checkout" , NULL , 1 ,
1681
- NULL , msg .buf ))
1696
+ if (reset_head (& options .onto -> object .oid , "checkout" , NULL ,
1697
+ RESET_HEAD_DETACH , NULL , msg .buf ))
1682
1698
die (_ ("Could not detach HEAD" ));
1683
1699
strbuf_release (& msg );
1684
1700
0 commit comments