Add zlib
[git/pclouds.git] / builtin-merge-index.c
blob91b4d375eee32b237ab9a47541d8c277a7dd46c6
1 #include "cache.h"
2 #include "run-command.h"
4 static const char *pgm;
5 static const char *arguments[9];
6 static int one_shot, quiet;
7 static int err;
9 static void run_program(void)
11 struct child_process child;
12 memset(&child, 0, sizeof(child));
13 child.argv = arguments;
14 if (run_command(&child)) {
15 if (one_shot) {
16 err++;
17 } else {
18 if (!quiet)
19 die("merge program failed");
20 exit(1);
25 static int merge_entry(int pos, const char *path)
27 int found;
29 if (pos >= active_nr)
30 die("git-merge-index: %s not in the cache", path);
31 arguments[0] = pgm;
32 arguments[1] = "";
33 arguments[2] = "";
34 arguments[3] = "";
35 arguments[4] = path;
36 arguments[5] = "";
37 arguments[6] = "";
38 arguments[7] = "";
39 arguments[8] = NULL;
40 found = 0;
41 do {
42 static char hexbuf[4][60];
43 static char ownbuf[4][60];
44 struct cache_entry *ce = active_cache[pos];
45 int stage = ce_stage(ce);
47 if (strcmp(ce->name, path))
48 break;
49 found++;
50 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
51 sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode));
52 arguments[stage] = hexbuf[stage];
53 arguments[stage + 4] = ownbuf[stage];
54 } while (++pos < active_nr);
55 if (!found)
56 die("git-merge-index: %s not in the cache", path);
57 run_program();
58 return found;
61 static void merge_file(const char *path)
63 int pos = cache_name_pos(path, strlen(path));
66 * If it already exists in the cache as stage0, it's
67 * already merged and there is nothing to do.
69 if (pos < 0)
70 merge_entry(-pos-1, path);
73 static void merge_all(void)
75 int i;
76 for (i = 0; i < active_nr; i++) {
77 struct cache_entry *ce = active_cache[i];
78 if (!ce_stage(ce))
79 continue;
80 i += merge_entry(i, ce->name)-1;
84 int cmd_merge_index(int argc, const char **argv, const char *prefix)
86 int i, force_file = 0;
88 /* Without this we cannot rely on waitpid() to tell
89 * what happened to our children.
91 #ifdef SIGCHLD
92 signal(SIGCHLD, SIG_DFL);
93 #endif
95 if (argc < 3)
96 usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)");
98 read_cache();
100 i = 1;
101 if (!strcmp(argv[i], "-o")) {
102 one_shot = 1;
103 i++;
105 if (!strcmp(argv[i], "-q")) {
106 quiet = 1;
107 i++;
109 pgm = argv[i++];
110 for (; i < argc; i++) {
111 char *arg = argv[i];
112 if (!force_file && *arg == '-') {
113 if (!strcmp(arg, "--")) {
114 force_file = 1;
115 continue;
117 if (!strcmp(arg, "-a")) {
118 merge_all();
119 continue;
121 die("git-merge-index: unknown option %s", arg);
123 merge_file(arg);
125 if (err && !quiet)
126 die("merge program failed");
127 return err;