]>
Commit | Line | Data |
---|---|---|
75118b13 | 1 | #include "cache.h" |
b49809c9 | 2 | #include "run-command.h" |
75118b13 | 3 | |
96f1e58f | 4 | static const char *pgm; |
e2b6a9d0 | 5 | static const char *arguments[8]; |
bbd14cb0 | 6 | static int one_shot, quiet; |
8c59926f | 7 | static int err; |
75118b13 LT |
8 | |
9 | static void run_program(void) | |
10 | { | |
b49809c9 SP |
11 | struct child_process child; |
12 | memset(&child, 0, sizeof(child)); | |
13 | child.argv = arguments; | |
14 | if (run_command(&child)) { | |
bbd14cb0 | 15 | if (one_shot) { |
2a459251 | 16 | err++; |
bbd14cb0 | 17 | } else { |
b32e986c | 18 | if (!quiet) |
bbd14cb0 PB |
19 | die("merge program failed"); |
20 | exit(1); | |
21 | } | |
2a459251 | 22 | } |
75118b13 LT |
23 | } |
24 | ||
75118b13 LT |
25 | static int merge_entry(int pos, const char *path) |
26 | { | |
27 | int found; | |
28 | ||
29 | if (pos >= active_nr) | |
215a7ad1 | 30 | die("git-merge-index: %s not in the cache", path); |
75118b13 LT |
31 | arguments[0] = pgm; |
32 | arguments[1] = ""; | |
33 | arguments[2] = ""; | |
34 | arguments[3] = ""; | |
35 | arguments[4] = path; | |
e2b6a9d0 JB |
36 | arguments[5] = ""; |
37 | arguments[6] = ""; | |
38 | arguments[7] = ""; | |
75118b13 LT |
39 | found = 0; |
40 | do { | |
e3b4be7f | 41 | static char hexbuf[4][60]; |
e2b6a9d0 | 42 | static char ownbuf[4][60]; |
75118b13 LT |
43 | struct cache_entry *ce = active_cache[pos]; |
44 | int stage = ce_stage(ce); | |
45 | ||
46 | if (strcmp(ce->name, path)) | |
47 | break; | |
48 | found++; | |
e3b4be7f | 49 | strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); |
308efc10 | 50 | sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode)); |
e3b4be7f | 51 | arguments[stage] = hexbuf[stage]; |
e2b6a9d0 | 52 | arguments[stage + 4] = ownbuf[stage]; |
75118b13 LT |
53 | } while (++pos < active_nr); |
54 | if (!found) | |
215a7ad1 | 55 | die("git-merge-index: %s not in the cache", path); |
75118b13 LT |
56 | run_program(); |
57 | return found; | |
58 | } | |
59 | ||
60 | static void merge_file(const char *path) | |
61 | { | |
62 | int pos = cache_name_pos(path, strlen(path)); | |
63 | ||
64 | /* | |
65 | * If it already exists in the cache as stage0, it's | |
66 | * already merged and there is nothing to do. | |
67 | */ | |
68 | if (pos < 0) | |
69 | merge_entry(-pos-1, path); | |
70 | } | |
71 | ||
72 | static void merge_all(void) | |
73 | { | |
74 | int i; | |
75 | for (i = 0; i < active_nr; i++) { | |
76 | struct cache_entry *ce = active_cache[i]; | |
77 | if (!ce_stage(ce)) | |
78 | continue; | |
79 | i += merge_entry(i, ce->name)-1; | |
80 | } | |
81 | } | |
82 | ||
83 | int main(int argc, char **argv) | |
84 | { | |
85 | int i, force_file = 0; | |
86 | ||
f0b7367c JH |
87 | /* Without this we cannot rely on waitpid() to tell |
88 | * what happened to our children. | |
89 | */ | |
90 | signal(SIGCHLD, SIG_DFL); | |
91 | ||
75118b13 | 92 | if (argc < 3) |
215a7ad1 | 93 | usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)"); |
75118b13 | 94 | |
53228a5f | 95 | setup_git_directory(); |
75118b13 LT |
96 | read_cache(); |
97 | ||
2a459251 | 98 | i = 1; |
bbd14cb0 | 99 | if (!strcmp(argv[i], "-o")) { |
2a459251 PB |
100 | one_shot = 1; |
101 | i++; | |
102 | } | |
bbd14cb0 PB |
103 | if (!strcmp(argv[i], "-q")) { |
104 | quiet = 1; | |
105 | i++; | |
106 | } | |
2a459251 PB |
107 | pgm = argv[i++]; |
108 | for (; i < argc; i++) { | |
75118b13 LT |
109 | char *arg = argv[i]; |
110 | if (!force_file && *arg == '-') { | |
111 | if (!strcmp(arg, "--")) { | |
112 | force_file = 1; | |
113 | continue; | |
114 | } | |
115 | if (!strcmp(arg, "-a")) { | |
116 | merge_all(); | |
117 | continue; | |
118 | } | |
215a7ad1 | 119 | die("git-merge-index: unknown option %s", arg); |
75118b13 LT |
120 | } |
121 | merge_file(arg); | |
122 | } | |
b32e986c | 123 | if (err && !quiet) |
8c59926f | 124 | die("merge program failed"); |
bbd14cb0 | 125 | return err; |
75118b13 | 126 | } |