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