[PATCH] provide better committer information to commit-tree.c
[git/libgit-gsoc.git] / merge-cache.c
blob68ac4ab2d926005a5a729017c1bcb70a30634bd8
1 #include <sys/types.h>
2 #include <sys/wait.h>
4 #include "cache.h"
6 static const char *pgm = NULL;
7 static const char *arguments[5];
9 static void run_program(void)
11 int pid = fork(), status;
13 if (pid < 0)
14 die("unable to fork");
15 if (!pid) {
16 execlp(pgm, arguments[0],
17 arguments[1],
18 arguments[2],
19 arguments[3],
20 arguments[4],
21 NULL);
22 die("unable to execute '%s'", pgm);
24 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
25 die("merge program failed");
28 static int merge_entry(int pos, const char *path)
30 int found;
32 if (pos >= active_nr)
33 die("merge-cache: %s not in the cache", path);
34 arguments[0] = pgm;
35 arguments[1] = "";
36 arguments[2] = "";
37 arguments[3] = "";
38 arguments[4] = path;
39 found = 0;
40 do {
41 static char hexbuf[4][60];
42 struct cache_entry *ce = active_cache[pos];
43 int stage = ce_stage(ce);
45 if (strcmp(ce->name, path))
46 break;
47 found++;
48 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
49 arguments[stage] = hexbuf[stage];
50 } while (++pos < active_nr);
51 if (!found)
52 die("merge-cache: %s not in the cache", path);
53 run_program();
54 return found;
57 static void merge_file(const char *path)
59 int pos = cache_name_pos(path, strlen(path));
62 * If it already exists in the cache as stage0, it's
63 * already merged and there is nothing to do.
65 if (pos < 0)
66 merge_entry(-pos-1, path);
69 static void merge_all(void)
71 int i;
72 for (i = 0; i < active_nr; i++) {
73 struct cache_entry *ce = active_cache[i];
74 if (!ce_stage(ce))
75 continue;
76 i += merge_entry(i, ce->name)-1;
80 int main(int argc, char **argv)
82 int i, force_file = 0;
84 if (argc < 3)
85 usage("merge-cache <merge-program> (-a | <filename>*)");
87 read_cache();
89 pgm = argv[1];
90 for (i = 2; i < argc; i++) {
91 char *arg = argv[i];
92 if (!force_file && *arg == '-') {
93 if (!strcmp(arg, "--")) {
94 force_file = 1;
95 continue;
97 if (!strcmp(arg, "-a")) {
98 merge_all();
99 continue;
101 die("merge-cache: unknown option %s", arg);
103 merge_file(arg);
105 return 0;