revert: refactor merge recursive code into its own function
The code that is used to do a recursive merge is extracted from the revert_or_cherry_pick() function and put into a new do_recursive_merge() function. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Mar 31, 2010 at 21:22 UTC
ae8c79fd8fe47bfebf56ae83461cbedb934cff92
1 file changed
+58
-48
builtin/revert.c
+58
-48
@@ -279,18 +279,69 @@ static NORETURN void die_dirty_index(const char *me)
279
}
280
}
281
282
+static void do_recursive_merge(struct commit *base, struct commit *next,
283
+ const char *base_label, const char *next_label,
284
+ unsigned char *head, struct strbuf *msgbuf,
285
+ char *defmsg)
286
+{
287
+ struct merge_options o;
288
+ struct tree *result, *next_tree, *base_tree, *head_tree;
289
+ int clean, index_fd;
290
+ static struct lock_file index_lock;
291
+
292
+ index_fd = hold_locked_index(&index_lock, 1);
293
+
294
+ read_cache();
295
+ init_merge_options(&o);
296
+ o.ancestor = base ? base_label : "(empty tree)";
297
+ o.branch1 = "HEAD";
298
+ o.branch2 = next ? next_label : "(empty tree)";
299
+
300
+ head_tree = parse_tree_indirect(head);
301
+ next_tree = next ? next->tree : empty_tree();
302
+ base_tree = base ? base->tree : empty_tree();
303
+
304
+ clean = merge_trees(&o,
305
+ head_tree,
306
+ next_tree, base_tree, &result);
307
+
308
+ if (active_cache_changed &&
309
+ (write_cache(index_fd, active_cache, active_nr) ||
310
+ commit_locked_index(&index_lock)))
311
+ die("%s: Unable to write new index file", me);
312
+ rollback_lock_file(&index_lock);
313
+
314
+ if (!clean) {
315
+ int i;
316
+ strbuf_addstr(msgbuf, "\nConflicts:\n\n");
317
+ for (i = 0; i < active_nr;) {
318
+ struct cache_entry *ce = active_cache[i++];
319
+ if (ce_stage(ce)) {
320
+ strbuf_addch(msgbuf, '\t');
321
+ strbuf_addstr(msgbuf, ce->name);
322
+ strbuf_addch(msgbuf, '\n');
323
+ while (i < active_nr && !strcmp(ce->name,
324
+ active_cache[i]->name))
325
+ i++;
326
+ }
327
+ }
328
+ write_message(msgbuf, defmsg);
329
+ fprintf(stderr, "Automatic %s failed.%s\n",
330
+ me, help_msg(commit_name));
331
+ rerere(allow_rerere_auto);
332
+ exit(1);
333
+ }
334
+ write_message(msgbuf, defmsg);
335
+ fprintf(stderr, "Finished one %s.\n", me);
336
+}
337
+
338
static int revert_or_cherry_pick(int argc, const char **argv)
339
{
340
unsigned char head[20];
341
struct commit *base, *next, *parent;
342
const char *base_label, *next_label;
287
- int i, index_fd, clean;
343
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
289
-
344
char *defmsg = git_pathdup("MERGE_MSG");
291
- struct merge_options o;
292
- struct tree *result, *next_tree, *base_tree, *head_tree;
293
- static struct lock_file index_lock;
345
struct strbuf msgbuf = STRBUF_INIT;
346
347
git_config(git_default_config, NULL);
@@ -321,8 +372,6 @@ static int revert_or_cherry_pick(int argc, const char **argv)
372
}
373
discard_cache();
374
324
- index_fd = hold_locked_index(&index_lock, 1);
325
-
375
if (!commit->parents) {
376
if (action == REVERT)
377
die ("Cannot revert a root commit");
@@ -395,47 +444,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
444
}
445
}
446
398
- read_cache();
399
- init_merge_options(&o);
400
- o.ancestor = base ? base_label : "(empty tree)";
401
- o.branch1 = "HEAD";
402
- o.branch2 = next ? next_label : "(empty tree)";
403
-
404
- head_tree = parse_tree_indirect(head);
405
- next_tree = next ? next->tree : empty_tree();
406
- base_tree = base ? base->tree : empty_tree();
407
-
408
- clean = merge_trees(&o,
409
- head_tree,
410
- next_tree, base_tree, &result);
411
-
412
- if (active_cache_changed &&
413
- (write_cache(index_fd, active_cache, active_nr) ||
414
- commit_locked_index(&index_lock)))
415
- die("%s: Unable to write new index file", me);
416
- rollback_lock_file(&index_lock);
417
-
418
- if (!clean) {
419
- strbuf_addstr(&msgbuf, "\nConflicts:\n\n");
420
- for (i = 0; i < active_nr;) {
421
- struct cache_entry *ce = active_cache[i++];
422
- if (ce_stage(ce)) {
423
- strbuf_addch(&msgbuf, '\t');
424
- strbuf_addstr(&msgbuf, ce->name);
425
- strbuf_addch(&msgbuf, '\n');
426
- while (i < active_nr && !strcmp(ce->name,
427
- active_cache[i]->name))
428
- i++;
429
- }
430
- }
431
- write_message(&msgbuf, defmsg);
432
- fprintf(stderr, "Automatic %s failed.%s\n",
433
- me, help_msg(commit_name));
434
- rerere(allow_rerere_auto);
435
- exit(1);
436
- }
437
- write_message(&msgbuf, defmsg);
438
- fprintf(stderr, "Finished one %s.\n", me);
447
+ do_recursive_merge(base, next, base_label, next_label,
448
+ head, &msgbuf, defmsg);
449
450
/*
451
*