Interested in helping open source friends on HP-UX?
[git/mjg.git] / builtin / merge-index.c
blob1a1eafa6fdc2e9dc66c98d3a81b7ee50e44727da
1 #include "builtin.h"
2 #include "run-command.h"
4 static const char *pgm;
5 static int one_shot, quiet;
6 static int err;
8 static int merge_entry(int pos, const char *path)
10 int found;
11 const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
12 char hexbuf[4][60];
13 char ownbuf[4][60];
15 if (pos >= active_nr)
16 die("git merge-index: %s not in the cache", path);
17 found = 0;
18 do {
19 const struct cache_entry *ce = active_cache[pos];
20 int stage = ce_stage(ce);
22 if (strcmp(ce->name, path))
23 break;
24 found++;
25 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
26 sprintf(ownbuf[stage], "%o", ce->ce_mode);
27 arguments[stage] = hexbuf[stage];
28 arguments[stage + 4] = ownbuf[stage];
29 } while (++pos < active_nr);
30 if (!found)
31 die("git merge-index: %s not in the cache", path);
33 if (run_command_v_opt(arguments, 0)) {
34 if (one_shot)
35 err++;
36 else {
37 if (!quiet)
38 die("merge program failed");
39 exit(1);
42 return found;
45 static void merge_one_path(const char *path)
47 int pos = cache_name_pos(path, strlen(path));
50 * If it already exists in the cache as stage0, it's
51 * already merged and there is nothing to do.
53 if (pos < 0)
54 merge_entry(-pos-1, path);
57 static void merge_all(void)
59 int i;
60 for (i = 0; i < active_nr; i++) {
61 const struct cache_entry *ce = active_cache[i];
62 if (!ce_stage(ce))
63 continue;
64 i += merge_entry(i, ce->name)-1;
68 int cmd_merge_index(int argc, const char **argv, const char *prefix)
70 int i, force_file = 0;
72 /* Without this we cannot rely on waitpid() to tell
73 * what happened to our children.
75 signal(SIGCHLD, SIG_DFL);
77 if (argc < 3)
78 usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
80 read_cache();
82 i = 1;
83 if (!strcmp(argv[i], "-o")) {
84 one_shot = 1;
85 i++;
87 if (!strcmp(argv[i], "-q")) {
88 quiet = 1;
89 i++;
91 pgm = argv[i++];
92 for (; i < argc; i++) {
93 const char *arg = argv[i];
94 if (!force_file && *arg == '-') {
95 if (!strcmp(arg, "--")) {
96 force_file = 1;
97 continue;
99 if (!strcmp(arg, "-a")) {
100 merge_all();
101 continue;
103 die("git merge-index: unknown option %s", arg);
105 merge_one_path(arg);
107 if (err && !quiet)
108 die("merge program failed");
109 return err;