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