6 static const char *pgm
= NULL
;
7 static const char *arguments
[5];
9 static void run_program(void)
11 int pid
= fork(), status
;
14 die("unable to fork");
16 execlp(pgm
, arguments
[0],
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
];
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");
44 die("unable to create temp-file");
45 if (write(fd
, buf
, size
) != size
)
46 die("unable to write temp-file");
51 static int merge_entry(int pos
, const char *path
)
56 die("merge-cache: %s not in the cache", path
);
64 struct cache_entry
*ce
= active_cache
[pos
];
65 int stage
= ce_stage(ce
);
67 if (strcmp(ce
->name
, path
))
70 arguments
[stage
] = create_temp_file(stage
, ce
->sha1
);
71 } while (++pos
< active_nr
);
73 die("merge-cache: %s not in the cache", path
);
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.
87 merge_entry(-pos
-1, path
);
90 static void merge_all(void)
93 for (i
= 0; i
< active_nr
; i
++) {
94 struct cache_entry
*ce
= active_cache
[i
];
97 i
+= merge_entry(i
, ce
->name
)-1;
101 int main(int argc
, char **argv
)
103 int i
, force_file
= 0;
106 usage("merge-cache <merge-program> (-a | <filename>*)");
111 for (i
= 2; i
< argc
; i
++) {
113 if (!force_file
&& *arg
== '-') {
114 if (!strcmp(arg
, "--")) {
118 if (!strcmp(arg
, "-a")) {
122 die("merge-cache: unknown option %s", arg
);