Update README.MinGW.
[git/mingw.git] / merge-index.c
blobbc85e35f4d520dbfa134d19dde22b8406e8ff23d
1 #include "cache.h"
2 #include "spawn-pipe.h"
4 static const char *pgm;
5 static const char *arguments[9]; /* last one is always NULL */
6 static int one_shot, quiet;
7 static int err;
9 static void run_program(void)
11 pid_t pid = spawnvpe_pipe(pgm, arguments, environ, NULL, NULL);
12 int status;
14 if (pid < 0)
15 die("unable to fork");
17 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
18 if (one_shot) {
19 err++;
20 } else {
21 if (!quiet)
22 die("merge program failed");
23 exit(1);
28 static int merge_entry(int pos, const char *path)
30 int found;
32 if (pos >= active_nr)
33 die("git-merge-index: %s not in the cache", path);
34 arguments[1] = "";
35 arguments[2] = "";
36 arguments[3] = "";
37 arguments[4] = path;
38 arguments[5] = "";
39 arguments[6] = "";
40 arguments[7] = "";
41 found = 0;
42 do {
43 static char hexbuf[4][60];
44 static char ownbuf[4][60];
45 struct cache_entry *ce = active_cache[pos];
46 int stage = ce_stage(ce);
48 if (strcmp(ce->name, path))
49 break;
50 found++;
51 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
52 sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
53 arguments[stage] = hexbuf[stage];
54 arguments[stage + 4] = ownbuf[stage];
55 } while (++pos < active_nr);
56 if (!found)
57 die("git-merge-index: %s not in the cache", path);
58 run_program();
59 return found;
62 static void merge_file(const char *path)
64 int pos = cache_name_pos(path, strlen(path));
67 * If it already exists in the cache as stage0, it's
68 * already merged and there is nothing to do.
70 if (pos < 0)
71 merge_entry(-pos-1, path);
74 static void merge_all(void)
76 int i;
77 for (i = 0; i < active_nr; i++) {
78 struct cache_entry *ce = active_cache[i];
79 if (!ce_stage(ce))
80 continue;
81 i += merge_entry(i, ce->name)-1;
85 int main(int argc, char **argv)
87 int i, force_file = 0;
89 /* Without this we cannot rely on waitpid() to tell
90 * what happened to our children.
92 signal(SIGCHLD, SIG_DFL);
94 if (argc < 3)
95 usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
97 setup_git_directory();
98 read_cache();
100 i = 1;
101 if (!strcmp(argv[i], "-o")) {
102 one_shot = 1;
103 i++;
105 if (!strcmp(argv[i], "-q")) {
106 quiet = 1;
107 i++;
109 pgm = argv[i++];
110 for (; i < argc; i++) {
111 char *arg = argv[i];
112 if (!force_file && *arg == '-') {
113 if (!strcmp(arg, "--")) {
114 force_file = 1;
115 continue;
117 if (!strcmp(arg, "-a")) {
118 merge_all();
119 continue;
121 die("git-merge-index: unknown option %s", arg);
123 merge_file(arg);
125 if (err && !quiet)
126 die("merge program failed");
127 return err;