@@ -472,6 +472,7 @@ static int rebase_setupfiles_merge(git_rebase *rebase)
472472static int rebase_setupfiles (git_rebase * rebase )
473473{
474474 char onto [GIT_OID_HEXSZ ], orig_head [GIT_OID_HEXSZ ];
475+ const char * orig_head_name ;
475476
476477 git_oid_fmt (onto , & rebase -> onto_id );
477478 git_oid_fmt (orig_head , & rebase -> orig_head_id );
@@ -481,8 +482,11 @@ static int rebase_setupfiles(git_rebase *rebase)
481482 return -1 ;
482483 }
483484
485+ orig_head_name = rebase -> head_detached ? ORIG_DETACHED_HEAD :
486+ rebase -> orig_head_name ;
487+
484488 if (git_repository__set_orig_head (rebase -> repo , & rebase -> orig_head_id ) < 0 ||
485- rebase_setupfile (rebase , HEAD_NAME_FILE , -1 , "%s\n" , rebase -> orig_head_name ) < 0 ||
489+ rebase_setupfile (rebase , HEAD_NAME_FILE , -1 , "%s\n" , orig_head_name ) < 0 ||
486490 rebase_setupfile (rebase , ONTO_FILE , -1 , "%.*s\n" , GIT_OID_HEXSZ , onto ) < 0 ||
487491 rebase_setupfile (rebase , ORIG_HEAD_FILE , -1 , "%.*s\n" , GIT_OID_HEXSZ , orig_head ) < 0 ||
488492 rebase_setupfile (rebase , QUIET_FILE , -1 , rebase -> quiet ? "t\n" : "\n" ) < 0 )
@@ -626,8 +630,12 @@ static int rebase_init_merge(
626630 rebase -> state_path = git_buf_detach (& state_path );
627631 GITERR_CHECK_ALLOC (rebase -> state_path );
628632
629- rebase -> orig_head_name = git__strdup (branch -> ref_name ? branch -> ref_name : ORIG_DETACHED_HEAD );
630- GITERR_CHECK_ALLOC (rebase -> orig_head_name );
633+ if (branch -> ref_name ) {
634+ rebase -> orig_head_name = git__strdup (branch -> ref_name );
635+ GITERR_CHECK_ALLOC (rebase -> orig_head_name );
636+ } else {
637+ rebase -> head_detached = 1 ;
638+ }
631639
632640 rebase -> onto_name = git__strdup (rebase_onto_name (onto ));
633641 GITERR_CHECK_ALLOC (rebase -> onto_name );
@@ -1254,42 +1262,33 @@ static int rebase_copy_notes(
12541262 return error ;
12551263}
12561264
1257- int git_rebase_finish (
1258- git_rebase * rebase ,
1259- const git_signature * signature )
1265+ static int return_to_orig_head (git_rebase * rebase )
12601266{
12611267 git_reference * terminal_ref = NULL , * branch_ref = NULL , * head_ref = NULL ;
12621268 git_commit * terminal_commit = NULL ;
12631269 git_buf branch_msg = GIT_BUF_INIT , head_msg = GIT_BUF_INIT ;
12641270 char onto [GIT_OID_HEXSZ ];
1265- int error ;
1266-
1267- assert (rebase );
1268-
1269- if (rebase -> inmemory )
1270- return 0 ;
1271+ int error = 0 ;
12711272
12721273 git_oid_fmt (onto , & rebase -> onto_id );
12731274
1274- if ((error = git_buf_printf (& branch_msg , "rebase finished: %s onto %.*s" ,
1275- rebase -> orig_head_name , GIT_OID_HEXSZ , onto )) < 0 ||
1276- (error = git_buf_printf (& head_msg , "rebase finished: returning to %s" ,
1277- rebase -> orig_head_name )) < 0 ||
1278- (error = git_repository_head (& terminal_ref , rebase -> repo )) < 0 ||
1275+ if ((error = git_buf_printf (& branch_msg ,
1276+ "rebase finished: %s onto %.*s" ,
1277+ rebase -> orig_head_name , GIT_OID_HEXSZ , onto )) == 0 &&
1278+ (error = git_buf_printf (& head_msg ,
1279+ "rebase finished: returning to %s" ,
1280+ rebase -> orig_head_name )) == 0 &&
1281+ (error = git_repository_head (& terminal_ref , rebase -> repo )) == 0 &&
12791282 (error = git_reference_peel ((git_object * * )& terminal_commit ,
1280- terminal_ref , GIT_OBJ_COMMIT )) < 0 ||
1283+ terminal_ref , GIT_OBJ_COMMIT )) == 0 &&
12811284 (error = git_reference_create_matching (& branch_ref ,
1282- rebase -> repo , rebase -> orig_head_name , git_commit_id (terminal_commit ), 1 ,
1283- & rebase -> orig_head_id , branch_msg .ptr )) < 0 ||
1284- (error = git_reference_symbolic_create (& head_ref ,
1285+ rebase -> repo , rebase -> orig_head_name ,
1286+ git_commit_id (terminal_commit ), 1 ,
1287+ & rebase -> orig_head_id , branch_msg .ptr )) == 0 )
1288+ error = git_reference_symbolic_create (& head_ref ,
12851289 rebase -> repo , GIT_HEAD_FILE , rebase -> orig_head_name , 1 ,
1286- head_msg .ptr )) < 0 ||
1287- (error = rebase_copy_notes (rebase , signature )) < 0 )
1288- goto done ;
1289-
1290- error = rebase_cleanup (rebase );
1290+ head_msg .ptr );
12911291
1292- done :
12931292 git_buf_free (& head_msg );
12941293 git_buf_free (& branch_msg );
12951294 git_commit_free (terminal_commit );
@@ -1300,6 +1299,26 @@ int git_rebase_finish(
13001299 return error ;
13011300}
13021301
1302+ int git_rebase_finish (
1303+ git_rebase * rebase ,
1304+ const git_signature * signature )
1305+ {
1306+ int error = 0 ;
1307+
1308+ assert (rebase );
1309+
1310+ if (rebase -> inmemory )
1311+ return 0 ;
1312+
1313+ if (!rebase -> head_detached )
1314+ error = return_to_orig_head (rebase );
1315+
1316+ if (error == 0 && (error = rebase_copy_notes (rebase , signature )) == 0 )
1317+ error = rebase_cleanup (rebase );
1318+
1319+ return error ;
1320+ }
1321+
13031322size_t git_rebase_operation_entrycount (git_rebase * rebase )
13041323{
13051324 assert (rebase );
0 commit comments