2005-04-18 02:52:54 +00:00
|
|
|
#include "cache.h"
|
2007-03-12 23:00:21 +00:00
|
|
|
#include "run-command.h"
|
2009-01-18 12:00:12 +00:00
|
|
|
#include "exec_cmd.h"
|
2005-04-18 02:52:54 +00:00
|
|
|
|
2006-08-15 17:23:48 +00:00
|
|
|
static const char *pgm;
|
2005-07-29 12:53:38 +00:00
|
|
|
static int one_shot, quiet;
|
2005-04-19 02:16:15 +00:00
|
|
|
static int err;
|
2005-04-18 02:52:54 +00:00
|
|
|
|
|
|
|
static int merge_entry(int pos, const char *path)
|
|
|
|
{
|
|
|
|
int found;
|
2009-06-08 20:34:29 +00:00
|
|
|
const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
|
|
|
|
char hexbuf[4][60];
|
|
|
|
char ownbuf[4][60];
|
2007-06-07 07:04:01 +00:00
|
|
|
|
2005-04-18 02:52:54 +00:00
|
|
|
if (pos >= active_nr)
|
2008-08-31 16:39:19 +00:00
|
|
|
die("git merge-index: %s not in the cache", path);
|
2005-04-18 02:52:54 +00:00
|
|
|
found = 0;
|
|
|
|
do {
|
|
|
|
struct cache_entry *ce = active_cache[pos];
|
|
|
|
int stage = ce_stage(ce);
|
|
|
|
|
|
|
|
if (strcmp(ce->name, path))
|
|
|
|
break;
|
|
|
|
found++;
|
2005-04-18 21:17:58 +00:00
|
|
|
strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
|
2008-01-15 00:03:17 +00:00
|
|
|
sprintf(ownbuf[stage], "%o", ce->ce_mode);
|
2005-04-18 21:17:58 +00:00
|
|
|
arguments[stage] = hexbuf[stage];
|
2005-04-24 03:50:10 +00:00
|
|
|
arguments[stage + 4] = ownbuf[stage];
|
2005-04-18 02:52:54 +00:00
|
|
|
} while (++pos < active_nr);
|
|
|
|
if (!found)
|
2008-08-31 16:39:19 +00:00
|
|
|
die("git merge-index: %s not in the cache", path);
|
2009-06-08 20:34:29 +00:00
|
|
|
|
|
|
|
if (run_command_v_opt(arguments, 0)) {
|
|
|
|
if (one_shot)
|
|
|
|
err++;
|
|
|
|
else {
|
|
|
|
if (!quiet)
|
|
|
|
die("merge program failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-04-18 02:52:54 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_file(const char *path)
|
|
|
|
{
|
|
|
|
int pos = cache_name_pos(path, strlen(path));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it already exists in the cache as stage0, it's
|
|
|
|
* already merged and there is nothing to do.
|
|
|
|
*/
|
|
|
|
if (pos < 0)
|
|
|
|
merge_entry(-pos-1, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < active_nr; i++) {
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
|
|
|
if (!ce_stage(ce))
|
|
|
|
continue;
|
|
|
|
i += merge_entry(i, ce->name)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 15:29:21 +00:00
|
|
|
int cmd_merge_index(int argc, const char **argv, const char *prefix)
|
2005-04-18 02:52:54 +00:00
|
|
|
{
|
|
|
|
int i, force_file = 0;
|
|
|
|
|
2006-06-20 01:25:21 +00:00
|
|
|
/* Without this we cannot rely on waitpid() to tell
|
|
|
|
* what happened to our children.
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
|
2005-04-18 02:52:54 +00:00
|
|
|
if (argc < 3)
|
2009-01-04 18:39:27 +00:00
|
|
|
usage("git merge-index [-o] [-q] <merge-program> (-a | [--] <filename>*)");
|
2005-04-18 02:52:54 +00:00
|
|
|
|
|
|
|
read_cache();
|
|
|
|
|
2005-05-11 02:44:59 +00:00
|
|
|
i = 1;
|
2005-07-29 12:53:38 +00:00
|
|
|
if (!strcmp(argv[i], "-o")) {
|
2005-05-11 02:44:59 +00:00
|
|
|
one_shot = 1;
|
|
|
|
i++;
|
|
|
|
}
|
2005-07-29 12:53:38 +00:00
|
|
|
if (!strcmp(argv[i], "-q")) {
|
|
|
|
quiet = 1;
|
|
|
|
i++;
|
|
|
|
}
|
2005-05-11 02:44:59 +00:00
|
|
|
pgm = argv[i++];
|
|
|
|
for (; i < argc; i++) {
|
2010-01-22 15:29:21 +00:00
|
|
|
const char *arg = argv[i];
|
2005-04-18 02:52:54 +00:00
|
|
|
if (!force_file && *arg == '-') {
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
force_file = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "-a")) {
|
|
|
|
merge_all();
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-31 16:39:19 +00:00
|
|
|
die("git merge-index: unknown option %s", arg);
|
2005-04-18 02:52:54 +00:00
|
|
|
}
|
|
|
|
merge_file(arg);
|
|
|
|
}
|
2005-08-04 22:31:12 +00:00
|
|
|
if (err && !quiet)
|
2005-04-19 02:16:15 +00:00
|
|
|
die("merge program failed");
|
2005-07-29 12:53:38 +00:00
|
|
|
return err;
|
2005-04-18 02:52:54 +00:00
|
|
|
}
|