merge-index.c 2.4 KB
Newer Older
1
#include "cache.h"
2
#include "run-command.h"
3

4
static const char *pgm;
J
Junio C Hamano 已提交
5
static const char *arguments[9];
6
static int one_shot, quiet;
7
static int err;
8 9 10

static void run_program(void)
{
11 12 13 14
	struct child_process child;
	memset(&child, 0, sizeof(child));
	child.argv = arguments;
	if (run_command(&child)) {
15
		if (one_shot) {
16
			err++;
17
		} else {
P
Petr Baudis 已提交
18
			if (!quiet)
19 20 21
				die("merge program failed");
			exit(1);
		}
22
	}
23 24 25 26 27
}

static int merge_entry(int pos, const char *path)
{
	int found;
J
Junio C Hamano 已提交
28

29
	if (pos >= active_nr)
J
Junio C Hamano 已提交
30
		die("git-merge-index: %s not in the cache", path);
31 32 33 34 35
	arguments[0] = pgm;
	arguments[1] = "";
	arguments[2] = "";
	arguments[3] = "";
	arguments[4] = path;
36 37 38
	arguments[5] = "";
	arguments[6] = "";
	arguments[7] = "";
J
Junio C Hamano 已提交
39
	arguments[8] = NULL;
40 41
	found = 0;
	do {
42
		static char hexbuf[4][60];
43
		static char ownbuf[4][60];
44 45 46 47 48 49
		struct cache_entry *ce = active_cache[pos];
		int stage = ce_stage(ce);

		if (strcmp(ce->name, path))
			break;
		found++;
50
		strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
51
		sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode));
52
		arguments[stage] = hexbuf[stage];
53
		arguments[stage + 4] = ownbuf[stage];
54 55
	} while (++pos < active_nr);
	if (!found)
J
Junio C Hamano 已提交
56
		die("git-merge-index: %s not in the cache", path);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	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;

88 89 90 91 92
	/* Without this we cannot rely on waitpid() to tell
	 * what happened to our children.
	 */
	signal(SIGCHLD, SIG_DFL);

93
	if (argc < 3)
J
Junio C Hamano 已提交
94
		usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
95

96
	setup_git_directory();
97 98
	read_cache();

99
	i = 1;
100
	if (!strcmp(argv[i], "-o")) {
101 102 103
		one_shot = 1;
		i++;
	}
104 105 106 107
	if (!strcmp(argv[i], "-q")) {
		quiet = 1;
		i++;
	}
108 109
	pgm = argv[i++];
	for (; i < argc; i++) {
110 111 112 113 114 115 116 117 118 119
		char *arg = argv[i];
		if (!force_file && *arg == '-') {
			if (!strcmp(arg, "--")) {
				force_file = 1;
				continue;
			}
			if (!strcmp(arg, "-a")) {
				merge_all();
				continue;
			}
J
Junio C Hamano 已提交
120
			die("git-merge-index: unknown option %s", arg);
121 122 123
		}
		merge_file(arg);
	}
P
Petr Baudis 已提交
124
	if (err && !quiet)
125
		die("merge program failed");
126
	return err;
127
}