Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Shawn O. Pearce | b49809c | 2007-03-12 19:00:21 -0400 | [diff] [blame] | 2 | #include "run-command.h" |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 3 | #include "exec_cmd.h" |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 4 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 5 | static const char *pgm; |
Junio C Hamano | 843d49a | 2007-03-19 02:48:37 -0700 | [diff] [blame] | 6 | static const char *arguments[9]; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 7 | static int one_shot, quiet; |
Petr Baudis | 8c59926 | 2005-04-19 04:16:15 +0200 | [diff] [blame] | 8 | static int err; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 9 | |
| 10 | static void run_program(void) |
| 11 | { |
Shawn O. Pearce | b49809c | 2007-03-12 19:00:21 -0400 | [diff] [blame] | 12 | struct child_process child; |
| 13 | memset(&child, 0, sizeof(child)); |
| 14 | child.argv = arguments; |
| 15 | if (run_command(&child)) { |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 16 | if (one_shot) { |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 17 | err++; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 18 | } else { |
Petr Baudis | b32e986 | 2005-08-05 00:31:12 +0200 | [diff] [blame] | 19 | if (!quiet) |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 20 | die("merge program failed"); |
| 21 | exit(1); |
| 22 | } |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 23 | } |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 26 | static int merge_entry(int pos, const char *path) |
| 27 | { |
| 28 | int found; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 30 | if (pos >= active_nr) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 31 | die("git merge-index: %s not in the cache", path); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 32 | arguments[0] = pgm; |
| 33 | arguments[1] = ""; |
| 34 | arguments[2] = ""; |
| 35 | arguments[3] = ""; |
| 36 | arguments[4] = path; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 37 | arguments[5] = ""; |
| 38 | arguments[6] = ""; |
| 39 | arguments[7] = ""; |
Junio C Hamano | 843d49a | 2007-03-19 02:48:37 -0700 | [diff] [blame] | 40 | arguments[8] = NULL; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 41 | found = 0; |
| 42 | do { |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 43 | static char hexbuf[4][60]; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 44 | static char ownbuf[4][60]; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 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++; |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 51 | strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 52 | sprintf(ownbuf[stage], "%o", ce->ce_mode); |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 53 | arguments[stage] = hexbuf[stage]; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 54 | arguments[stage + 4] = ownbuf[stage]; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 55 | } while (++pos < active_nr); |
| 56 | if (!found) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 57 | die("git merge-index: %s not in the cache", path); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 58 | run_program(); |
| 59 | return found; |
| 60 | } |
| 61 | |
| 62 | static 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 | |
| 74 | static 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 | |
| 85 | int main(int argc, char **argv) |
| 86 | { |
| 87 | int i, force_file = 0; |
| 88 | |
Junio C Hamano | f0b7367 | 2006-06-19 18:25:21 -0700 | [diff] [blame] | 89 | /* Without this we cannot rely on waitpid() to tell |
| 90 | * what happened to our children. |
| 91 | */ |
| 92 | signal(SIGCHLD, SIG_DFL); |
| 93 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 94 | if (argc < 3) |
Alexander Potashev | 34263de | 2009-01-04 21:39:27 +0300 | [diff] [blame] | 95 | usage("git merge-index [-o] [-q] <merge-program> (-a | [--] <filename>*)"); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 96 | |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 97 | git_extract_argv0_path(argv[0]); |
| 98 | |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 99 | setup_git_directory(); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 100 | read_cache(); |
| 101 | |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 102 | i = 1; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 103 | if (!strcmp(argv[i], "-o")) { |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 104 | one_shot = 1; |
| 105 | i++; |
| 106 | } |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 107 | if (!strcmp(argv[i], "-q")) { |
| 108 | quiet = 1; |
| 109 | i++; |
| 110 | } |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 111 | pgm = argv[i++]; |
| 112 | for (; i < argc; i++) { |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 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 | } |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 123 | die("git merge-index: unknown option %s", arg); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 124 | } |
| 125 | merge_file(arg); |
| 126 | } |
Petr Baudis | b32e986 | 2005-08-05 00:31:12 +0200 | [diff] [blame] | 127 | if (err && !quiet) |
Petr Baudis | 8c59926 | 2005-04-19 04:16:15 +0200 | [diff] [blame] | 128 | die("merge program failed"); |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 129 | return err; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 130 | } |