1717#include "merge-ll.h"
1818#include "lockfile.h"
1919#include "mem-pool.h"
20- #include "merge-ort-wrappers.h"
2120#include "object-file.h"
2221#include "object-name.h"
2322#include "odb.h"
3029#include "repo-settings.h"
3130#include "resolve-undo.h"
3231#include "revision.h"
32+ #include "sequencer.h"
3333#include "setup.h"
3434#include "submodule.h"
3535#include "symlinks.h"
@@ -853,90 +853,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
853853 ret = unpack_trees (2 , trees , & topts );
854854 clear_unpack_trees_porcelain (& topts );
855855 if (ret == -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- rollback_lock_file (& lock_file );
869- return 1 ;
870- }
871-
872- /*
873- * Without old_branch_info->commit, the below is the same as
874- * the two-tree unpack we already tried and failed.
875- */
876- if (!old_branch_info -> commit ) {
877- rollback_lock_file (& lock_file );
878- return 1 ;
879- }
880- old_tree = repo_get_commit_tree (the_repository ,
881- old_branch_info -> commit );
882-
883- if (repo_index_has_changes (the_repository , old_tree , & sb ))
884- die (_ ("cannot continue with staged changes in "
885- "the following files:\n%s" ), sb .buf );
886- strbuf_release (& sb );
887-
888- /* Do more real merge */
889-
890- /*
891- * We update the index fully, then write the
892- * tree from the index, then merge the new
893- * branch with the current tree, with the old
894- * branch as the base. Then we reset the index
895- * (but not the working tree) to the new
896- * branch, leaving the working tree as the
897- * merged version, but skipping unmerged
898- * entries in the index.
899- */
900-
901- add_files_to_cache (the_repository , NULL , NULL , NULL , 0 ,
902- 0 , 0 );
903- init_ui_merge_options (& o , the_repository );
904- o .verbosity = 0 ;
905- work = write_in_core_index_as_tree (the_repository ,
906- the_repository -> index );
907-
908- ret = reset_tree (new_tree ,
909- opts , 1 ,
910- writeout_error , new_branch_info );
911- if (ret ) {
912- rollback_lock_file (& lock_file );
913- return ret ;
914- }
915- o .ancestor = old_branch_info -> name ;
916- if (!old_branch_info -> name ) {
917- strbuf_add_unique_abbrev (& old_commit_shortname ,
918- & old_branch_info -> commit -> object .oid ,
919- DEFAULT_ABBREV );
920- o .ancestor = old_commit_shortname .buf ;
921- }
922- o .branch1 = new_branch_info -> name ;
923- o .branch2 = "local" ;
924- o .conflict_style = opts -> conflict_style ;
925- ret = merge_ort_nonrecursive (& o ,
926- new_tree ,
927- work ,
928- old_tree );
929- if (ret < 0 )
930- die (NULL );
931- ret = reset_tree (new_tree ,
932- opts , 0 ,
933- writeout_error , new_branch_info );
934- strbuf_release (& o .obuf );
935- strbuf_release (& old_commit_shortname );
936- if (ret ) {
937- rollback_lock_file (& lock_file );
938- return ret ;
939- }
856+ rollback_lock_file (& lock_file );
857+ return ret ;
940858 }
941859 }
942860
@@ -1181,6 +1099,10 @@ static int switch_branches(const struct checkout_opts *opts,
11811099 struct object_id rev ;
11821100 int flag , writeout_error = 0 ;
11831101 int do_merge = 1 ;
1102+ int created_autostash = 0 ;
1103+ struct strbuf old_commit_shortname = STRBUF_INIT ;
1104+ struct strbuf autostash_msg = STRBUF_INIT ;
1105+ const char * stash_label_base = NULL ;
11841106
11851107 trace2_cmd_mode ("branch" );
11861108
@@ -1218,11 +1140,39 @@ static int switch_branches(const struct checkout_opts *opts,
12181140 do_merge = 0 ;
12191141 }
12201142
1143+ if (old_branch_info .name )
1144+ stash_label_base = old_branch_info .name ;
1145+ else if (old_branch_info .commit ) {
1146+ strbuf_add_unique_abbrev (& old_commit_shortname ,
1147+ & old_branch_info .commit -> object .oid ,
1148+ DEFAULT_ABBREV );
1149+ stash_label_base = old_commit_shortname .buf ;
1150+ }
1151+
12211152 if (do_merge ) {
12221153 ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1154+ if (ret == -1 && opts -> merge ) {
1155+ strbuf_addf (& autostash_msg ,
1156+ "autostash while switching to '%s'" ,
1157+ new_branch_info -> name );
1158+ create_autostash_ref (the_repository ,
1159+ "CHECKOUT_AUTOSTASH_HEAD" ,
1160+ autostash_msg .buf , true);
1161+ created_autostash = 1 ;
1162+ ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1163+ }
12231164 if (ret ) {
1165+ if (created_autostash )
1166+ apply_autostash_ref (the_repository ,
1167+ "CHECKOUT_AUTOSTASH_HEAD" ,
1168+ new_branch_info -> name ,
1169+ "local" ,
1170+ stash_label_base ,
1171+ autostash_msg .buf );
12241172 branch_info_release (& old_branch_info );
1225- return ret ;
1173+ strbuf_release (& old_commit_shortname );
1174+ strbuf_release (& autostash_msg );
1175+ return ret < 0 ? 1 : ret ;
12261176 }
12271177 }
12281178
@@ -1231,8 +1181,30 @@ static int switch_branches(const struct checkout_opts *opts,
12311181
12321182 update_refs_for_switch (opts , & old_branch_info , new_branch_info );
12331183
1184+ if (opts -> conflict_style >= 0 ) {
1185+ struct strbuf cfg = STRBUF_INIT ;
1186+ strbuf_addf (& cfg , "merge.conflictStyle=%s" ,
1187+ conflict_style_name (opts -> conflict_style ));
1188+ git_config_push_parameter (cfg .buf );
1189+ strbuf_release (& cfg );
1190+ }
1191+ apply_autostash_ref (the_repository , "CHECKOUT_AUTOSTASH_HEAD" ,
1192+ new_branch_info -> name , "local" ,
1193+ stash_label_base ,
1194+ autostash_msg .buf );
1195+
1196+ discard_index (the_repository -> index );
1197+ if (repo_read_index (the_repository ) < 0 )
1198+ die (_ ("index file corrupt" ));
1199+
1200+ if (created_autostash && !opts -> quiet && new_branch_info -> commit )
1201+ show_local_changes (& new_branch_info -> commit -> object ,
1202+ & opts -> diff_options );
1203+
12341204 ret = post_checkout_hook (old_branch_info .commit , new_branch_info -> commit , 1 );
12351205 branch_info_release (& old_branch_info );
1206+ strbuf_release (& old_commit_shortname );
1207+ strbuf_release (& autostash_msg );
12361208
12371209 return ret || writeout_error ;
12381210}
0 commit comments