2005-04-18 06:52:54 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2006-06-26 12:26:13 +04:00
|
|
|
#include <signal.h>
|
2005-04-18 06:52:54 +04:00
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
|
2006-08-15 21:23:48 +04:00
|
|
|
static const char *pgm;
|
2005-04-24 07:50:10 +04:00
|
|
|
static const char *arguments[8];
|
2005-07-29 16:53:38 +04:00
|
|
|
static int one_shot, quiet;
|
2005-04-19 06:16:15 +04:00
|
|
|
static int err;
|
2005-04-18 06:52:54 +04:00
|
|
|
|
|
|
|
static void run_program(void)
|
|
|
|
{
|
2006-08-15 21:40:06 +04:00
|
|
|
pid_t pid = fork();
|
|
|
|
int status;
|
2005-04-18 06:52:54 +04:00
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
die("unable to fork");
|
|
|
|
if (!pid) {
|
|
|
|
execlp(pgm, arguments[0],
|
|
|
|
arguments[1],
|
|
|
|
arguments[2],
|
|
|
|
arguments[3],
|
|
|
|
arguments[4],
|
2005-04-24 07:50:10 +04:00
|
|
|
arguments[5],
|
|
|
|
arguments[6],
|
|
|
|
arguments[7],
|
2005-04-18 06:52:54 +04:00
|
|
|
NULL);
|
|
|
|
die("unable to execute '%s'", pgm);
|
|
|
|
}
|
2005-05-11 06:44:59 +04:00
|
|
|
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
|
2005-07-29 16:53:38 +04:00
|
|
|
if (one_shot) {
|
2005-05-11 06:44:59 +04:00
|
|
|
err++;
|
2005-07-29 16:53:38 +04:00
|
|
|
} else {
|
2005-08-05 02:31:12 +04:00
|
|
|
if (!quiet)
|
2005-07-29 16:53:38 +04:00
|
|
|
die("merge program failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-05-11 06:44:59 +04:00
|
|
|
}
|
2005-04-18 06:52:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_entry(int pos, const char *path)
|
|
|
|
{
|
|
|
|
int found;
|
|
|
|
|
|
|
|
if (pos >= active_nr)
|
2005-09-08 04:26:23 +04:00
|
|
|
die("git-merge-index: %s not in the cache", path);
|
2005-04-18 06:52:54 +04:00
|
|
|
arguments[0] = pgm;
|
|
|
|
arguments[1] = "";
|
|
|
|
arguments[2] = "";
|
|
|
|
arguments[3] = "";
|
|
|
|
arguments[4] = path;
|
2005-04-24 07:50:10 +04:00
|
|
|
arguments[5] = "";
|
|
|
|
arguments[6] = "";
|
|
|
|
arguments[7] = "";
|
2005-04-18 06:52:54 +04:00
|
|
|
found = 0;
|
|
|
|
do {
|
2005-04-19 01:17:58 +04:00
|
|
|
static char hexbuf[4][60];
|
2005-04-24 07:50:10 +04:00
|
|
|
static char ownbuf[4][60];
|
2005-04-18 06:52:54 +04:00
|
|
|
struct cache_entry *ce = active_cache[pos];
|
|
|
|
int stage = ce_stage(ce);
|
|
|
|
|
|
|
|
if (strcmp(ce->name, path))
|
|
|
|
break;
|
|
|
|
found++;
|
2005-04-19 01:17:58 +04:00
|
|
|
strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
|
2005-04-24 07:50:10 +04:00
|
|
|
sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
|
2005-04-19 01:17:58 +04:00
|
|
|
arguments[stage] = hexbuf[stage];
|
2005-04-24 07:50:10 +04:00
|
|
|
arguments[stage + 4] = ownbuf[stage];
|
2005-04-18 06:52:54 +04:00
|
|
|
} while (++pos < active_nr);
|
|
|
|
if (!found)
|
2005-09-08 04:26:23 +04:00
|
|
|
die("git-merge-index: %s not in the cache", path);
|
2005-04-18 06:52:54 +04:00
|
|
|
run_program();
|
|
|
|
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 05:25:21 +04:00
|
|
|
/* Without this we cannot rely on waitpid() to tell
|
|
|
|
* what happened to our children.
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
|
2005-04-18 06:52:54 +04:00
|
|
|
if (argc < 3)
|
2005-09-08 04:26:23 +04:00
|
|
|
usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
|
2005-04-18 06:52:54 +04:00
|
|
|
|
2005-11-26 11:50:02 +03:00
|
|
|
setup_git_directory();
|
2005-04-18 06:52:54 +04:00
|
|
|
read_cache();
|
|
|
|
|
2005-05-11 06:44:59 +04:00
|
|
|
i = 1;
|
2005-07-29 16:53:38 +04:00
|
|
|
if (!strcmp(argv[i], "-o")) {
|
2005-05-11 06:44:59 +04:00
|
|
|
one_shot = 1;
|
|
|
|
i++;
|
|
|
|
}
|
2005-07-29 16:53:38 +04:00
|
|
|
if (!strcmp(argv[i], "-q")) {
|
|
|
|
quiet = 1;
|
|
|
|
i++;
|
|
|
|
}
|
2005-05-11 06:44:59 +04:00
|
|
|
pgm = argv[i++];
|
|
|
|
for (; i < argc; i++) {
|
2005-04-18 06:52:54 +04:00
|
|
|
char *arg = argv[i];
|
|
|
|
if (!force_file && *arg == '-') {
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
force_file = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "-a")) {
|
|
|
|
merge_all();
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-08 04:26:23 +04:00
|
|
|
die("git-merge-index: unknown option %s", arg);
|
2005-04-18 06:52:54 +04:00
|
|
|
}
|
|
|
|
merge_file(arg);
|
|
|
|
}
|
2005-08-05 02:31:12 +04:00
|
|
|
if (err && !quiet)
|
2005-04-19 06:16:15 +04:00
|
|
|
die("merge program failed");
|
2005-07-29 16:53:38 +04:00
|
|
|
return err;
|
2005-04-18 06:52:54 +04:00
|
|
|
}
|