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"
@@ -783,8 +783,10 @@ static int merge_working_tree(const struct checkout_opts *opts,
783783 struct tree * new_tree ;
784784
785785 repo_hold_locked_index (the_repository , & lock_file , LOCK_DIE_ON_ERROR );
786- if (repo_read_index_preload (the_repository , NULL , 0 ) < 0 )
786+ if (repo_read_index_preload (the_repository , NULL , 0 ) < 0 ) {
787+ rollback_lock_file (& lock_file );
787788 return error (_ ("index file corrupt" ));
789+ }
788790
789791 resolve_undo_clear_index (the_repository -> index );
790792 if (opts -> new_orphan_branch && opts -> orphan_from_empty_tree ) {
@@ -797,14 +799,18 @@ static int merge_working_tree(const struct checkout_opts *opts,
797799 } else {
798800 new_tree = repo_get_commit_tree (the_repository ,
799801 new_branch_info -> commit );
800- if (!new_tree )
802+ if (!new_tree ) {
803+ rollback_lock_file (& lock_file );
801804 return error (_ ("unable to read tree (%s)" ),
802805 oid_to_hex (& new_branch_info -> commit -> object .oid ));
806+ }
803807 }
804808 if (opts -> discard_changes ) {
805809 ret = reset_tree (new_tree , opts , 1 , writeout_error , new_branch_info );
806- if (ret )
810+ if (ret ) {
811+ rollback_lock_file (& lock_file );
807812 return ret ;
813+ }
808814 } else {
809815 struct tree_desc trees [2 ];
810816 struct tree * tree ;
@@ -814,6 +820,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
814820 refresh_index (the_repository -> index , REFRESH_QUIET , NULL , NULL , NULL );
815821
816822 if (unmerged_index (the_repository -> index )) {
823+ rollback_lock_file (& lock_file );
817824 error (_ ("you need to resolve your current index first" ));
818825 return 1 ;
819826 }
@@ -846,82 +853,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
846853 ret = unpack_trees (2 , trees , & topts );
847854 clear_unpack_trees_porcelain (& topts );
848855 if (ret == -1 ) {
849- /*
850- * Unpack couldn't do a trivial merge; either
851- * give up or do a real merge, depending on
852- * whether the merge flag was used.
853- */
854- struct tree * work ;
855- struct tree * old_tree ;
856- struct merge_options o ;
857- struct strbuf sb = STRBUF_INIT ;
858- struct strbuf old_commit_shortname = STRBUF_INIT ;
859-
860- if (!opts -> merge )
861- return 1 ;
862-
863- /*
864- * Without old_branch_info->commit, the below is the same as
865- * the two-tree unpack we already tried and failed.
866- */
867- if (!old_branch_info -> commit )
868- return 1 ;
869- old_tree = repo_get_commit_tree (the_repository ,
870- old_branch_info -> commit );
871-
872- if (repo_index_has_changes (the_repository , old_tree , & sb ))
873- die (_ ("cannot continue with staged changes in "
874- "the following files:\n%s" ), sb .buf );
875- strbuf_release (& sb );
876-
877- /* Do more real merge */
878-
879- /*
880- * We update the index fully, then write the
881- * tree from the index, then merge the new
882- * branch with the current tree, with the old
883- * branch as the base. Then we reset the index
884- * (but not the working tree) to the new
885- * branch, leaving the working tree as the
886- * merged version, but skipping unmerged
887- * entries in the index.
888- */
889-
890- add_files_to_cache (the_repository , NULL , NULL , NULL , 0 ,
891- 0 , 0 );
892- init_ui_merge_options (& o , the_repository );
893- o .verbosity = 0 ;
894- work = write_in_core_index_as_tree (the_repository ,
895- the_repository -> index );
896-
897- ret = reset_tree (new_tree ,
898- opts , 1 ,
899- writeout_error , new_branch_info );
900- if (ret )
901- return ret ;
902- o .ancestor = old_branch_info -> name ;
903- if (!old_branch_info -> name ) {
904- strbuf_add_unique_abbrev (& old_commit_shortname ,
905- & old_branch_info -> commit -> object .oid ,
906- DEFAULT_ABBREV );
907- o .ancestor = old_commit_shortname .buf ;
908- }
909- o .branch1 = new_branch_info -> name ;
910- o .branch2 = "local" ;
911- o .conflict_style = opts -> conflict_style ;
912- ret = merge_ort_nonrecursive (& o ,
913- new_tree ,
914- work ,
915- old_tree );
916- if (ret < 0 )
917- die (NULL );
918- ret = reset_tree (new_tree ,
919- opts , 0 ,
920- writeout_error , new_branch_info );
921- strbuf_release (& o .obuf );
922- strbuf_release (& old_commit_shortname );
923- if (ret )
924- return ret ;
856+ rollback_lock_file (& lock_file );
857+ return 1 ;
925858 }
926859 }
927860
@@ -1166,6 +1099,10 @@ static int switch_branches(const struct checkout_opts *opts,
11661099 struct object_id rev ;
11671100 int flag , writeout_error = 0 ;
11681101 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 ;
11691106
11701107 trace2_cmd_mode ("branch" );
11711108
@@ -1203,10 +1140,37 @@ static int switch_branches(const struct checkout_opts *opts,
12031140 do_merge = 0 ;
12041141 }
12051142
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+
12061152 if (do_merge ) {
12071153 ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1154+ if (ret && opts -> merge ) {
1155+ strbuf_addf (& autostash_msg ,
1156+ "autostash while switching to '%s'" ,
1157+ new_branch_info -> name );
1158+ create_autostash_ref_silent_with_msg (the_repository ,
1159+ "CHECKOUT_AUTOSTASH_HEAD" ,
1160+ autostash_msg .buf );
1161+ created_autostash = 1 ;
1162+ ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1163+ }
12081164 if (ret ) {
1165+ apply_autostash_ref_with_labels (the_repository ,
1166+ "CHECKOUT_AUTOSTASH_HEAD" ,
1167+ new_branch_info -> name ,
1168+ "local" ,
1169+ stash_label_base ,
1170+ autostash_msg .len ? autostash_msg .buf : NULL );
12091171 branch_info_release (& old_branch_info );
1172+ strbuf_release (& old_commit_shortname );
1173+ strbuf_release (& autostash_msg );
12101174 return ret ;
12111175 }
12121176 }
@@ -1216,8 +1180,31 @@ static int switch_branches(const struct checkout_opts *opts,
12161180
12171181 update_refs_for_switch (opts , & old_branch_info , new_branch_info );
12181182
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+ autostash_msg .len ? autostash_msg .buf : NULL );
1194+
1195+ discard_index (the_repository -> index );
1196+ if (repo_read_index (the_repository ) < 0 )
1197+ die (_ ("index file corrupt" ));
1198+
1199+ if (created_autostash && !opts -> discard_changes && !opts -> quiet &&
1200+ new_branch_info -> commit )
1201+ show_local_changes (& new_branch_info -> commit -> object ,
1202+ & opts -> diff_options );
1203+
12191204 ret = post_checkout_hook (old_branch_info .commit , new_branch_info -> commit , 1 );
12201205 branch_info_release (& old_branch_info );
1206+ strbuf_release (& old_commit_shortname );
1207+ strbuf_release (& autostash_msg );
12211208
12221209 return ret || writeout_error ;
12231210}
0 commit comments