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"
@@ -845,83 +845,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
845845
846846 ret = unpack_trees (2 , trees , & topts );
847847 clear_unpack_trees_porcelain (& topts );
848- 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-
896- ret = reset_tree (new_tree ,
897- opts , 1 ,
898- writeout_error , new_branch_info );
899- if (ret )
900- return ret ;
901- o .ancestor = old_branch_info -> name ;
902- if (!old_branch_info -> name ) {
903- strbuf_add_unique_abbrev (& old_commit_shortname ,
904- & old_branch_info -> commit -> object .oid ,
905- DEFAULT_ABBREV );
906- o .ancestor = old_commit_shortname .buf ;
907- }
908- o .branch1 = new_branch_info -> name ;
909- o .branch2 = "local" ;
910- o .conflict_style = opts -> conflict_style ;
911- ret = merge_ort_nonrecursive (& o ,
912- new_tree ,
913- work ,
914- old_tree );
915- if (ret < 0 )
916- die (NULL );
917- ret = reset_tree (new_tree ,
918- opts , 0 ,
919- writeout_error , new_branch_info );
920- strbuf_release (& o .obuf );
921- strbuf_release (& old_commit_shortname );
922- if (ret )
923- return ret ;
924- }
848+ if (ret == -1 )
849+ return 1 ;
925850 }
926851
927852 if (!cache_tree_fully_valid (the_repository -> index -> cache_tree ))
@@ -1157,6 +1082,55 @@ static void orphaned_commit_warning(struct commit *old_commit, struct commit *ne
11571082 release_revisions (& revs );
11581083}
11591084
1085+ static int checkout_would_clobber_changes (struct branch_info * old_branch_info ,
1086+ struct branch_info * new_branch_info )
1087+ {
1088+ struct tree_desc trees [2 ];
1089+ struct tree * old_tree , * new_tree ;
1090+ struct unpack_trees_options topts ;
1091+ struct index_state tmp_index = INDEX_STATE_INIT (the_repository );
1092+ const struct object_id * old_commit_oid ;
1093+ int ret ;
1094+
1095+ if (!new_branch_info -> commit )
1096+ return 0 ;
1097+
1098+ old_commit_oid = old_branch_info -> commit ?
1099+ & old_branch_info -> commit -> object .oid :
1100+ the_hash_algo -> empty_tree ;
1101+ old_tree = repo_parse_tree_indirect (the_repository , old_commit_oid );
1102+ if (!old_tree )
1103+ return 0 ;
1104+
1105+ new_tree = repo_get_commit_tree (the_repository ,
1106+ new_branch_info -> commit );
1107+ if (!new_tree )
1108+ return 0 ;
1109+ if (repo_parse_tree (the_repository , new_tree ) < 0 )
1110+ return 0 ;
1111+
1112+ memset (& topts , 0 , sizeof (topts ));
1113+ topts .head_idx = -1 ;
1114+ topts .src_index = the_repository -> index ;
1115+ topts .dst_index = & tmp_index ;
1116+ topts .initial_checkout = is_index_unborn (the_repository -> index );
1117+ topts .merge = 1 ;
1118+ topts .update = 1 ;
1119+ topts .dry_run = 1 ;
1120+ topts .quiet = 1 ;
1121+ topts .fn = twoway_merge ;
1122+
1123+ init_tree_desc (& trees [0 ], & old_tree -> object .oid ,
1124+ old_tree -> buffer , old_tree -> size );
1125+ init_tree_desc (& trees [1 ], & new_tree -> object .oid ,
1126+ new_tree -> buffer , new_tree -> size );
1127+
1128+ ret = unpack_trees (2 , trees , & topts );
1129+ discard_index (& tmp_index );
1130+
1131+ return ret != 0 ;
1132+ }
1133+
11601134static int switch_branches (const struct checkout_opts * opts ,
11611135 struct branch_info * new_branch_info )
11621136{
@@ -1165,6 +1139,9 @@ static int switch_branches(const struct checkout_opts *opts,
11651139 struct object_id rev ;
11661140 int flag , writeout_error = 0 ;
11671141 int do_merge = 1 ;
1142+ int created_autostash = 0 ;
1143+ struct strbuf old_commit_shortname = STRBUF_INIT ;
1144+ const char * stash_label_ancestor = NULL ;
11681145
11691146 trace2_cmd_mode ("branch" );
11701147
@@ -1202,10 +1179,36 @@ static int switch_branches(const struct checkout_opts *opts,
12021179 do_merge = 0 ;
12031180 }
12041181
1182+ if (old_branch_info .name )
1183+ stash_label_ancestor = old_branch_info .name ;
1184+ else if (old_branch_info .commit ) {
1185+ strbuf_add_unique_abbrev (& old_commit_shortname ,
1186+ & old_branch_info .commit -> object .oid ,
1187+ DEFAULT_ABBREV );
1188+ stash_label_ancestor = old_commit_shortname .buf ;
1189+ }
1190+
1191+ if (opts -> merge ) {
1192+ if (repo_read_index (the_repository ) < 0 )
1193+ die (_ ("index file corrupt" ));
1194+ if (checkout_would_clobber_changes (& old_branch_info ,
1195+ new_branch_info )) {
1196+ create_autostash_ref_silent (the_repository ,
1197+ "CHECKOUT_AUTOSTASH" );
1198+ created_autostash = 1 ;
1199+ }
1200+ }
1201+
12051202 if (do_merge ) {
12061203 ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
12071204 if (ret ) {
1205+ apply_autostash_ref_with_labels (the_repository ,
1206+ "CHECKOUT_AUTOSTASH" ,
1207+ new_branch_info -> name ,
1208+ "local" ,
1209+ stash_label_ancestor );
12081210 branch_info_release (& old_branch_info );
1211+ strbuf_release (& old_commit_shortname );
12091212 return ret ;
12101213 }
12111214 }
@@ -1215,8 +1218,29 @@ static int switch_branches(const struct checkout_opts *opts,
12151218
12161219 update_refs_for_switch (opts , & old_branch_info , new_branch_info );
12171220
1221+ if (opts -> conflict_style >= 0 ) {
1222+ struct strbuf cfg = STRBUF_INIT ;
1223+ strbuf_addf (& cfg , "merge.conflictStyle=%s" ,
1224+ conflict_style_name (opts -> conflict_style ));
1225+ git_config_push_parameter (cfg .buf );
1226+ strbuf_release (& cfg );
1227+ }
1228+ apply_autostash_ref_with_labels (the_repository , "CHECKOUT_AUTOSTASH" ,
1229+ new_branch_info -> name , "local" ,
1230+ stash_label_ancestor );
1231+
1232+ discard_index (the_repository -> index );
1233+ if (repo_read_index (the_repository ) < 0 )
1234+ die (_ ("index file corrupt" ));
1235+
1236+ if (created_autostash && !opts -> discard_changes && !opts -> quiet &&
1237+ new_branch_info -> commit )
1238+ show_local_changes (& new_branch_info -> commit -> object ,
1239+ & opts -> diff_options );
1240+
12181241 ret = post_checkout_hook (old_branch_info .commit , new_branch_info -> commit , 1 );
12191242 branch_info_release (& old_branch_info );
1243+ strbuf_release (& old_commit_shortname );
12201244
12211245 return ret || writeout_error ;
12221246}
0 commit comments