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