[PATCH] fix bug in read-cache.c which loses files when merging a tree
[git/spearce.git] / merge-cache.c
blob3aa391be0a236ea1c432dadcd14824c2fe2b44ea
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 char *create_temp_file(int stage, unsigned char *sha1)
30 static char template[4][50];
31 char *path = template[stage];
32 void *buf;
33 char type[100];
34 unsigned long size;
35 int fd;
37 buf = read_sha1_file(sha1, type, &size);
38 if (!buf || strcmp(type, "blob"))
39 die("unable to read blob object %s", sha1_to_hex(sha1));
41 strcpy(path, ".merge_file_XXXXXX");
42 fd = mkstemp(path);
43 if (fd < 0)
44 die("unable to create temp-file");
45 if (write(fd, buf, size) != size)
46 die("unable to write temp-file");
47 close(fd);
48 return path;
51 static int merge_entry(int pos, const char *path)
53 int found;
55 if (pos >= active_nr)
56 die("merge-cache: %s not in the cache", path);
57 arguments[0] = pgm;
58 arguments[1] = "";
59 arguments[2] = "";
60 arguments[3] = "";
61 arguments[4] = path;
62 found = 0;
63 do {
64 struct cache_entry *ce = active_cache[pos];
65 int stage = ce_stage(ce);
67 if (strcmp(ce->name, path))
68 break;
69 found++;
70 arguments[stage] = create_temp_file(stage, ce->sha1);
71 } while (++pos < active_nr);
72 if (!found)
73 die("merge-cache: %s not in the cache", path);
74 run_program();
75 return found;
78 static void merge_file(const char *path)
80 int pos = cache_name_pos(path, strlen(path));
83 * If it already exists in the cache as stage0, it's
84 * already merged and there is nothing to do.
86 if (pos < 0)
87 merge_entry(-pos-1, path);
90 static void merge_all(void)
92 int i;
93 for (i = 0; i < active_nr; i++) {
94 struct cache_entry *ce = active_cache[i];
95 if (!ce_stage(ce))
96 continue;
97 i += merge_entry(i, ce->name)-1;
101 int main(int argc, char **argv)
103 int i, force_file = 0;
105 if (argc < 3)
106 usage("merge-cache <merge-program> (-a | <filename>*)");
108 read_cache();
110 pgm = argv[1];
111 for (i = 2; i < argc; i++) {
112 char *arg = argv[i];
113 if (!force_file && *arg == '-') {
114 if (!strcmp(arg, "--")) {
115 force_file = 1;
116 continue;
118 if (!strcmp(arg, "-a")) {
119 merge_all();
120 continue;
122 die("merge-cache: unknown option %s", arg);
124 merge_file(arg);
126 return 0;