1717#include "merge-ll.h"
1818#include "lockfile.h"
1919#include "mem-pool.h"
20+ #include "merge-ort-wrappers.h"
2021#include "object-file.h"
2122#include "object-name.h"
2223#include "odb.h"
2930#include "repo-settings.h"
3031#include "resolve-undo.h"
3132#include "revision.h"
32- #include "sequencer.h"
3333#include "setup.h"
3434#include "strvec.h"
3535#include "submodule.h"
@@ -790,10 +790,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
790790 struct tree * new_tree ;
791791
792792 repo_hold_locked_index (the_repository , & lock_file , LOCK_DIE_ON_ERROR );
793- if (repo_read_index_preload (the_repository , NULL , 0 ) < 0 ) {
794- rollback_lock_file (& lock_file );
793+ if (repo_read_index_preload (the_repository , NULL , 0 ) < 0 )
795794 return error (_ ("index file corrupt" ));
796- }
797795
798796 resolve_undo_clear_index (the_repository -> index );
799797 if (opts -> new_orphan_branch && opts -> orphan_from_empty_tree ) {
@@ -806,18 +804,14 @@ static int merge_working_tree(const struct checkout_opts *opts,
806804 } else {
807805 new_tree = repo_get_commit_tree (the_repository ,
808806 new_branch_info -> commit );
809- if (!new_tree ) {
810- rollback_lock_file (& lock_file );
807+ if (!new_tree )
811808 return error (_ ("unable to read tree (%s)" ),
812809 oid_to_hex (& new_branch_info -> commit -> object .oid ));
813- }
814810 }
815811 if (opts -> discard_changes ) {
816812 ret = reset_tree (new_tree , opts , 1 , writeout_error , new_branch_info );
817- if (ret ) {
818- rollback_lock_file (& lock_file );
813+ if (ret )
819814 return ret ;
820- }
821815 } else {
822816 struct tree_desc trees [2 ];
823817 struct tree * tree ;
@@ -827,7 +821,6 @@ static int merge_working_tree(const struct checkout_opts *opts,
827821 refresh_index (the_repository -> index , REFRESH_QUIET , NULL , NULL , NULL );
828822
829823 if (unmerged_index (the_repository -> index )) {
830- rollback_lock_file (& lock_file );
831824 error (_ ("you need to resolve your current index first" ));
832825 return 1 ;
833826 }
@@ -860,8 +853,82 @@ static int merge_working_tree(const struct checkout_opts *opts,
860853 ret = unpack_trees (2 , trees , & topts );
861854 clear_unpack_trees_porcelain (& topts );
862855 if (ret == -1 ) {
863- rollback_lock_file (& lock_file );
864- return 1 ;
856+ /*
857+ * Unpack couldn't do a trivial merge; either
858+ * give up or do a real merge, depending on
859+ * whether the merge flag was used.
860+ */
861+ struct tree * work ;
862+ struct tree * old_tree ;
863+ struct merge_options o ;
864+ struct strbuf sb = STRBUF_INIT ;
865+ struct strbuf old_commit_shortname = STRBUF_INIT ;
866+
867+ if (!opts -> merge )
868+ return 1 ;
869+
870+ /*
871+ * Without old_branch_info->commit, the below is the same as
872+ * the two-tree unpack we already tried and failed.
873+ */
874+ if (!old_branch_info -> commit )
875+ return 1 ;
876+ old_tree = repo_get_commit_tree (the_repository ,
877+ old_branch_info -> commit );
878+
879+ if (repo_index_has_changes (the_repository , old_tree , & sb ))
880+ die (_ ("cannot continue with staged changes in "
881+ "the following files:\n%s" ), sb .buf );
882+ strbuf_release (& sb );
883+
884+ /* Do more real merge */
885+
886+ /*
887+ * We update the index fully, then write the
888+ * tree from the index, then merge the new
889+ * branch with the current tree, with the old
890+ * branch as the base. Then we reset the index
891+ * (but not the working tree) to the new
892+ * branch, leaving the working tree as the
893+ * merged version, but skipping unmerged
894+ * entries in the index.
895+ */
896+
897+ add_files_to_cache (the_repository , NULL , NULL , NULL , 0 ,
898+ 0 , 0 );
899+ init_ui_merge_options (& o , the_repository );
900+ o .verbosity = 0 ;
901+ work = write_in_core_index_as_tree (the_repository ,
902+ the_repository -> index );
903+
904+ ret = reset_tree (new_tree ,
905+ opts , 1 ,
906+ writeout_error , new_branch_info );
907+ if (ret )
908+ return ret ;
909+ o .ancestor = old_branch_info -> name ;
910+ if (!old_branch_info -> name ) {
911+ strbuf_add_unique_abbrev (& old_commit_shortname ,
912+ & old_branch_info -> commit -> object .oid ,
913+ DEFAULT_ABBREV );
914+ o .ancestor = old_commit_shortname .buf ;
915+ }
916+ o .branch1 = new_branch_info -> name ;
917+ o .branch2 = "local" ;
918+ o .conflict_style = opts -> conflict_style ;
919+ ret = merge_ort_nonrecursive (& o ,
920+ new_tree ,
921+ work ,
922+ old_tree );
923+ if (ret < 0 )
924+ die (NULL );
925+ ret = reset_tree (new_tree ,
926+ opts , 0 ,
927+ writeout_error , new_branch_info );
928+ strbuf_release (& o .obuf );
929+ strbuf_release (& old_commit_shortname );
930+ if (ret )
931+ return ret ;
865932 }
866933 }
867934
@@ -1106,9 +1173,6 @@ static int switch_branches(const struct checkout_opts *opts,
11061173 struct object_id rev ;
11071174 int flag , writeout_error = 0 ;
11081175 int do_merge = 1 ;
1109- int created_autostash = 0 ;
1110- struct strbuf old_commit_shortname = STRBUF_INIT ;
1111- const char * stash_label_base = NULL ;
11121176
11131177 trace2_cmd_mode ("branch" );
11141178
@@ -1146,31 +1210,10 @@ static int switch_branches(const struct checkout_opts *opts,
11461210 do_merge = 0 ;
11471211 }
11481212
1149- if (old_branch_info .name )
1150- stash_label_base = old_branch_info .name ;
1151- else if (old_branch_info .commit ) {
1152- strbuf_add_unique_abbrev (& old_commit_shortname ,
1153- & old_branch_info .commit -> object .oid ,
1154- DEFAULT_ABBREV );
1155- stash_label_base = old_commit_shortname .buf ;
1156- }
1157-
11581213 if (do_merge ) {
11591214 ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1160- if (ret && opts -> merge ) {
1161- create_autostash_ref_silent (the_repository ,
1162- "CHECKOUT_AUTOSTASH_HEAD" );
1163- created_autostash = 1 ;
1164- ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1165- }
11661215 if (ret ) {
1167- apply_autostash_ref_with_labels (the_repository ,
1168- "CHECKOUT_AUTOSTASH_HEAD" ,
1169- new_branch_info -> name ,
1170- "local" ,
1171- stash_label_base );
11721216 branch_info_release (& old_branch_info );
1173- strbuf_release (& old_commit_shortname );
11741217 return ret ;
11751218 }
11761219 }
@@ -1180,29 +1223,8 @@ static int switch_branches(const struct checkout_opts *opts,
11801223
11811224 update_refs_for_switch (opts , & old_branch_info , new_branch_info );
11821225
1183- if (opts -> conflict_style >= 0 ) {
1184- struct strbuf cfg = STRBUF_INIT ;
1185- strbuf_addf (& cfg , "merge.conflictStyle=%s" ,
1186- conflict_style_name (opts -> conflict_style ));
1187- git_config_push_parameter (cfg .buf );
1188- strbuf_release (& cfg );
1189- }
1190- apply_autostash_ref_with_labels (the_repository , "CHECKOUT_AUTOSTASH_HEAD" ,
1191- new_branch_info -> name , "local" ,
1192- stash_label_base );
1193-
1194- discard_index (the_repository -> index );
1195- if (repo_read_index (the_repository ) < 0 )
1196- die (_ ("index file corrupt" ));
1197-
1198- if (created_autostash && !opts -> discard_changes && !opts -> quiet &&
1199- new_branch_info -> commit )
1200- show_local_changes (& new_branch_info -> commit -> object ,
1201- & opts -> diff_options );
1202-
12031226 ret = post_checkout_hook (old_branch_info .commit , new_branch_info -> commit , 1 );
12041227 branch_info_release (& old_branch_info );
1205- strbuf_release (& old_commit_shortname );
12061228
12071229 return ret || writeout_error ;
12081230}
0 commit comments