@@ -50,7 +50,6 @@ static int fast_forward_only;
50
50
static int allow_trivial = 1 , have_message ;
51
51
static struct strbuf merge_msg ;
52
52
static struct commit_list * remoteheads ;
53
- static unsigned char head [20 ];
54
53
static struct strategy * * use_strategies ;
55
54
static size_t use_strategies_nr , use_strategies_alloc ;
56
55
static const char * * xopts ;
@@ -279,7 +278,8 @@ static void reset_hard(unsigned const char *sha1, int verbose)
279
278
die (_ ("read-tree failed" ));
280
279
}
281
280
282
- static void restore_state (const unsigned char * stash )
281
+ static void restore_state (const unsigned char * head ,
282
+ const unsigned char * stash )
283
283
{
284
284
struct strbuf sb = STRBUF_INIT ;
285
285
const char * args [] = { "stash" , "apply" , NULL , NULL };
@@ -309,10 +309,9 @@ static void finish_up_to_date(const char *msg)
309
309
drop_save ();
310
310
}
311
311
312
- static void squash_message (void )
312
+ static void squash_message (struct commit * commit )
313
313
{
314
314
struct rev_info rev ;
315
- struct commit * commit ;
316
315
struct strbuf out = STRBUF_INIT ;
317
316
struct commit_list * j ;
318
317
int fd ;
@@ -327,7 +326,6 @@ static void squash_message(void)
327
326
rev .ignore_merges = 1 ;
328
327
rev .commit_format = CMIT_FMT_MEDIUM ;
329
328
330
- commit = lookup_commit (head );
331
329
commit -> object .flags |= UNINTERESTING ;
332
330
add_pending_object (& rev , & commit -> object , NULL );
333
331
@@ -356,9 +354,11 @@ static void squash_message(void)
356
354
strbuf_release (& out );
357
355
}
358
356
359
- static void finish (const unsigned char * new_head , const char * msg )
357
+ static void finish (struct commit * head_commit ,
358
+ const unsigned char * new_head , const char * msg )
360
359
{
361
360
struct strbuf reflog_message = STRBUF_INIT ;
361
+ const unsigned char * head = head_commit -> object .sha1 ;
362
362
363
363
if (!msg )
364
364
strbuf_addstr (& reflog_message , getenv ("GIT_REFLOG_ACTION" ));
@@ -369,7 +369,7 @@ static void finish(const unsigned char *new_head, const char *msg)
369
369
getenv ("GIT_REFLOG_ACTION" ), msg );
370
370
}
371
371
if (squash ) {
372
- squash_message ();
372
+ squash_message (head_commit );
373
373
} else {
374
374
if (verbosity >= 0 && !merge_msg .len )
375
375
printf (_ ("No merge message -- not updating HEAD\n" ));
@@ -664,7 +664,7 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
664
664
}
665
665
666
666
static int try_merge_strategy (const char * strategy , struct commit_list * common ,
667
- const char * head_arg )
667
+ struct commit * head , const char * head_arg )
668
668
{
669
669
int index_fd ;
670
670
struct lock_file * lock = xcalloc (1 , sizeof (struct lock_file ));
@@ -710,7 +710,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
710
710
commit_list_insert (j -> item , & reversed );
711
711
712
712
index_fd = hold_locked_index (lock , 1 );
713
- clean = merge_recursive (& o , lookup_commit ( head ) ,
713
+ clean = merge_recursive (& o , head ,
714
714
remoteheads -> item , reversed , & result );
715
715
if (active_cache_changed &&
716
716
(write_cache (index_fd , active_cache , active_nr ) ||
@@ -861,25 +861,26 @@ static void run_prepare_commit_msg(void)
861
861
read_merge_msg ();
862
862
}
863
863
864
- static int merge_trivial (void )
864
+ static int merge_trivial (struct commit * head )
865
865
{
866
866
unsigned char result_tree [20 ], result_commit [20 ];
867
867
struct commit_list * parent = xmalloc (sizeof (* parent ));
868
868
869
869
write_tree_trivial (result_tree );
870
870
printf (_ ("Wonderful.\n" ));
871
- parent -> item = lookup_commit ( head ) ;
871
+ parent -> item = head ;
872
872
parent -> next = xmalloc (sizeof (* parent -> next ));
873
873
parent -> next -> item = remoteheads -> item ;
874
874
parent -> next -> next = NULL ;
875
875
run_prepare_commit_msg ();
876
876
commit_tree (merge_msg .buf , result_tree , parent , result_commit , NULL );
877
- finish (result_commit , "In-index merge" );
877
+ finish (head , result_commit , "In-index merge" );
878
878
drop_save ();
879
879
return 0 ;
880
880
}
881
881
882
- static int finish_automerge (struct commit_list * common ,
882
+ static int finish_automerge (struct commit * head ,
883
+ struct commit_list * common ,
883
884
unsigned char * result_tree ,
884
885
const char * wt_strategy )
885
886
{
@@ -890,12 +891,12 @@ static int finish_automerge(struct commit_list *common,
890
891
free_commit_list (common );
891
892
if (allow_fast_forward ) {
892
893
parents = remoteheads ;
893
- commit_list_insert (lookup_commit ( head ) , & parents );
894
+ commit_list_insert (head , & parents );
894
895
parents = reduce_heads (parents );
895
896
} else {
896
897
struct commit_list * * pptr = & parents ;
897
898
898
- pptr = & commit_list_insert (lookup_commit ( head ) ,
899
+ pptr = & commit_list_insert (head ,
899
900
pptr )-> next ;
900
901
for (j = remoteheads ; j ; j = j -> next )
901
902
pptr = & commit_list_insert (j -> item , pptr )-> next ;
@@ -905,7 +906,7 @@ static int finish_automerge(struct commit_list *common,
905
906
run_prepare_commit_msg ();
906
907
commit_tree (merge_msg .buf , result_tree , parents , result_commit , NULL );
907
908
strbuf_addf (& buf , "Merge made by %s." , wt_strategy );
908
- finish (result_commit , buf .buf );
909
+ finish (head , result_commit , buf .buf );
909
910
strbuf_release (& buf );
910
911
drop_save ();
911
912
return 0 ;
@@ -939,7 +940,8 @@ static int suggest_conflicts(int renormalizing)
939
940
return 1 ;
940
941
}
941
942
942
- static struct commit * is_old_style_invocation (int argc , const char * * argv )
943
+ static struct commit * is_old_style_invocation (int argc , const char * * argv ,
944
+ const unsigned char * head )
943
945
{
944
946
struct commit * second_token = NULL ;
945
947
if (argc > 2 ) {
@@ -1012,9 +1014,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1012
1014
{
1013
1015
unsigned char result_tree [20 ];
1014
1016
unsigned char stash [20 ];
1017
+ unsigned char head_sha1 [20 ];
1018
+ struct commit * head_commit ;
1015
1019
struct strbuf buf = STRBUF_INIT ;
1016
1020
const char * head_arg ;
1017
- int flag , head_invalid = 0 , i ;
1021
+ int flag , i ;
1018
1022
int best_cnt = -1 , merge_was_ok = 0 , automerge_was_ok = 0 ;
1019
1023
struct commit_list * common = NULL ;
1020
1024
const char * best_strategy = NULL , * wt_strategy = NULL ;
@@ -1027,11 +1031,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1027
1031
* Check if we are _not_ on a detached HEAD, i.e. if there is a
1028
1032
* current branch.
1029
1033
*/
1030
- branch = resolve_ref ("HEAD" , head , 0 , & flag );
1034
+ branch = resolve_ref ("HEAD" , head_sha1 , 0 , & flag );
1031
1035
if (branch && !prefixcmp (branch , "refs/heads/" ))
1032
1036
branch += 11 ;
1033
- if (!branch || is_null_sha1 (head ))
1034
- head_invalid = 1 ;
1037
+ if (!branch || is_null_sha1 (head_sha1 ))
1038
+ head_commit = NULL ;
1039
+ else {
1040
+ head_commit = lookup_commit (head_sha1 );
1041
+ if (!head_commit )
1042
+ die (_ ("could not parse HEAD" ));
1043
+ }
1035
1044
1036
1045
git_config (git_merge_config , NULL );
1037
1046
@@ -1112,12 +1121,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1112
1121
* additional safety measure to check for it.
1113
1122
*/
1114
1123
1115
- if (!have_message && is_old_style_invocation (argc , argv )) {
1124
+ if (!have_message && head_commit &&
1125
+ is_old_style_invocation (argc , argv , head_commit -> object .sha1 )) {
1116
1126
strbuf_addstr (& merge_msg , argv [0 ]);
1117
1127
head_arg = argv [1 ];
1118
1128
argv += 2 ;
1119
1129
argc -= 2 ;
1120
- } else if (head_invalid ) {
1130
+ } else if (! head_commit ) {
1121
1131
struct object * remote_head ;
1122
1132
/*
1123
1133
* If the merged head is a valid one there is no reason
@@ -1164,7 +1174,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1164
1174
}
1165
1175
}
1166
1176
1167
- if (head_invalid || !argc )
1177
+ if (! head_commit || !argc )
1168
1178
usage_with_options (builtin_merge_usage ,
1169
1179
builtin_merge_options );
1170
1180
@@ -1205,17 +1215,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1205
1215
}
1206
1216
1207
1217
if (!remoteheads -> next )
1208
- common = get_merge_bases (lookup_commit (head ),
1209
- remoteheads -> item , 1 );
1218
+ common = get_merge_bases (head_commit , remoteheads -> item , 1 );
1210
1219
else {
1211
1220
struct commit_list * list = remoteheads ;
1212
- commit_list_insert (lookup_commit ( head ) , & list );
1221
+ commit_list_insert (head_commit , & list );
1213
1222
common = get_octopus_merge_bases (list );
1214
1223
free (list );
1215
1224
}
1216
1225
1217
- update_ref ("updating ORIG_HEAD" , "ORIG_HEAD" , head , NULL , 0 ,
1218
- DIE_ON_ERR );
1226
+ update_ref ("updating ORIG_HEAD" , "ORIG_HEAD" , head_commit -> object . sha1 ,
1227
+ NULL , 0 , DIE_ON_ERR );
1219
1228
1220
1229
if (!common )
1221
1230
; /* No common ancestors found. We need a real merge. */
@@ -1229,13 +1238,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1229
1238
return 0 ;
1230
1239
} else if (allow_fast_forward && !remoteheads -> next &&
1231
1240
!common -> next &&
1232
- !hashcmp (common -> item -> object .sha1 , head )) {
1241
+ !hashcmp (common -> item -> object .sha1 , head_commit -> object . sha1 )) {
1233
1242
/* Again the most common case of merging one remote. */
1234
1243
struct strbuf msg = STRBUF_INIT ;
1235
1244
struct object * o ;
1236
1245
char hex [41 ];
1237
1246
1238
- strcpy (hex , find_unique_abbrev (head , DEFAULT_ABBREV ));
1247
+ strcpy (hex , find_unique_abbrev (head_commit -> object . sha1 , DEFAULT_ABBREV ));
1239
1248
1240
1249
if (verbosity >= 0 )
1241
1250
printf (_ ("Updating %s..%s\n" ),
@@ -1251,10 +1260,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1251
1260
if (!o )
1252
1261
return 1 ;
1253
1262
1254
- if (checkout_fast_forward (head , remoteheads -> item -> object .sha1 ))
1263
+ if (checkout_fast_forward (head_commit -> object . sha1 , remoteheads -> item -> object .sha1 ))
1255
1264
return 1 ;
1256
1265
1257
- finish (o -> sha1 , msg .buf );
1266
+ finish (head_commit , o -> sha1 , msg .buf );
1258
1267
drop_save ();
1259
1268
return 0 ;
1260
1269
} else if (!remoteheads -> next && common -> next )
@@ -1274,8 +1283,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1274
1283
git_committer_info (IDENT_ERROR_ON_NO_NAME );
1275
1284
printf (_ ("Trying really trivial in-index merge...\n" ));
1276
1285
if (!read_tree_trivial (common -> item -> object .sha1 ,
1277
- head , remoteheads -> item -> object .sha1 ))
1278
- return merge_trivial ();
1286
+ head_commit -> object . sha1 , remoteheads -> item -> object .sha1 ))
1287
+ return merge_trivial (head_commit );
1279
1288
printf (_ ("Nope.\n" ));
1280
1289
}
1281
1290
} else {
@@ -1294,8 +1303,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1294
1303
* merge_bases again, otherwise "git merge HEAD^
1295
1304
* HEAD^^" would be missed.
1296
1305
*/
1297
- common_one = get_merge_bases (lookup_commit (head ),
1298
- j -> item , 1 );
1306
+ common_one = get_merge_bases (head_commit , j -> item , 1 );
1299
1307
if (hashcmp (common_one -> item -> object .sha1 ,
1300
1308
j -> item -> object .sha1 )) {
1301
1309
up_to_date = 0 ;
@@ -1333,7 +1341,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1333
1341
int ret ;
1334
1342
if (i ) {
1335
1343
printf (_ ("Rewinding the tree to pristine...\n" ));
1336
- restore_state (stash );
1344
+ restore_state (head_commit -> object . sha1 , stash );
1337
1345
}
1338
1346
if (use_strategies_nr != 1 )
1339
1347
printf (_ ("Trying merge strategy %s...\n" ),
@@ -1345,7 +1353,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1345
1353
wt_strategy = use_strategies [i ]-> name ;
1346
1354
1347
1355
ret = try_merge_strategy (use_strategies [i ]-> name ,
1348
- common , head_arg );
1356
+ common , head_commit , head_arg );
1349
1357
if (!option_commit && !ret ) {
1350
1358
merge_was_ok = 1 ;
1351
1359
/*
@@ -1387,14 +1395,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1387
1395
* auto resolved the merge cleanly.
1388
1396
*/
1389
1397
if (automerge_was_ok )
1390
- return finish_automerge (common , result_tree , wt_strategy );
1398
+ return finish_automerge (head_commit , common , result_tree ,
1399
+ wt_strategy );
1391
1400
1392
1401
/*
1393
1402
* Pick the result from the best strategy and have the user fix
1394
1403
* it up.
1395
1404
*/
1396
1405
if (!best_strategy ) {
1397
- restore_state (stash );
1406
+ restore_state (head_commit -> object . sha1 , stash );
1398
1407
if (use_strategies_nr > 1 )
1399
1408
fprintf (stderr ,
1400
1409
_ ("No merge strategy handled the merge.\n" ));
@@ -1406,14 +1415,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1406
1415
; /* We already have its result in the working tree. */
1407
1416
else {
1408
1417
printf (_ ("Rewinding the tree to pristine...\n" ));
1409
- restore_state (stash );
1418
+ restore_state (head_commit -> object . sha1 , stash );
1410
1419
printf (_ ("Using the %s to prepare resolving by hand.\n" ),
1411
1420
best_strategy );
1412
- try_merge_strategy (best_strategy , common , head_arg );
1421
+ try_merge_strategy (best_strategy , common , head_commit , head_arg );
1413
1422
}
1414
1423
1415
1424
if (squash )
1416
- finish (NULL , NULL );
1425
+ finish (head_commit , NULL , NULL );
1417
1426
else {
1418
1427
int fd ;
1419
1428
struct commit_list * j ;
0 commit comments