Solaris needs inclusion of signal.h for signal()
[git/git.git] / merge-index.c
CommitLineData
75118b13
LT
1#include <sys/types.h>
2#include <sys/wait.h>
e898081d 3#include <signal.h>
75118b13
LT
4
5#include "cache.h"
6
7static const char *pgm = NULL;
e2b6a9d0 8static const char *arguments[8];
bbd14cb0 9static int one_shot, quiet;
8c59926f 10static int err;
75118b13
LT
11
12static void run_program(void)
13{
14 int pid = fork(), status;
15
16 if (pid < 0)
17 die("unable to fork");
18 if (!pid) {
19 execlp(pgm, arguments[0],
20 arguments[1],
21 arguments[2],
22 arguments[3],
23 arguments[4],
e2b6a9d0
JB
24 arguments[5],
25 arguments[6],
26 arguments[7],
75118b13
LT
27 NULL);
28 die("unable to execute '%s'", pgm);
29 }
2a459251 30 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
bbd14cb0 31 if (one_shot) {
2a459251 32 err++;
bbd14cb0 33 } else {
b32e986c 34 if (!quiet)
bbd14cb0
PB
35 die("merge program failed");
36 exit(1);
37 }
2a459251 38 }
75118b13
LT
39}
40
75118b13
LT
41static int merge_entry(int pos, const char *path)
42{
43 int found;
44
45 if (pos >= active_nr)
215a7ad1 46 die("git-merge-index: %s not in the cache", path);
75118b13
LT
47 arguments[0] = pgm;
48 arguments[1] = "";
49 arguments[2] = "";
50 arguments[3] = "";
51 arguments[4] = path;
e2b6a9d0
JB
52 arguments[5] = "";
53 arguments[6] = "";
54 arguments[7] = "";
75118b13
LT
55 found = 0;
56 do {
e3b4be7f 57 static char hexbuf[4][60];
e2b6a9d0 58 static char ownbuf[4][60];
75118b13
LT
59 struct cache_entry *ce = active_cache[pos];
60 int stage = ce_stage(ce);
61
62 if (strcmp(ce->name, path))
63 break;
64 found++;
e3b4be7f 65 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
e2b6a9d0 66 sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
e3b4be7f 67 arguments[stage] = hexbuf[stage];
e2b6a9d0 68 arguments[stage + 4] = ownbuf[stage];
75118b13
LT
69 } while (++pos < active_nr);
70 if (!found)
215a7ad1 71 die("git-merge-index: %s not in the cache", path);
75118b13
LT
72 run_program();
73 return found;
74}
75
76static void merge_file(const char *path)
77{
78 int pos = cache_name_pos(path, strlen(path));
79
80 /*
81 * If it already exists in the cache as stage0, it's
82 * already merged and there is nothing to do.
83 */
84 if (pos < 0)
85 merge_entry(-pos-1, path);
86}
87
88static void merge_all(void)
89{
90 int i;
91 for (i = 0; i < active_nr; i++) {
92 struct cache_entry *ce = active_cache[i];
93 if (!ce_stage(ce))
94 continue;
95 i += merge_entry(i, ce->name)-1;
96 }
97}
98
99int main(int argc, char **argv)
100{
101 int i, force_file = 0;
102
f0b7367c
JH
103 /* Without this we cannot rely on waitpid() to tell
104 * what happened to our children.
105 */
106 signal(SIGCHLD, SIG_DFL);
107
75118b13 108 if (argc < 3)
215a7ad1 109 usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
75118b13 110
53228a5f 111 setup_git_directory();
75118b13
LT
112 read_cache();
113
2a459251 114 i = 1;
bbd14cb0 115 if (!strcmp(argv[i], "-o")) {
2a459251
PB
116 one_shot = 1;
117 i++;
118 }
bbd14cb0
PB
119 if (!strcmp(argv[i], "-q")) {
120 quiet = 1;
121 i++;
122 }
2a459251
PB
123 pgm = argv[i++];
124 for (; i < argc; i++) {
75118b13
LT
125 char *arg = argv[i];
126 if (!force_file && *arg == '-') {
127 if (!strcmp(arg, "--")) {
128 force_file = 1;
129 continue;
130 }
131 if (!strcmp(arg, "-a")) {
132 merge_all();
133 continue;
134 }
215a7ad1 135 die("git-merge-index: unknown option %s", arg);
75118b13
LT
136 }
137 merge_file(arg);
138 }
b32e986c 139 if (err && !quiet)
8c59926f 140 die("merge program failed");
bbd14cb0 141 return err;
75118b13 142}