blob: aa9cf23a39ae271a53d1a0c05ac99be0e832b46a [file] [log] [blame]
Linus Torvalds75118b12005-04-17 19:52:54 -07001#include "cache.h"
Shawn O. Pearceb49809c2007-03-12 19:00:21 -04002#include "run-command.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +01003#include "exec_cmd.h"
Linus Torvalds75118b12005-04-17 19:52:54 -07004
David Rientjes96f1e582006-08-15 10:23:48 -07005static const char *pgm;
Junio C Hamano843d49a2007-03-19 02:48:37 -07006static const char *arguments[9];
Petr Baudisbbd14cb2005-07-29 14:53:38 +02007static int one_shot, quiet;
Petr Baudis8c599262005-04-19 04:16:15 +02008static int err;
Linus Torvalds75118b12005-04-17 19:52:54 -07009
10static void run_program(void)
11{
Shawn O. Pearceb49809c2007-03-12 19:00:21 -040012 struct child_process child;
13 memset(&child, 0, sizeof(child));
14 child.argv = arguments;
15 if (run_command(&child)) {
Petr Baudisbbd14cb2005-07-29 14:53:38 +020016 if (one_shot) {
Petr Baudis2a459252005-05-11 04:44:59 +020017 err++;
Petr Baudisbbd14cb2005-07-29 14:53:38 +020018 } else {
Petr Baudisb32e9862005-08-05 00:31:12 +020019 if (!quiet)
Petr Baudisbbd14cb2005-07-29 14:53:38 +020020 die("merge program failed");
21 exit(1);
22 }
Petr Baudis2a459252005-05-11 04:44:59 +020023 }
Linus Torvalds75118b12005-04-17 19:52:54 -070024}
25
Linus Torvalds75118b12005-04-17 19:52:54 -070026static int merge_entry(int pos, const char *path)
27{
28 int found;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070029
Linus Torvalds75118b12005-04-17 19:52:54 -070030 if (pos >= active_nr)
Junio C Hamano7e44c932008-08-31 09:39:19 -070031 die("git merge-index: %s not in the cache", path);
Linus Torvalds75118b12005-04-17 19:52:54 -070032 arguments[0] = pgm;
33 arguments[1] = "";
34 arguments[2] = "";
35 arguments[3] = "";
36 arguments[4] = path;
James Bottomleye2b6a9d2005-04-23 20:50:10 -070037 arguments[5] = "";
38 arguments[6] = "";
39 arguments[7] = "";
Junio C Hamano843d49a2007-03-19 02:48:37 -070040 arguments[8] = NULL;
Linus Torvalds75118b12005-04-17 19:52:54 -070041 found = 0;
42 do {
Linus Torvaldse3b4be72005-04-18 14:17:58 -070043 static char hexbuf[4][60];
James Bottomleye2b6a9d2005-04-23 20:50:10 -070044 static char ownbuf[4][60];
Linus Torvalds75118b12005-04-17 19:52:54 -070045 struct cache_entry *ce = active_cache[pos];
46 int stage = ce_stage(ce);
47
48 if (strcmp(ce->name, path))
49 break;
50 found++;
Linus Torvaldse3b4be72005-04-18 14:17:58 -070051 strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
Linus Torvalds7a51ed62008-01-14 16:03:17 -080052 sprintf(ownbuf[stage], "%o", ce->ce_mode);
Linus Torvaldse3b4be72005-04-18 14:17:58 -070053 arguments[stage] = hexbuf[stage];
James Bottomleye2b6a9d2005-04-23 20:50:10 -070054 arguments[stage + 4] = ownbuf[stage];
Linus Torvalds75118b12005-04-17 19:52:54 -070055 } while (++pos < active_nr);
56 if (!found)
Junio C Hamano7e44c932008-08-31 09:39:19 -070057 die("git merge-index: %s not in the cache", path);
Linus Torvalds75118b12005-04-17 19:52:54 -070058 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
Junio C Hamanof0b73672006-06-19 18:25:21 -070089 /* Without this we cannot rely on waitpid() to tell
90 * what happened to our children.
91 */
92 signal(SIGCHLD, SIG_DFL);
93
Linus Torvalds75118b12005-04-17 19:52:54 -070094 if (argc < 3)
Alexander Potashev34263de2009-01-04 21:39:27 +030095 usage("git merge-index [-o] [-q] <merge-program> (-a | [--] <filename>*)");
Linus Torvalds75118b12005-04-17 19:52:54 -070096
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010097 git_extract_argv0_path(argv[0]);
98
Junio C Hamano53228a52005-11-26 00:50:02 -080099 setup_git_directory();
Linus Torvalds75118b12005-04-17 19:52:54 -0700100 read_cache();
101
Petr Baudis2a459252005-05-11 04:44:59 +0200102 i = 1;
Petr Baudisbbd14cb2005-07-29 14:53:38 +0200103 if (!strcmp(argv[i], "-o")) {
Petr Baudis2a459252005-05-11 04:44:59 +0200104 one_shot = 1;
105 i++;
106 }
Petr Baudisbbd14cb2005-07-29 14:53:38 +0200107 if (!strcmp(argv[i], "-q")) {
108 quiet = 1;
109 i++;
110 }
Petr Baudis2a459252005-05-11 04:44:59 +0200111 pgm = argv[i++];
112 for (; i < argc; i++) {
Linus Torvalds75118b12005-04-17 19:52:54 -0700113 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 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700123 die("git merge-index: unknown option %s", arg);
Linus Torvalds75118b12005-04-17 19:52:54 -0700124 }
125 merge_file(arg);
126 }
Petr Baudisb32e9862005-08-05 00:31:12 +0200127 if (err && !quiet)
Petr Baudis8c599262005-04-19 04:16:15 +0200128 die("merge program failed");
Petr Baudisbbd14cb2005-07-29 14:53:38 +0200129 return err;
Linus Torvalds75118b12005-04-17 19:52:54 -0700130}