@@ -938,31 +938,22 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
938
938
}
939
939
940
940
static int finish_automerge (struct commit * head ,
941
+ int head_subsumed ,
941
942
struct commit_list * common ,
942
943
struct commit_list * remoteheads ,
943
944
unsigned char * result_tree ,
944
945
const char * wt_strategy )
945
946
{
946
- struct commit_list * parents = NULL , * j ;
947
+ struct commit_list * parents = NULL ;
947
948
struct strbuf buf = STRBUF_INIT ;
948
949
unsigned char result_commit [20 ];
949
950
950
951
free_commit_list (common );
951
- if ( allow_fast_forward ) {
952
- parents = remoteheads ;
952
+ parents = remoteheads ;
953
+ if (! head_subsumed || ! allow_fast_forward )
953
954
commit_list_insert (head , & parents );
954
- parents = reduce_heads (parents );
955
- } else {
956
- struct commit_list * * pptr = & parents ;
957
-
958
- pptr = & commit_list_insert (head ,
959
- pptr )-> next ;
960
- for (j = remoteheads ; j ; j = j -> next )
961
- pptr = & commit_list_insert (j -> item , pptr )-> next ;
962
- }
963
955
strbuf_addch (& merge_msg , '\n' );
964
956
prepare_to_commit (remoteheads );
965
- free_commit_list (remoteheads );
966
957
if (commit_tree (& merge_msg , result_tree , parents , result_commit ,
967
958
NULL , sign_commit ))
968
959
die (_ ("failed to write commit object" ));
@@ -1137,19 +1128,37 @@ static int default_edit_option(void)
1137
1128
st_stdin .st_mode == st_stdout .st_mode );
1138
1129
}
1139
1130
1140
- static struct commit_list * collect_parents (int argc , const char * * argv )
1131
+ static struct commit_list * collect_parents (struct commit * head_commit ,
1132
+ int * head_subsumed ,
1133
+ int argc , const char * * argv )
1141
1134
{
1142
1135
int i ;
1143
- struct commit_list * remoteheads = NULL ;
1136
+ struct commit_list * remoteheads = NULL , * parents , * next ;
1144
1137
struct commit_list * * remotes = & remoteheads ;
1145
1138
1139
+ if (head_commit )
1140
+ remotes = & commit_list_insert (head_commit , remotes )-> next ;
1146
1141
for (i = 0 ; i < argc ; i ++ ) {
1147
1142
struct commit * commit = get_merge_parent (argv [i ]);
1148
1143
if (!commit )
1149
1144
die (_ ("%s - not something we can merge" ), argv [i ]);
1150
1145
remotes = & commit_list_insert (commit , remotes )-> next ;
1151
1146
}
1152
1147
* remotes = NULL ;
1148
+
1149
+ parents = reduce_heads (remoteheads );
1150
+
1151
+ * head_subsumed = 1 ; /* we will flip this to 0 when we find it */
1152
+ for (remoteheads = NULL , remotes = & remoteheads ;
1153
+ parents ;
1154
+ parents = next ) {
1155
+ struct commit * commit = parents -> item ;
1156
+ next = parents -> next ;
1157
+ if (commit == head_commit )
1158
+ * head_subsumed = 0 ;
1159
+ else
1160
+ remotes = & commit_list_insert (commit , remotes )-> next ;
1161
+ }
1153
1162
return remoteheads ;
1154
1163
}
1155
1164
@@ -1161,7 +1170,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1161
1170
struct commit * head_commit ;
1162
1171
struct strbuf buf = STRBUF_INIT ;
1163
1172
const char * head_arg ;
1164
- int flag , i , ret = 0 ;
1173
+ int flag , i , ret = 0 , head_subsumed ;
1165
1174
int best_cnt = -1 , merge_was_ok = 0 , automerge_was_ok = 0 ;
1166
1175
struct commit_list * common = NULL ;
1167
1176
const char * best_strategy = NULL , * wt_strategy = NULL ;
@@ -1270,7 +1279,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1270
1279
head_arg = argv [1 ];
1271
1280
argv += 2 ;
1272
1281
argc -= 2 ;
1273
- remoteheads = collect_parents (argc , argv );
1282
+ remoteheads = collect_parents (head_commit , & head_subsumed , argc , argv );
1274
1283
} else if (!head_commit ) {
1275
1284
struct commit * remote_head ;
1276
1285
/*
@@ -1286,7 +1295,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1286
1295
if (!allow_fast_forward )
1287
1296
die (_ ("Non-fast-forward commit does not make sense into "
1288
1297
"an empty head" ));
1289
- remoteheads = collect_parents (argc , argv );
1298
+ remoteheads = collect_parents (head_commit , & head_subsumed , argc , argv );
1290
1299
remote_head = remoteheads -> item ;
1291
1300
if (!remote_head )
1292
1301
die (_ ("%s - not something we can merge" ), argv [0 ]);
@@ -1305,7 +1314,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1305
1314
* the standard merge summary message to be appended
1306
1315
* to the given message.
1307
1316
*/
1308
- remoteheads = collect_parents (argc , argv );
1317
+ remoteheads = collect_parents (head_commit , & head_subsumed , argc , argv );
1309
1318
for (p = remoteheads ; p ; p = p -> next )
1310
1319
merge_name (merge_remote_util (p -> item )-> name , & merge_names );
1311
1320
@@ -1351,7 +1360,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1351
1360
option_edit = 0 ;
1352
1361
1353
1362
if (!use_strategies ) {
1354
- if (!remoteheads -> next )
1363
+ if (!remoteheads )
1364
+ ; /* already up-to-date */
1365
+ else if (!remoteheads -> next )
1355
1366
add_strategies (pull_twohead , DEFAULT_TWOHEAD );
1356
1367
else
1357
1368
add_strategies (pull_octopus , DEFAULT_OCTOPUS );
@@ -1364,7 +1375,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1364
1375
allow_trivial = 0 ;
1365
1376
}
1366
1377
1367
- if (!remoteheads -> next )
1378
+ if (!remoteheads )
1379
+ ; /* already up-to-date */
1380
+ else if (!remoteheads -> next )
1368
1381
common = get_merge_bases (head_commit , remoteheads -> item , 1 );
1369
1382
else {
1370
1383
struct commit_list * list = remoteheads ;
@@ -1376,10 +1389,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1376
1389
update_ref ("updating ORIG_HEAD" , "ORIG_HEAD" , head_commit -> object .sha1 ,
1377
1390
NULL , 0 , DIE_ON_ERR );
1378
1391
1379
- if (!common )
1392
+ if (remoteheads && !common )
1380
1393
; /* No common ancestors found. We need a real merge. */
1381
- else if (!remoteheads -> next && !common -> next &&
1382
- common -> item == remoteheads -> item ) {
1394
+ else if (!remoteheads ||
1395
+ (!remoteheads -> next && !common -> next &&
1396
+ common -> item == remoteheads -> item )) {
1383
1397
/*
1384
1398
* If head can reach all the merge then we are up to date.
1385
1399
* but first the most common case of merging one remote.
@@ -1553,7 +1567,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1553
1567
* auto resolved the merge cleanly.
1554
1568
*/
1555
1569
if (automerge_was_ok ) {
1556
- ret = finish_automerge (head_commit , common , remoteheads ,
1570
+ ret = finish_automerge (head_commit , head_subsumed ,
1571
+ common , remoteheads ,
1557
1572
result_tree , wt_strategy );
1558
1573
goto done ;
1559
1574
}
0 commit comments