@@ -367,9 +367,124 @@ N_("Resolve all conflicts manually, mark them as resolved with\n"
367367"To abort and get back to the state before \"git rebase\", run " 
368368"\"git rebase --abort\"." );
369369
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+ 
370375static  int  reset_head (struct  object_id  * oid , const  char  * action ,
371- 		      const  char  * switch_to_branch , int  detach_head ,
372- 		      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+ 
373488
374489static  int  move_to_original_branch (struct  rebase_options  * opts )
375490{
@@ -700,124 +815,6 @@ static int run_specific_rebase(struct rebase_options *opts)
700815	return  status  ? -1  : 0 ;
701816}
702817
703- #define  GIT_REFLOG_ACTION_ENVIRONMENT  "GIT_REFLOG_ACTION"
704- 
705- #define  RESET_HEAD_DETACH  (1<<0)
706- #define  RESET_HEAD_HARD  (1<<1)
707- 
708- static  int  reset_head (struct  object_id  * oid , const  char  * action ,
709- 		      const  char  * switch_to_branch , unsigned  flags ,
710- 		      const  char  * reflog_orig_head , const  char  * reflog_head )
711- {
712- 	unsigned  detach_head  =  flags  &  RESET_HEAD_DETACH ;
713- 	unsigned  reset_hard  =  flags  &  RESET_HEAD_HARD ;
714- 	struct  object_id  head_oid ;
715- 	struct  tree_desc  desc [2 ] =  { { NULL  }, { NULL  } };
716- 	struct  lock_file  lock  =  LOCK_INIT ;
717- 	struct  unpack_trees_options  unpack_tree_opts ;
718- 	struct  tree  * tree ;
719- 	const  char  * reflog_action ;
720- 	struct  strbuf  msg  =  STRBUF_INIT ;
721- 	size_t  prefix_len ;
722- 	struct  object_id  * orig  =  NULL , oid_orig ,
723- 		* old_orig  =  NULL , oid_old_orig ;
724- 	int  ret  =  0 , nr  =  0 ;
725- 
726- 	if  (switch_to_branch  &&  !starts_with (switch_to_branch , "refs/" ))
727- 		BUG ("Not a fully qualified branch: '%s'" , switch_to_branch );
728- 
729- 	if  (hold_locked_index (& lock , LOCK_REPORT_ON_ERROR ) <  0 ) {
730- 		ret  =  -1 ;
731- 		goto leave_reset_head ;
732- 	}
733- 
734- 	if  ((!oid  ||  !reset_hard ) &&  get_oid ("HEAD" , & head_oid )) {
735- 		ret  =  error (_ ("could not determine HEAD revision" ));
736- 		goto leave_reset_head ;
737- 	}
738- 
739- 	if  (!oid )
740- 		oid  =  & head_oid ;
741- 
742- 	memset (& unpack_tree_opts , 0 , sizeof (unpack_tree_opts ));
743- 	setup_unpack_trees_porcelain (& unpack_tree_opts , action );
744- 	unpack_tree_opts .head_idx  =  1 ;
745- 	unpack_tree_opts .src_index  =  the_repository -> index ;
746- 	unpack_tree_opts .dst_index  =  the_repository -> index ;
747- 	unpack_tree_opts .fn  =  reset_hard  ? oneway_merge  : twoway_merge ;
748- 	unpack_tree_opts .update  =  1 ;
749- 	unpack_tree_opts .merge  =  1 ;
750- 	if  (!detach_head )
751- 		unpack_tree_opts .reset  =  1 ;
752- 
753- 	if  (read_index_unmerged (the_repository -> index ) <  0 ) {
754- 		ret  =  error (_ ("could not read index" ));
755- 		goto leave_reset_head ;
756- 	}
757- 
758- 	if  (!reset_hard  &&  !fill_tree_descriptor (& desc [nr ++ ], & head_oid )) {
759- 		ret  =  error (_ ("failed to find tree of %s" ), oid_to_hex (oid ));
760- 		goto leave_reset_head ;
761- 	}
762- 
763- 	if  (!fill_tree_descriptor (& desc [nr ++ ], oid )) {
764- 		ret  =  error (_ ("failed to find tree of %s" ), oid_to_hex (oid ));
765- 		goto leave_reset_head ;
766- 	}
767- 
768- 	if  (unpack_trees (nr , desc , & unpack_tree_opts )) {
769- 		ret  =  -1 ;
770- 		goto leave_reset_head ;
771- 	}
772- 
773- 	tree  =  parse_tree_indirect (oid );
774- 	prime_cache_tree (the_repository -> index , tree );
775- 
776- 	if  (write_locked_index (the_repository -> index , & lock , COMMIT_LOCK ) <  0 ) {
777- 		ret  =  error (_ ("could not write index" ));
778- 		goto leave_reset_head ;
779- 	}
780- 
781- 	reflog_action  =  getenv (GIT_REFLOG_ACTION_ENVIRONMENT );
782- 	strbuf_addf (& msg , "%s: " , reflog_action  ? reflog_action  : "rebase" );
783- 	prefix_len  =  msg .len ;
784- 
785- 	if  (!get_oid ("ORIG_HEAD" , & oid_old_orig ))
786- 		old_orig  =  & oid_old_orig ;
787- 	if  (!get_oid ("HEAD" , & oid_orig )) {
788- 		orig  =  & oid_orig ;
789- 		if  (!reflog_orig_head ) {
790- 			strbuf_addstr (& msg , "updating ORIG_HEAD" );
791- 			reflog_orig_head  =  msg .buf ;
792- 		}
793- 		update_ref (reflog_orig_head , "ORIG_HEAD" , orig , old_orig , 0 ,
794- 			   UPDATE_REFS_MSG_ON_ERR );
795- 	} else  if  (old_orig )
796- 		delete_ref (NULL , "ORIG_HEAD" , old_orig , 0 );
797- 	if  (!reflog_head ) {
798- 		strbuf_setlen (& msg , prefix_len );
799- 		strbuf_addstr (& msg , "updating HEAD" );
800- 		reflog_head  =  msg .buf ;
801- 	}
802- 	if  (!switch_to_branch )
803- 		ret  =  update_ref (reflog_head , "HEAD" , oid , orig ,
804- 				 detach_head  ? REF_NO_DEREF  : 0 ,
805- 				 UPDATE_REFS_MSG_ON_ERR );
806- 	else  {
807- 		ret  =  create_symref ("HEAD" , switch_to_branch , msg .buf );
808- 		if  (!ret )
809- 			ret  =  update_ref (reflog_head , "HEAD" , oid , NULL , 0 ,
810- 					 UPDATE_REFS_MSG_ON_ERR );
811- 	}
812- 
813- leave_reset_head :
814- 	strbuf_release (& msg );
815- 	rollback_lock_file (& lock );
816- 	while  (nr )
817- 		free ((void  * )desc [-- nr ].buffer );
818- 	return  ret ;
819- }
820- 
821818static  int  rebase_config (const  char  * var , const  char  * value , void  * data )
822819{
823820	struct  rebase_options  * opts  =  data ;
0 commit comments