Skip to content

Commit f78eeea

Browse files
committed
Merge branch 'cc/revert-strategy'
* cc/revert-strategy: revert: add "--strategy" option to choose merge strategy merge: make function try_merge_command non static merge: refactor code that calls "git merge-STRATEGY" revert: refactor merge recursive code into its own function revert: use strbuf to refactor the code that writes the merge message Conflicts: builtin/revert.c
2 parents f350e1f + 91e5259 commit f78eeea

File tree

3 files changed

+153
-110
lines changed

3 files changed

+153
-110
lines changed

builtin/merge.c

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,53 @@ static void write_tree_trivial(unsigned char *sha1)
548548
die("git write-tree failed to write a tree");
549549
}
550550

551-
static int try_merge_strategy(const char *strategy, struct commit_list *common,
552-
const char *head_arg)
551+
int try_merge_command(const char *strategy, struct commit_list *common,
552+
const char *head_arg, struct commit_list *remotes)
553553
{
554554
const char **args;
555555
int i = 0, x = 0, ret;
556556
struct commit_list *j;
557557
struct strbuf buf = STRBUF_INIT;
558+
559+
args = xmalloc((4 + xopts_nr + commit_list_count(common) +
560+
commit_list_count(remotes)) * sizeof(char *));
561+
strbuf_addf(&buf, "merge-%s", strategy);
562+
args[i++] = buf.buf;
563+
for (x = 0; x < xopts_nr; x++) {
564+
char *s = xmalloc(strlen(xopts[x])+2+1);
565+
strcpy(s, "--");
566+
strcpy(s+2, xopts[x]);
567+
args[i++] = s;
568+
}
569+
for (j = common; j; j = j->next)
570+
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
571+
args[i++] = "--";
572+
args[i++] = head_arg;
573+
for (j = remotes; j; j = j->next)
574+
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
575+
args[i] = NULL;
576+
ret = run_command_v_opt(args, RUN_GIT_CMD);
577+
strbuf_release(&buf);
578+
i = 1;
579+
for (x = 0; x < xopts_nr; x++)
580+
free((void *)args[i++]);
581+
for (j = common; j; j = j->next)
582+
free((void *)args[i++]);
583+
i += 2;
584+
for (j = remotes; j; j = j->next)
585+
free((void *)args[i++]);
586+
free(args);
587+
discard_cache();
588+
if (read_cache() < 0)
589+
die("failed to read the cache");
590+
resolve_undo_clear();
591+
592+
return ret;
593+
}
594+
595+
static int try_merge_strategy(const char *strategy, struct commit_list *common,
596+
const char *head_arg)
597+
{
558598
int index_fd;
559599
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
560600

@@ -567,12 +607,13 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
567607
rollback_lock_file(lock);
568608

569609
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
570-
int clean;
610+
int clean, x;
571611
struct commit *result;
572612
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
573613
int index_fd;
574614
struct commit_list *reversed = NULL;
575615
struct merge_options o;
616+
struct commit_list *j;
576617

577618
if (remoteheads->next) {
578619
error("Not handling anything other than two heads merge.");
@@ -612,39 +653,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
612653
rollback_lock_file(lock);
613654
return clean ? 0 : 1;
614655
} else {
615-
args = xmalloc((4 + xopts_nr + commit_list_count(common) +
616-
commit_list_count(remoteheads)) * sizeof(char *));
617-
strbuf_addf(&buf, "merge-%s", strategy);
618-
args[i++] = buf.buf;
619-
for (x = 0; x < xopts_nr; x++) {
620-
char *s = xmalloc(strlen(xopts[x])+2+1);
621-
strcpy(s, "--");
622-
strcpy(s+2, xopts[x]);
623-
args[i++] = s;
624-
}
625-
for (j = common; j; j = j->next)
626-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
627-
args[i++] = "--";
628-
args[i++] = head_arg;
629-
for (j = remoteheads; j; j = j->next)
630-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
631-
args[i] = NULL;
632-
ret = run_command_v_opt(args, RUN_GIT_CMD);
633-
strbuf_release(&buf);
634-
i = 1;
635-
for (x = 0; x < xopts_nr; x++)
636-
free((void *)args[i++]);
637-
for (j = common; j; j = j->next)
638-
free((void *)args[i++]);
639-
i += 2;
640-
for (j = remoteheads; j; j = j->next)
641-
free((void *)args[i++]);
642-
free(args);
643-
discard_cache();
644-
if (read_cache() < 0)
645-
die("failed to read the cache");
646-
resolve_undo_clear();
647-
return ret;
656+
return try_merge_command(strategy, common, head_arg, remoteheads);
648657
}
649658
}
650659

builtin/revert.c

Lines changed: 105 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const char *commit_name;
4343
static int allow_rerere_auto;
4444

4545
static const char *me;
46+
static const char *strategy;
4647

4748
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
4849

@@ -62,6 +63,7 @@ static void parse_args(int argc, const char **argv)
6263
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
6364
OPT_INTEGER('m', "mainline", &mainline, "parent number"),
6465
OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
66+
OPT_STRING(0, "strategy", &strategy, "strategy", "merge strategy"),
6567
OPT_END(),
6668
OPT_END(),
6769
OPT_END(),
@@ -174,28 +176,17 @@ static char *get_encoding(const char *message)
174176
return NULL;
175177
}
176178

177-
static struct lock_file msg_file;
178-
static int msg_fd;
179-
180-
static void add_to_msg(const char *string)
181-
{
182-
int len = strlen(string);
183-
if (write_in_full(msg_fd, string, len) < 0)
184-
die_errno ("Could not write to MERGE_MSG");
185-
}
186-
187-
static void add_message_to_msg(const char *message)
179+
static void add_message_to_msg(struct strbuf *msgbuf, const char *message)
188180
{
189181
const char *p = message;
190182
while (*p && (*p != '\n' || p[1] != '\n'))
191183
p++;
192184

193185
if (!*p)
194-
add_to_msg(sha1_to_hex(commit->object.sha1));
186+
strbuf_addstr(msgbuf, sha1_to_hex(commit->object.sha1));
195187

196188
p += 2;
197-
add_to_msg(p);
198-
return;
189+
strbuf_addstr(msgbuf, p);
199190
}
200191

201192
static void set_author_ident_env(const char *message)
@@ -271,6 +262,19 @@ static char *help_msg(const char *name)
271262
return strbuf_detach(&helpbuf, NULL);
272263
}
273264

265+
static void write_message(struct strbuf *msgbuf, const char *filename)
266+
{
267+
static struct lock_file msg_file;
268+
269+
int msg_fd = hold_lock_file_for_update(&msg_file, filename,
270+
LOCK_DIE_ON_ERROR);
271+
if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
272+
die_errno("Could not write to %s.", filename);
273+
strbuf_release(msgbuf);
274+
if (commit_lock_file(&msg_file) < 0)
275+
die("Error wrapping up %s", filename);
276+
}
277+
274278
static struct tree *empty_tree(void)
275279
{
276280
struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -305,17 +309,70 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from)
305309
return write_ref_sha1(ref_lock, to, "cherry-pick");
306310
}
307311

312+
static void do_recursive_merge(struct commit *base, struct commit *next,
313+
const char *base_label, const char *next_label,
314+
unsigned char *head, struct strbuf *msgbuf,
315+
char *defmsg)
316+
{
317+
struct merge_options o;
318+
struct tree *result, *next_tree, *base_tree, *head_tree;
319+
int clean, index_fd;
320+
static struct lock_file index_lock;
321+
322+
index_fd = hold_locked_index(&index_lock, 1);
323+
324+
read_cache();
325+
init_merge_options(&o);
326+
o.ancestor = base ? base_label : "(empty tree)";
327+
o.branch1 = "HEAD";
328+
o.branch2 = next ? next_label : "(empty tree)";
329+
330+
head_tree = parse_tree_indirect(head);
331+
next_tree = next ? next->tree : empty_tree();
332+
base_tree = base ? base->tree : empty_tree();
333+
334+
clean = merge_trees(&o,
335+
head_tree,
336+
next_tree, base_tree, &result);
337+
338+
if (active_cache_changed &&
339+
(write_cache(index_fd, active_cache, active_nr) ||
340+
commit_locked_index(&index_lock)))
341+
die("%s: Unable to write new index file", me);
342+
rollback_lock_file(&index_lock);
343+
344+
if (!clean) {
345+
int i;
346+
strbuf_addstr(msgbuf, "\nConflicts:\n\n");
347+
for (i = 0; i < active_nr;) {
348+
struct cache_entry *ce = active_cache[i++];
349+
if (ce_stage(ce)) {
350+
strbuf_addch(msgbuf, '\t');
351+
strbuf_addstr(msgbuf, ce->name);
352+
strbuf_addch(msgbuf, '\n');
353+
while (i < active_nr && !strcmp(ce->name,
354+
active_cache[i]->name))
355+
i++;
356+
}
357+
}
358+
write_message(msgbuf, defmsg);
359+
fprintf(stderr, "Automatic %s failed.%s\n",
360+
me, help_msg(commit_name));
361+
rerere(allow_rerere_auto);
362+
exit(1);
363+
}
364+
write_message(msgbuf, defmsg);
365+
fprintf(stderr, "Finished one %s.\n", me);
366+
}
367+
308368
static int revert_or_cherry_pick(int argc, const char **argv)
309369
{
310370
unsigned char head[20];
311371
struct commit *base, *next, *parent;
312372
const char *base_label, *next_label;
313-
int i, index_fd, clean;
314373
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
315374
char *defmsg = NULL;
316-
struct merge_options o;
317-
struct tree *result, *next_tree, *base_tree, *head_tree;
318-
static struct lock_file index_lock;
375+
struct strbuf msgbuf = STRBUF_INIT;
319376

320377
git_config(git_default_config, NULL);
321378
me = action == REVERT ? "revert" : "cherry-pick";
@@ -403,83 +460,57 @@ static int revert_or_cherry_pick(int argc, const char **argv)
403460
*/
404461

405462
defmsg = git_pathdup("MERGE_MSG");
406-
msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
407-
LOCK_DIE_ON_ERROR);
408-
409-
index_fd = hold_locked_index(&index_lock, 1);
410463

411464
if (action == REVERT) {
412465
base = commit;
413466
base_label = msg.label;
414467
next = parent;
415468
next_label = msg.parent_label;
416-
add_to_msg("Revert \"");
417-
add_to_msg(msg.subject);
418-
add_to_msg("\"\n\nThis reverts commit ");
419-
add_to_msg(sha1_to_hex(commit->object.sha1));
469+
strbuf_addstr(&msgbuf, "Revert \"");
470+
strbuf_addstr(&msgbuf, msg.subject);
471+
strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
472+
strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
420473

421474
if (commit->parents->next) {
422-
add_to_msg(", reversing\nchanges made to ");
423-
add_to_msg(sha1_to_hex(parent->object.sha1));
475+
strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
476+
strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1));
424477
}
425-
add_to_msg(".\n");
478+
strbuf_addstr(&msgbuf, ".\n");
426479
} else {
427480
base = parent;
428481
base_label = msg.parent_label;
429482
next = commit;
430483
next_label = msg.label;
431484
set_author_ident_env(msg.message);
432-
add_message_to_msg(msg.message);
485+
add_message_to_msg(&msgbuf, msg.message);
433486
if (no_replay) {
434-
add_to_msg("(cherry picked from commit ");
435-
add_to_msg(sha1_to_hex(commit->object.sha1));
436-
add_to_msg(")\n");
487+
strbuf_addstr(&msgbuf, "(cherry picked from commit ");
488+
strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
489+
strbuf_addstr(&msgbuf, ")\n");
437490
}
438491
}
439492

440-
read_cache();
441-
init_merge_options(&o);
442-
o.ancestor = base ? base_label : "(empty tree)";
443-
o.branch1 = "HEAD";
444-
o.branch2 = next ? next_label : "(empty tree)";
445-
446-
head_tree = parse_tree_indirect(head);
447-
next_tree = next ? next->tree : empty_tree();
448-
base_tree = base ? base->tree : empty_tree();
449-
450-
clean = merge_trees(&o,
451-
head_tree,
452-
next_tree, base_tree, &result);
453-
454-
if (active_cache_changed &&
455-
(write_cache(index_fd, active_cache, active_nr) ||
456-
commit_locked_index(&index_lock)))
457-
die("%s: Unable to write new index file", me);
458-
rollback_lock_file(&index_lock);
459-
460-
if (!clean) {
461-
add_to_msg("\nConflicts:\n\n");
462-
for (i = 0; i < active_nr;) {
463-
struct cache_entry *ce = active_cache[i++];
464-
if (ce_stage(ce)) {
465-
add_to_msg("\t");
466-
add_to_msg(ce->name);
467-
add_to_msg("\n");
468-
while (i < active_nr && !strcmp(ce->name,
469-
active_cache[i]->name))
470-
i++;
471-
}
493+
if (!strategy || !strcmp(strategy, "recursive") || action == REVERT)
494+
do_recursive_merge(base, next, base_label, next_label,
495+
head, &msgbuf, defmsg);
496+
else {
497+
int res;
498+
struct commit_list *common = NULL;
499+
struct commit_list *remotes = NULL;
500+
write_message(&msgbuf, defmsg);
501+
commit_list_insert(base, &common);
502+
commit_list_insert(next, &remotes);
503+
res = try_merge_command(strategy, common,
504+
sha1_to_hex(head), remotes);
505+
free_commit_list(common);
506+
free_commit_list(remotes);
507+
if (res) {
508+
fprintf(stderr, "Automatic %s with strategy %s failed.%s\n",
509+
me, strategy, help_msg(commit_name));
510+
rerere(allow_rerere_auto);
511+
exit(1);
472512
}
473-
if (commit_lock_file(&msg_file) < 0)
474-
die ("Error wrapping up %s", defmsg);
475-
fprintf(stderr, "Automatic %s failed.%s\n",
476-
me, help_msg(commit_name));
477-
rerere(allow_rerere_auto);
478-
exit(1);
479513
}
480-
if (commit_lock_file(&msg_file) < 0)
481-
die ("Error wrapping up %s", defmsg);
482-
fprintf(stderr, "Finished one %s.\n", me);
483514

484515
/*
485516
*

merge-recursive.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@ int merge_recursive_generic(struct merge_options *o,
5454
void init_merge_options(struct merge_options *o);
5555
struct tree *write_tree_from_memory(struct merge_options *o);
5656

57+
/* builtin/merge.c */
58+
int try_merge_command(const char *strategy, struct commit_list *common, const char *head_arg, struct commit_list *remotes);
59+
5760
#endif

0 commit comments

Comments
 (0)