2005-04-18 02:52:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
|
|
|
|
static const char *pgm = NULL;
|
2005-04-24 03:50:10 +00:00
|
|
|
static const char *arguments[8];
|
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 void run_program(void)
|
|
|
|
{
|
|
|
|
int pid = fork(), status;
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
die("unable to fork");
|
|
|
|
if (!pid) {
|
|
|
|
execlp(pgm, arguments[0],
|
|
|
|
arguments[1],
|
|
|
|
arguments[2],
|
|
|
|
arguments[3],
|
|
|
|
arguments[4],
|
2005-04-24 03:50:10 +00:00
|
|
|
arguments[5],
|
|
|
|
arguments[6],
|
|
|
|
arguments[7],
|
2005-04-18 02:52:54 +00:00
|
|
|
NULL);
|
|
|
|
die("unable to execute '%s'", pgm);
|
|
|
|
}
|
2005-05-11 02:44:59 +00:00
|
|
|
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
|
2005-07-29 12:53:38 +00:00
|
|
|
if (one_shot) {
|
2005-05-11 02:44:59 +00:00
|
|
|
err++;
|
2005-07-29 12:53:38 +00:00
|
|
|
} else {
|
2005-08-04 22:31:12 +00:00
|
|
|
if (!quiet)
|
2005-07-29 12:53:38 +00:00
|
|
|
die("merge program failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-05-11 02:44:59 +00:00
|
|
|
}
|
2005-04-18 02:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int merge_entry(int pos, const char *path)
|
|
|
|
{
|
|
|
|
int found;
|
|
|
|
|
|
|
|
if (pos >= active_nr)
|
2005-05-19 11:17:16 +00:00
|
|
|
die("git-merge-cache: %s not in the cache", path);
|
2005-04-18 02:52:54 +00:00
|
|
|
arguments[0] = pgm;
|
|
|
|
arguments[1] = "";
|
|
|
|
arguments[2] = "";
|
|
|
|
arguments[3] = "";
|
|
|
|
arguments[4] = path;
|
2005-04-24 03:50:10 +00:00
|
|
|
arguments[5] = "";
|
|
|
|
arguments[6] = "";
|
|
|
|
arguments[7] = "";
|
2005-04-18 02:52:54 +00:00
|
|
|
found = 0;
|
|
|
|
do {
|
2005-04-18 21:17:58 +00:00
|
|
|
static char hexbuf[4][60];
|
2005-04-24 03:50:10 +00:00
|
|
|
static char ownbuf[4][60];
|
2005-04-18 02:52:54 +00:00
|
|
|
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));
|
2005-04-24 03:50:10 +00:00
|
|
|
sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
|
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)
|
2005-05-19 11:17:16 +00:00
|
|
|
die("git-merge-cache: %s not in the cache", path);
|
2005-04-18 02:52:54 +00: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;
|
|
|
|
|
|
|
|
if (argc < 3)
|
2005-07-29 12:53:38 +00:00
|
|
|
usage("git-merge-cache [-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++) {
|
2005-04-18 02:52:54 +00:00
|
|
|
char *arg = argv[i];
|
|
|
|
if (!force_file && *arg == '-') {
|
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
force_file = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "-a")) {
|
|
|
|
merge_all();
|
|
|
|
continue;
|
|
|
|
}
|
2005-05-19 11:17:16 +00:00
|
|
|
die("git-merge-cache: 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
|
|
|
}
|