]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge-index.c
path.c: clarify trie_find()'s in-code comment
[thirdparty/git.git] / builtin / merge-index.c
CommitLineData
f8adbec9 1#define USE_THE_INDEX_COMPATIBILITY_MACROS
c2e86add 2#include "builtin.h"
b49809c9 3#include "run-command.h"
75118b13 4
96f1e58f 5static const char *pgm;
bbd14cb0 6static int one_shot, quiet;
8c59926f 7static int err;
75118b13 8
75118b13
LT
9static int merge_entry(int pos, const char *path)
10{
11 int found;
0077138c 12 const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
dc01505f 13 char hexbuf[4][GIT_MAX_HEXSZ + 1];
0077138c 14 char ownbuf[4][60];
a6080a0a 15
75118b13 16 if (pos >= active_nr)
7e44c935 17 die("git merge-index: %s not in the cache", path);
75118b13
LT
18 found = 0;
19 do {
9c5e6c80 20 const struct cache_entry *ce = active_cache[pos];
75118b13
LT
21 int stage = ce_stage(ce);
22
23 if (strcmp(ce->name, path))
24 break;
25 found++;
2490574d 26 oid_to_hex_r(hexbuf[stage], &ce->oid);
5096d490 27 xsnprintf(ownbuf[stage], sizeof(ownbuf[stage]), "%o", ce->ce_mode);
e3b4be7f 28 arguments[stage] = hexbuf[stage];
e2b6a9d0 29 arguments[stage + 4] = ownbuf[stage];
75118b13
LT
30 } while (++pos < active_nr);
31 if (!found)
7e44c935 32 die("git merge-index: %s not in the cache", path);
0077138c
JS
33
34 if (run_command_v_opt(arguments, 0)) {
35 if (one_shot)
36 err++;
37 else {
38 if (!quiet)
39 die("merge program failed");
40 exit(1);
41 }
42 }
75118b13
LT
43 return found;
44}
45
fa2364ec 46static void merge_one_path(const char *path)
75118b13
LT
47{
48 int pos = cache_name_pos(path, strlen(path));
49
50 /*
51 * If it already exists in the cache as stage0, it's
52 * already merged and there is nothing to do.
53 */
54 if (pos < 0)
55 merge_entry(-pos-1, path);
56}
57
58static void merge_all(void)
59{
60 int i;
61 for (i = 0; i < active_nr; i++) {
9c5e6c80 62 const struct cache_entry *ce = active_cache[i];
75118b13
LT
63 if (!ce_stage(ce))
64 continue;
65 i += merge_entry(i, ce->name)-1;
66 }
67}
68
0ecace72 69int cmd_merge_index(int argc, const char **argv, const char *prefix)
75118b13
LT
70{
71 int i, force_file = 0;
72
f0b7367c
JH
73 /* Without this we cannot rely on waitpid() to tell
74 * what happened to our children.
75 */
76 signal(SIGCHLD, SIG_DFL);
77
75118b13 78 if (argc < 3)
9c9b4f2f 79 usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
75118b13
LT
80
81 read_cache();
82
2a459251 83 i = 1;
bbd14cb0 84 if (!strcmp(argv[i], "-o")) {
2a459251
PB
85 one_shot = 1;
86 i++;
87 }
bbd14cb0
PB
88 if (!strcmp(argv[i], "-q")) {
89 quiet = 1;
90 i++;
91 }
2a459251
PB
92 pgm = argv[i++];
93 for (; i < argc; i++) {
0ecace72 94 const char *arg = argv[i];
75118b13
LT
95 if (!force_file && *arg == '-') {
96 if (!strcmp(arg, "--")) {
97 force_file = 1;
98 continue;
99 }
100 if (!strcmp(arg, "-a")) {
101 merge_all();
102 continue;
103 }
7e44c935 104 die("git merge-index: unknown option %s", arg);
75118b13 105 }
fa2364ec 106 merge_one_path(arg);
75118b13 107 }
b32e986c 108 if (err && !quiet)
8c59926f 109 die("merge program failed");
bbd14cb0 110 return err;
75118b13 111}