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