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" |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 3 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 4 | static const char *pgm; |
Junio C Hamano | 843d49a | 2007-03-19 02:48:37 -0700 | [diff] [blame] | 5 | static const char *arguments[9]; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 6 | static int one_shot, quiet; |
Petr Baudis | 8c59926 | 2005-04-19 04:16:15 +0200 | [diff] [blame] | 7 | static int err; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 8 | |
| 9 | static void run_program(void) |
| 10 | { |
Shawn O. Pearce | b49809c | 2007-03-12 19:00:21 -0400 | [diff] [blame] | 11 | struct child_process child; |
| 12 | memset(&child, 0, sizeof(child)); |
| 13 | child.argv = arguments; |
| 14 | if (run_command(&child)) { |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 15 | if (one_shot) { |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 16 | err++; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 17 | } else { |
Petr Baudis | b32e986 | 2005-08-05 00:31:12 +0200 | [diff] [blame] | 18 | if (!quiet) |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 19 | die("merge program failed"); |
| 20 | exit(1); |
| 21 | } |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 22 | } |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 25 | static int merge_entry(int pos, const char *path) |
| 26 | { |
| 27 | int found; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 29 | if (pos >= active_nr) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 30 | die("git-merge-index: %s not in the cache", path); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 31 | arguments[0] = pgm; |
| 32 | arguments[1] = ""; |
| 33 | arguments[2] = ""; |
| 34 | arguments[3] = ""; |
| 35 | arguments[4] = path; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 36 | arguments[5] = ""; |
| 37 | arguments[6] = ""; |
| 38 | arguments[7] = ""; |
Junio C Hamano | 843d49a | 2007-03-19 02:48:37 -0700 | [diff] [blame] | 39 | arguments[8] = NULL; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 40 | found = 0; |
| 41 | do { |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 42 | static char hexbuf[4][60]; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 43 | static char ownbuf[4][60]; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 44 | struct cache_entry *ce = active_cache[pos]; |
| 45 | int stage = ce_stage(ce); |
| 46 | |
| 47 | if (strcmp(ce->name, path)) |
| 48 | break; |
| 49 | found++; |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 50 | strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); |
Junio C Hamano | 308efc1 | 2007-02-25 18:17:15 -0800 | [diff] [blame] | 51 | sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode)); |
Linus Torvalds | e3b4be7 | 2005-04-18 14:17:58 -0700 | [diff] [blame] | 52 | arguments[stage] = hexbuf[stage]; |
James Bottomley | e2b6a9d | 2005-04-23 20:50:10 -0700 | [diff] [blame] | 53 | arguments[stage + 4] = ownbuf[stage]; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 54 | } while (++pos < active_nr); |
| 55 | if (!found) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 56 | die("git-merge-index: %s not in the cache", path); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 57 | run_program(); |
| 58 | return found; |
| 59 | } |
| 60 | |
| 61 | static void merge_file(const char *path) |
| 62 | { |
| 63 | int pos = cache_name_pos(path, strlen(path)); |
| 64 | |
| 65 | /* |
| 66 | * If it already exists in the cache as stage0, it's |
| 67 | * already merged and there is nothing to do. |
| 68 | */ |
| 69 | if (pos < 0) |
| 70 | merge_entry(-pos-1, path); |
| 71 | } |
| 72 | |
| 73 | static void merge_all(void) |
| 74 | { |
| 75 | int i; |
| 76 | for (i = 0; i < active_nr; i++) { |
| 77 | struct cache_entry *ce = active_cache[i]; |
| 78 | if (!ce_stage(ce)) |
| 79 | continue; |
| 80 | i += merge_entry(i, ce->name)-1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char **argv) |
| 85 | { |
| 86 | int i, force_file = 0; |
| 87 | |
Junio C Hamano | f0b7367 | 2006-06-19 18:25:21 -0700 | [diff] [blame] | 88 | /* Without this we cannot rely on waitpid() to tell |
| 89 | * what happened to our children. |
| 90 | */ |
| 91 | signal(SIGCHLD, SIG_DFL); |
| 92 | |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 93 | if (argc < 3) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 94 | usage("git-merge-index [-o] [-q] <merge-program> (-a | <filename>*)"); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 95 | |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 96 | setup_git_directory(); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 97 | read_cache(); |
| 98 | |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 99 | i = 1; |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 100 | if (!strcmp(argv[i], "-o")) { |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 101 | one_shot = 1; |
| 102 | i++; |
| 103 | } |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 104 | if (!strcmp(argv[i], "-q")) { |
| 105 | quiet = 1; |
| 106 | i++; |
| 107 | } |
Petr Baudis | 2a45925 | 2005-05-11 04:44:59 +0200 | [diff] [blame] | 108 | pgm = argv[i++]; |
| 109 | for (; i < argc; i++) { |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 110 | char *arg = argv[i]; |
| 111 | if (!force_file && *arg == '-') { |
| 112 | if (!strcmp(arg, "--")) { |
| 113 | force_file = 1; |
| 114 | continue; |
| 115 | } |
| 116 | if (!strcmp(arg, "-a")) { |
| 117 | merge_all(); |
| 118 | continue; |
| 119 | } |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 120 | die("git-merge-index: unknown option %s", arg); |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 121 | } |
| 122 | merge_file(arg); |
| 123 | } |
Petr Baudis | b32e986 | 2005-08-05 00:31:12 +0200 | [diff] [blame] | 124 | if (err && !quiet) |
Petr Baudis | 8c59926 | 2005-04-19 04:16:15 +0200 | [diff] [blame] | 125 | die("merge program failed"); |
Petr Baudis | bbd14cb | 2005-07-29 14:53:38 +0200 | [diff] [blame] | 126 | return err; |
Linus Torvalds | 75118b1 | 2005-04-17 19:52:54 -0700 | [diff] [blame] | 127 | } |