/*
* Builtin "git merge"
*
* Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
*
* Based on git-merge.sh by Junio C Hamano.
*/
#include "cache.h"
#include "parse-options.h"
#include "builtin.h"
#include "run-command.h"
#include "diff.h"
#include "refs.h"
#include "commit.h"
#include "diffcore.h"
#include "revision.h"
#include "unpack-trees.h"
#include "cache-tree.h"
#include "dir.h"
#include "utf8.h"
#include "log-tree.h"
#include "color.h"
#include "rerere.h"
#define DEFAULT_TWOHEAD (1<<0)
#define DEFAULT_OCTOPUS (1<<1)
#define NO_FAST_FORWARD (1<<2)
#define NO_TRIVIAL (1<<3)
struct strategy {
const char *name;
unsigned attr;
};
static const char * const builtin_merge_usage[] = {
"git-merge [options] <remote>...",
"git-merge [options] <msg> HEAD <remote>",
NULL
};
static int show_diffstat = 1, option_log, squash;
static int option_commit = 1, allow_fast_forward = 1;
static int allow_trivial = 1, have_message;
static struct strbuf merge_msg;
static struct commit_list *remoteheads;
static unsigned char head[20], stash[20];
static struct strategy **use_strategies;
static size_t use_strategies_nr, use_strategies_alloc;
static const char *branch;
static struct strategy all_strategy[] = {
{ "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
{ "octopus", DEFAULT_OCTOPUS },
{ "resolve", 0 },
{ "ours", NO_FAST_FORWARD | NO_TRIVIAL },
{ "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
};
static const char *pull_twohead, *pull_octopus;
static int option_parse_message(const struct option *opt,
const char *arg, int unset)
{
struct strbuf *buf = opt->value;
if (unset)
strbuf_setlen(buf, 0);
else if (arg) {
strbuf_addf(buf, "%s\n\n", arg);
have_message = 1;
} else
return error("switch `m' requires a value");
return 0;
}
static struct strategy *get_strategy(const char *name)
{
int i;
struct strbuf err;
if (!name)
return NULL;
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
if (!strcmp(name, all_strategy[i].name))
return &all_strategy[i];
strbuf_init(&err, 0);
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
strbuf_addf(&err, " %s", all_strategy[i].name);
fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
fprintf(stderr, "Available strategies are:%s.\n", err.buf);
exit(1);
}
static void append_strategy(struct strategy *s)
{
ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
use_strategies[use_strategies_nr++] = s;
}
static int option_parse_strategy(const struct option *opt,
const char *name, int unset)
{
if (unset)
return 0;
append_strategy(get_strategy(name));
return 0;
}
static int option_parse_n(const struct option *opt,
const char *arg, int unset)
{
show_diffstat = unset;
return 0;
}
static struct option builtin_merge_options[] = {
{ OPTION_CALLBACK, 'n', NULL, NULL, NULL,
"do not show a diffstat at the end of the merge",
PARSE_OPT_NOARG, option_parse_n },
OPT_BOOLEAN(0, "stat", &show_diffstat,
"show a diffstat at the end of the merge"),
OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"),
OPT_BOOLEAN(0, "log", &option_log,
"add list of one-line log to merge commit message"),
OPT_BOOLEAN(0, "squash", &squash,
"create a single commit instead of doing a merge"),
OPT_BOOLEAN(0, "commit", &option_commit,
"perform a commit if the merge succeeds (default)"),
OPT_BOOLEAN(0, "ff", &allow_fast_forward,
"allow fast forward (default)"),
OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
"merge strategy to use", option_parse_strategy),
OPT_CALLBACK('m', "message", &merge_msg, "message",
"message to be used for the merge commit (if any)",
option_parse_message),
OPT_END()
};
/* Cleans up metadata that is uninteresting after a succeeded merge. */
static void drop_save(void)
|