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