Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Recursive Merge algorithm stolen from git-merge-recursive.py by |
| 3 | * Fredrik Kuivinen. |
| 4 | * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 |
| 5 | */ |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "cache-tree.h" |
| 8 | #include "commit.h" |
| 9 | #include "blob.h" |
| 10 | #include "tree-walk.h" |
| 11 | #include "diff.h" |
| 12 | #include "diffcore.h" |
| 13 | #include "run-command.h" |
| 14 | #include "tag.h" |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 15 | #include "unpack-trees.h" |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 16 | #include "path-list.h" |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 17 | #include "xdiff-interface.h" |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 18 | #include "interpolate.h" |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 19 | #include "attr.h" |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 20 | |
Junio C Hamano | 68faf68 | 2007-02-15 16:32:45 -0800 | [diff] [blame] | 21 | static int subtree_merge; |
| 22 | |
| 23 | static struct tree *shift_tree_object(struct tree *one, struct tree *two) |
| 24 | { |
| 25 | unsigned char shifted[20]; |
| 26 | |
| 27 | /* |
| 28 | * NEEDSWORK: this limits the recursion depth to hardcoded |
| 29 | * value '2' to avoid excessive overhead. |
| 30 | */ |
| 31 | shift_tree(one->object.sha1, two->object.sha1, shifted, 2); |
| 32 | if (!hashcmp(two->object.sha1, shifted)) |
| 33 | return two; |
| 34 | return lookup_tree(shifted); |
| 35 | } |
| 36 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 37 | /* |
| 38 | * A virtual commit has |
| 39 | * - (const char *)commit->util set to the name, and |
| 40 | * - *(int *)commit->object.sha1 set to the virtual id. |
| 41 | */ |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 42 | |
| 43 | static unsigned commit_list_count(const struct commit_list *l) |
| 44 | { |
| 45 | unsigned c = 0; |
| 46 | for (; l; l = l->next ) |
| 47 | c++; |
| 48 | return c; |
| 49 | } |
| 50 | |
| 51 | static struct commit *make_virtual_commit(struct tree *tree, const char *comment) |
| 52 | { |
| 53 | struct commit *commit = xcalloc(1, sizeof(struct commit)); |
| 54 | static unsigned virtual_id = 1; |
| 55 | commit->tree = tree; |
| 56 | commit->util = (void*)comment; |
| 57 | *(int*)commit->object.sha1 = virtual_id++; |
Johannes Schindelin | c1f3089 | 2006-07-31 12:42:35 +0200 | [diff] [blame] | 58 | /* avoid warnings */ |
| 59 | commit->object.parsed = 1; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 60 | return commit; |
| 61 | } |
| 62 | |
| 63 | /* |
Johannes Schindelin | 3058e93 | 2006-07-27 19:14:17 +0200 | [diff] [blame] | 64 | * Since we use get_tree_entry(), which does not put the read object into |
| 65 | * the object pool, we cannot rely on a == b. |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 66 | */ |
| 67 | static int sha_eq(const unsigned char *a, const unsigned char *b) |
| 68 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 69 | if (!a && !b) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 70 | return 2; |
Junio C Hamano | 87cb004 | 2006-08-23 14:31:20 -0700 | [diff] [blame] | 71 | return a && b && hashcmp(a, b) == 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 74 | /* |
Johannes Schindelin | 3058e93 | 2006-07-27 19:14:17 +0200 | [diff] [blame] | 75 | * Since we want to write the index eventually, we cannot reuse the index |
| 76 | * for these (temporary) data. |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 77 | */ |
| 78 | struct stage_data |
| 79 | { |
| 80 | struct |
| 81 | { |
| 82 | unsigned mode; |
| 83 | unsigned char sha[20]; |
| 84 | } stages[4]; |
| 85 | unsigned processed:1; |
| 86 | }; |
| 87 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 88 | static struct path_list current_file_set = {NULL, 0, 0, 1}; |
| 89 | static struct path_list current_directory_set = {NULL, 0, 0, 1}; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 90 | |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 91 | static int call_depth = 0; |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 92 | static int verbosity = 2; |
Lars Hjemli | df3a02f | 2007-09-25 08:36:38 +0200 | [diff] [blame] | 93 | static int rename_limit = -1; |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 94 | static int buffer_output = 1; |
Pierre Habouzit | 19247e5 | 2007-09-20 10:43:11 +0200 | [diff] [blame] | 95 | static struct strbuf obuf = STRBUF_INIT; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 96 | |
Pierre Habouzit | 19247e5 | 2007-09-20 10:43:11 +0200 | [diff] [blame] | 97 | static int show(int v) |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 98 | { |
| 99 | return (!call_depth && verbosity >= v) || verbosity >= 5; |
| 100 | } |
| 101 | |
Junio C Hamano | b79d18c | 2007-06-13 01:22:51 -0700 | [diff] [blame] | 102 | static void flush_output(void) |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 103 | { |
Pierre Habouzit | 19247e5 | 2007-09-20 10:43:11 +0200 | [diff] [blame] | 104 | if (obuf.len) { |
| 105 | fputs(obuf.buf, stdout); |
| 106 | strbuf_reset(&obuf); |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 107 | } |
Pierre Habouzit | 19247e5 | 2007-09-20 10:43:11 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void output(int v, const char *fmt, ...) |
| 111 | { |
| 112 | int len; |
| 113 | va_list ap; |
| 114 | |
| 115 | if (!show(v)) |
| 116 | return; |
| 117 | |
| 118 | strbuf_grow(&obuf, call_depth * 2 + 2); |
| 119 | memset(obuf.buf + obuf.len, ' ', call_depth * 2); |
| 120 | strbuf_setlen(&obuf, obuf.len + call_depth * 2); |
| 121 | |
| 122 | va_start(ap, fmt); |
| 123 | len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); |
| 124 | va_end(ap); |
| 125 | |
| 126 | if (len < 0) |
| 127 | len = 0; |
| 128 | if (len >= strbuf_avail(&obuf)) { |
| 129 | strbuf_grow(&obuf, len + 2); |
| 130 | va_start(ap, fmt); |
| 131 | len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap); |
| 132 | va_end(ap); |
| 133 | if (len >= strbuf_avail(&obuf)) { |
| 134 | die("this should not happen, your snprintf is broken"); |
| 135 | } |
| 136 | } |
| 137 | strbuf_setlen(&obuf, obuf.len + len); |
| 138 | strbuf_add(&obuf, "\n", 1); |
| 139 | if (!buffer_output) |
| 140 | flush_output(); |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 143 | static void output_commit_title(struct commit *commit) |
| 144 | { |
| 145 | int i; |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 146 | flush_output(); |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 147 | for (i = call_depth; i--;) |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 148 | fputs(" ", stdout); |
| 149 | if (commit->util) |
| 150 | printf("virtual %s\n", (char *)commit->util); |
| 151 | else { |
Junio C Hamano | 9926ba9 | 2006-10-23 00:36:22 -0700 | [diff] [blame] | 152 | printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 153 | if (parse_commit(commit) != 0) |
| 154 | printf("(bad commit)\n"); |
| 155 | else { |
| 156 | const char *s; |
| 157 | int len; |
| 158 | for (s = commit->buffer; *s; s++) |
| 159 | if (*s == '\n' && s[1] == '\n') { |
| 160 | s += 2; |
| 161 | break; |
| 162 | } |
| 163 | for (len = 0; s[len] && '\n' != s[len]; len++) |
| 164 | ; /* do nothing */ |
| 165 | printf("%.*s\n", len, s); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 170 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
| 171 | const char *path, int stage, int refresh, int options) |
| 172 | { |
| 173 | struct cache_entry *ce; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 174 | ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh); |
| 175 | if (!ce) |
Junio C Hamano | 0424138 | 2007-04-01 21:13:27 -0700 | [diff] [blame] | 176 | return error("addinfo_cache failed for path '%s'", path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 177 | return add_cache_entry(ce, options); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * This is a global variable which is used in a number of places but |
| 182 | * only written to in the 'merge' function. |
| 183 | * |
| 184 | * index_only == 1 => Don't leave any non-stage 0 entries in the cache and |
| 185 | * don't update the working directory. |
| 186 | * 0 => Leave unmerged entries in the cache and update |
| 187 | * the working directory. |
| 188 | */ |
| 189 | static int index_only = 0; |
| 190 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 191 | static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) |
| 192 | { |
| 193 | parse_tree(tree); |
| 194 | init_tree_desc(desc, tree->buffer, tree->size); |
| 195 | } |
| 196 | |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 197 | static int git_merge_trees(int index_only, |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 198 | struct tree *common, |
| 199 | struct tree *head, |
| 200 | struct tree *merge) |
| 201 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 202 | int rc; |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 203 | struct tree_desc t[3]; |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 204 | struct unpack_trees_options opts; |
| 205 | |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 206 | memset(&opts, 0, sizeof(opts)); |
| 207 | if (index_only) |
| 208 | opts.index_only = 1; |
| 209 | else |
| 210 | opts.update = 1; |
| 211 | opts.merge = 1; |
| 212 | opts.head_idx = 2; |
| 213 | opts.fn = threeway_merge; |
| 214 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 215 | init_tree_desc_from_tree(t+0, common); |
| 216 | init_tree_desc_from_tree(t+1, head); |
| 217 | init_tree_desc_from_tree(t+2, merge); |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 218 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 219 | rc = unpack_trees(3, t, &opts); |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 220 | cache_tree_free(&active_cache_tree); |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 221 | return rc; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 224 | static int unmerged_index(void) |
| 225 | { |
| 226 | int i; |
| 227 | for (i = 0; i < active_nr; i++) { |
| 228 | struct cache_entry *ce = active_cache[i]; |
| 229 | if (ce_stage(ce)) |
| 230 | return 1; |
| 231 | } |
| 232 | return 0; |
| 233 | } |
| 234 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 235 | static struct tree *git_write_tree(void) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 236 | { |
Johannes Schindelin | 5b982f8 | 2006-08-09 15:04:16 +0200 | [diff] [blame] | 237 | struct tree *result = NULL; |
| 238 | |
Alex Riesen | a97e407 | 2007-03-31 13:49:38 +0200 | [diff] [blame] | 239 | if (unmerged_index()) { |
| 240 | int i; |
| 241 | output(0, "There are unmerged index entries:"); |
| 242 | for (i = 0; i < active_nr; i++) { |
| 243 | struct cache_entry *ce = active_cache[i]; |
| 244 | if (ce_stage(ce)) |
| 245 | output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); |
| 246 | } |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 247 | return NULL; |
Alex Riesen | a97e407 | 2007-03-31 13:49:38 +0200 | [diff] [blame] | 248 | } |
Johannes Schindelin | 5b982f8 | 2006-08-09 15:04:16 +0200 | [diff] [blame] | 249 | |
| 250 | if (!active_cache_tree) |
| 251 | active_cache_tree = cache_tree(); |
| 252 | |
| 253 | if (!cache_tree_fully_valid(active_cache_tree) && |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 254 | cache_tree_update(active_cache_tree, |
| 255 | active_cache, active_nr, 0, 0) < 0) |
Johannes Schindelin | 5b982f8 | 2006-08-09 15:04:16 +0200 | [diff] [blame] | 256 | die("error building trees"); |
| 257 | |
| 258 | result = lookup_tree(active_cache_tree->sha1); |
| 259 | |
Johannes Schindelin | 5b982f8 | 2006-08-09 15:04:16 +0200 | [diff] [blame] | 260 | return result; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 263 | static int save_files_dirs(const unsigned char *sha1, |
| 264 | const char *base, int baselen, const char *path, |
| 265 | unsigned int mode, int stage) |
| 266 | { |
| 267 | int len = strlen(path); |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 268 | char *newpath = xmalloc(baselen + len + 1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 269 | memcpy(newpath, base, baselen); |
| 270 | memcpy(newpath + baselen, path, len); |
| 271 | newpath[baselen + len] = '\0'; |
| 272 | |
| 273 | if (S_ISDIR(mode)) |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 274 | path_list_insert(newpath, ¤t_directory_set); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 275 | else |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 276 | path_list_insert(newpath, ¤t_file_set); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 277 | free(newpath); |
| 278 | |
| 279 | return READ_TREE_RECURSIVE; |
| 280 | } |
| 281 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 282 | static int get_files_dirs(struct tree *tree) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 283 | { |
| 284 | int n; |
Johannes Schindelin | bd66998 | 2006-07-27 19:12:51 +0200 | [diff] [blame] | 285 | if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 286 | return 0; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 287 | n = current_file_set.nr + current_directory_set.nr; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 288 | return n; |
| 289 | } |
| 290 | |
| 291 | /* |
Jim Meyering | 790296f | 2008-01-03 15:18:07 +0100 | [diff] [blame] | 292 | * Returns an index_entry instance which doesn't have to correspond to |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 293 | * a real cache entry in Git's index. |
| 294 | */ |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 295 | static struct stage_data *insert_stage_data(const char *path, |
| 296 | struct tree *o, struct tree *a, struct tree *b, |
| 297 | struct path_list *entries) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 298 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 299 | struct path_list_item *item; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 300 | struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); |
| 301 | get_tree_entry(o->object.sha1, path, |
| 302 | e->stages[1].sha, &e->stages[1].mode); |
| 303 | get_tree_entry(a->object.sha1, path, |
| 304 | e->stages[2].sha, &e->stages[2].mode); |
| 305 | get_tree_entry(b->object.sha1, path, |
| 306 | e->stages[3].sha, &e->stages[3].mode); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 307 | item = path_list_insert(path, entries); |
| 308 | item->util = e; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 309 | return e; |
| 310 | } |
| 311 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 312 | /* |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 313 | * Create a dictionary mapping file names to stage_data objects. The |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 314 | * dictionary contains one entry for every path with a non-zero stage entry. |
| 315 | */ |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 316 | static struct path_list *get_unmerged(void) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 317 | { |
| 318 | struct path_list *unmerged = xcalloc(1, sizeof(struct path_list)); |
| 319 | int i; |
| 320 | |
| 321 | unmerged->strdup_paths = 1; |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 322 | |
Shawn O. Pearce | ad57cbc | 2007-04-20 02:37:18 -0400 | [diff] [blame] | 323 | for (i = 0; i < active_nr; i++) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 324 | struct path_list_item *item; |
| 325 | struct stage_data *e; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 326 | struct cache_entry *ce = active_cache[i]; |
| 327 | if (!ce_stage(ce)) |
| 328 | continue; |
| 329 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 330 | item = path_list_lookup(ce->name, unmerged); |
| 331 | if (!item) { |
| 332 | item = path_list_insert(ce->name, unmerged); |
| 333 | item->util = xcalloc(1, sizeof(struct stage_data)); |
| 334 | } |
| 335 | e = item->util; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 336 | e->stages[ce_stage(ce)].mode = ce->ce_mode; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 337 | hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 340 | return unmerged; |
| 341 | } |
| 342 | |
| 343 | struct rename |
| 344 | { |
| 345 | struct diff_filepair *pair; |
| 346 | struct stage_data *src_entry; |
| 347 | struct stage_data *dst_entry; |
| 348 | unsigned processed:1; |
| 349 | }; |
| 350 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 351 | /* |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 352 | * Get information of all renames which occurred between 'o_tree' and |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 353 | * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and |
| 354 | * 'b_tree') to be able to associate the correct cache entries with |
| 355 | * the rename information. 'tree' is always equal to either a_tree or b_tree. |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 356 | */ |
| 357 | static struct path_list *get_renames(struct tree *tree, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 358 | struct tree *o_tree, |
| 359 | struct tree *a_tree, |
| 360 | struct tree *b_tree, |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 361 | struct path_list *entries) |
| 362 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 363 | int i; |
| 364 | struct path_list *renames; |
| 365 | struct diff_options opts; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 366 | |
| 367 | renames = xcalloc(1, sizeof(struct path_list)); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 368 | diff_setup(&opts); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 369 | DIFF_OPT_SET(&opts, RECURSIVE); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 370 | opts.detect_rename = DIFF_DETECT_RENAME; |
Lars Hjemli | df3a02f | 2007-09-25 08:36:38 +0200 | [diff] [blame] | 371 | opts.rename_limit = rename_limit; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 372 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
| 373 | if (diff_setup_done(&opts) < 0) |
| 374 | die("diff setup failed"); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 375 | diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 376 | diffcore_std(&opts); |
| 377 | for (i = 0; i < diff_queued_diff.nr; ++i) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 378 | struct path_list_item *item; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 379 | struct rename *re; |
| 380 | struct diff_filepair *pair = diff_queued_diff.queue[i]; |
| 381 | if (pair->status != 'R') { |
| 382 | diff_free_filepair(pair); |
| 383 | continue; |
| 384 | } |
| 385 | re = xmalloc(sizeof(*re)); |
| 386 | re->processed = 0; |
| 387 | re->pair = pair; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 388 | item = path_list_lookup(re->pair->one->path, entries); |
| 389 | if (!item) |
| 390 | re->src_entry = insert_stage_data(re->pair->one->path, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 391 | o_tree, a_tree, b_tree, entries); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 392 | else |
| 393 | re->src_entry = item->util; |
| 394 | |
| 395 | item = path_list_lookup(re->pair->two->path, entries); |
| 396 | if (!item) |
| 397 | re->dst_entry = insert_stage_data(re->pair->two->path, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 398 | o_tree, a_tree, b_tree, entries); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 399 | else |
| 400 | re->dst_entry = item->util; |
| 401 | item = path_list_insert(pair->one->path, renames); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 402 | item->util = re; |
| 403 | } |
| 404 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
| 405 | diff_queued_diff.nr = 0; |
| 406 | diff_flush(&opts); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 407 | return renames; |
| 408 | } |
| 409 | |
Junio C Hamano | 9fe0d87 | 2006-10-23 00:46:15 -0700 | [diff] [blame] | 410 | static int update_stages(const char *path, struct diff_filespec *o, |
| 411 | struct diff_filespec *a, struct diff_filespec *b, |
| 412 | int clear) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 413 | { |
| 414 | int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 415 | if (clear) |
| 416 | if (remove_file_from_cache(path)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 417 | return -1; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 418 | if (o) |
| 419 | if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 420 | return -1; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 421 | if (a) |
| 422 | if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 423 | return -1; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 424 | if (b) |
| 425 | if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 426 | return -1; |
| 427 | return 0; |
| 428 | } |
| 429 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 430 | static int remove_path(const char *name) |
| 431 | { |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 432 | int ret; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 433 | char *slash, *dirs; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 434 | |
| 435 | ret = unlink(name); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 436 | if (ret) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 437 | return ret; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 438 | dirs = xstrdup(name); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 439 | while ((slash = strrchr(name, '/'))) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 440 | *slash = '\0'; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 441 | if (rmdir(name) != 0) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | free(dirs); |
| 445 | return ret; |
| 446 | } |
| 447 | |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 448 | static int remove_file(int clean, const char *path, int no_wd) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 449 | { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 450 | int update_cache = index_only || clean; |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 451 | int update_working_directory = !index_only && !no_wd; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 452 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 453 | if (update_cache) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 454 | if (remove_file_from_cache(path)) |
| 455 | return -1; |
| 456 | } |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 457 | if (update_working_directory) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 458 | unlink(path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 459 | if (errno != ENOENT || errno != EISDIR) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 460 | return -1; |
| 461 | remove_path(path); |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static char *unique_path(const char *path, const char *branch) |
| 467 | { |
| 468 | char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 469 | int suffix = 0; |
| 470 | struct stat st; |
Johannes Schindelin | f59aac4 | 2006-07-30 18:35:21 +0200 | [diff] [blame] | 471 | char *p = newpath + strlen(path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 472 | strcpy(newpath, path); |
Johannes Schindelin | f59aac4 | 2006-07-30 18:35:21 +0200 | [diff] [blame] | 473 | *(p++) = '~'; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 474 | strcpy(p, branch); |
| 475 | for (; *p; ++p) |
| 476 | if ('/' == *p) |
| 477 | *p = '_'; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 478 | while (path_list_has_path(¤t_file_set, newpath) || |
| 479 | path_list_has_path(¤t_directory_set, newpath) || |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 480 | lstat(newpath, &st) == 0) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 481 | sprintf(p, "_%d", suffix++); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 482 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 483 | path_list_insert(newpath, ¤t_file_set); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 484 | return newpath; |
| 485 | } |
| 486 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 487 | static int mkdir_p(const char *path, unsigned long mode) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 488 | { |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 489 | /* path points to cache entries, so xstrdup before messing with it */ |
| 490 | char *buf = xstrdup(path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 491 | int result = safe_create_leading_directories(buf); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 492 | free(buf); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 493 | return result; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void flush_buffer(int fd, const char *buf, unsigned long size) |
| 497 | { |
| 498 | while (size > 0) { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 499 | long ret = write_in_full(fd, buf, size); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 500 | if (ret < 0) { |
| 501 | /* Ignore epipe */ |
| 502 | if (errno == EPIPE) |
| 503 | break; |
| 504 | die("merge-recursive: %s", strerror(errno)); |
| 505 | } else if (!ret) { |
| 506 | die("merge-recursive: disk full?"); |
| 507 | } |
| 508 | size -= ret; |
| 509 | buf += ret; |
| 510 | } |
| 511 | } |
| 512 | |
Linus Torvalds | 851c603 | 2007-04-19 22:48:21 -0700 | [diff] [blame] | 513 | static int make_room_for_path(const char *path) |
| 514 | { |
| 515 | int status; |
| 516 | const char *msg = "failed to create path '%s'%s"; |
| 517 | |
| 518 | status = mkdir_p(path, 0777); |
| 519 | if (status) { |
| 520 | if (status == -3) { |
| 521 | /* something else exists */ |
| 522 | error(msg, path, ": perhaps a D/F conflict?"); |
| 523 | return -1; |
| 524 | } |
| 525 | die(msg, path, ""); |
| 526 | } |
| 527 | |
| 528 | /* Successful unlink is good.. */ |
| 529 | if (!unlink(path)) |
| 530 | return 0; |
| 531 | /* .. and so is no existing file */ |
| 532 | if (errno == ENOENT) |
| 533 | return 0; |
| 534 | /* .. but not some other error (who really cares what?) */ |
| 535 | return error(msg, path, ": perhaps a D/F conflict?"); |
| 536 | } |
| 537 | |
Junio C Hamano | 9fe0d87 | 2006-10-23 00:46:15 -0700 | [diff] [blame] | 538 | static void update_file_flags(const unsigned char *sha, |
| 539 | unsigned mode, |
| 540 | const char *path, |
| 541 | int update_cache, |
| 542 | int update_wd) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 543 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 544 | if (index_only) |
| 545 | update_wd = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 546 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 547 | if (update_wd) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 548 | enum object_type type; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 549 | void *buf; |
| 550 | unsigned long size; |
| 551 | |
Finn Arne Gangstad | 20b178d | 2007-12-18 20:50:28 +0100 | [diff] [blame] | 552 | if (S_ISGITLINK(mode)) |
| 553 | die("cannot read object %s '%s': It is a submodule!", |
| 554 | sha1_to_hex(sha), path); |
| 555 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 556 | buf = read_sha1_file(sha, &type, &size); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 557 | if (!buf) |
| 558 | die("cannot read object %s '%s'", sha1_to_hex(sha), path); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 559 | if (type != OBJ_BLOB) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 560 | die("blob expected for %s '%s'", sha1_to_hex(sha), path); |
| 561 | |
Linus Torvalds | 851c603 | 2007-04-19 22:48:21 -0700 | [diff] [blame] | 562 | if (make_room_for_path(path) < 0) { |
| 563 | update_wd = 0; |
| 564 | goto update_index; |
| 565 | } |
Johannes Sixt | 723024d | 2007-03-03 20:32:46 +0100 | [diff] [blame] | 566 | if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 567 | int fd; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 568 | if (mode & 0100) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 569 | mode = 0777; |
| 570 | else |
| 571 | mode = 0666; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 572 | fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); |
| 573 | if (fd < 0) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 574 | die("failed to open %s: %s", path, strerror(errno)); |
| 575 | flush_buffer(fd, buf, size); |
| 576 | close(fd); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 577 | } else if (S_ISLNK(mode)) { |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 578 | char *lnk = xmemdupz(buf, size); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 579 | mkdir_p(path, 0777); |
Junio C Hamano | 17cd29b | 2007-02-25 18:42:07 -0800 | [diff] [blame] | 580 | unlink(path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 581 | symlink(lnk, path); |
Johannes Sixt | 723024d | 2007-03-03 20:32:46 +0100 | [diff] [blame] | 582 | free(lnk); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 583 | } else |
| 584 | die("do not know what to do with %06o %s '%s'", |
| 585 | mode, sha1_to_hex(sha), path); |
| 586 | } |
Junio C Hamano | 4d50895 | 2007-04-07 06:41:13 -0700 | [diff] [blame] | 587 | update_index: |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 588 | if (update_cache) |
| 589 | add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Junio C Hamano | 9fe0d87 | 2006-10-23 00:46:15 -0700 | [diff] [blame] | 592 | static void update_file(int clean, |
| 593 | const unsigned char *sha, |
| 594 | unsigned mode, |
| 595 | const char *path) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 596 | { |
| 597 | update_file_flags(sha, mode, path, index_only || clean, !index_only); |
| 598 | } |
| 599 | |
| 600 | /* Low level file merging, update and removal */ |
| 601 | |
| 602 | struct merge_file_info |
| 603 | { |
| 604 | unsigned char sha[20]; |
| 605 | unsigned mode; |
| 606 | unsigned clean:1, |
| 607 | merge:1; |
| 608 | }; |
| 609 | |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 610 | static void fill_mm(const unsigned char *sha1, mmfile_t *mm) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 611 | { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 612 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 613 | enum object_type type; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 614 | |
Johannes Schindelin | f953831 | 2006-12-13 04:05:39 +0100 | [diff] [blame] | 615 | if (!hashcmp(sha1, null_sha1)) { |
| 616 | mm->ptr = xstrdup(""); |
| 617 | mm->size = 0; |
| 618 | return; |
| 619 | } |
| 620 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 621 | mm->ptr = read_sha1_file(sha1, &type, &size); |
| 622 | if (!mm->ptr || type != OBJ_BLOB) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 623 | die("unable to read blob object %s", sha1_to_hex(sha1)); |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 624 | mm->size = size; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 627 | /* |
| 628 | * Customizable low-level merge drivers support. |
| 629 | */ |
| 630 | |
| 631 | struct ll_merge_driver; |
| 632 | typedef int (*ll_merge_fn)(const struct ll_merge_driver *, |
| 633 | const char *path, |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 634 | mmfile_t *orig, |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 635 | mmfile_t *src1, const char *name1, |
| 636 | mmfile_t *src2, const char *name2, |
| 637 | mmbuffer_t *result); |
| 638 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 639 | struct ll_merge_driver { |
| 640 | const char *name; |
| 641 | const char *description; |
| 642 | ll_merge_fn fn; |
Junio C Hamano | 3086486 | 2007-04-18 12:18:25 -0700 | [diff] [blame] | 643 | const char *recursive; |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 644 | struct ll_merge_driver *next; |
| 645 | char *cmdline; |
| 646 | }; |
| 647 | |
| 648 | /* |
| 649 | * Built-in low-levels |
| 650 | */ |
Junio C Hamano | b798671 | 2007-08-14 15:33:07 -0700 | [diff] [blame] | 651 | static int ll_binary_merge(const struct ll_merge_driver *drv_unused, |
| 652 | const char *path_unused, |
| 653 | mmfile_t *orig, |
| 654 | mmfile_t *src1, const char *name1, |
| 655 | mmfile_t *src2, const char *name2, |
| 656 | mmbuffer_t *result) |
| 657 | { |
| 658 | /* |
| 659 | * The tentative merge result is "ours" for the final round, |
| 660 | * or common ancestor for an internal merge. Still return |
| 661 | * "conflicted merge" status. |
| 662 | */ |
| 663 | mmfile_t *stolen = index_only ? orig : src1; |
| 664 | |
| 665 | result->ptr = stolen->ptr; |
| 666 | result->size = stolen->size; |
| 667 | stolen->ptr = NULL; |
| 668 | return 1; |
| 669 | } |
| 670 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 671 | static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, |
| 672 | const char *path_unused, |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 673 | mmfile_t *orig, |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 674 | mmfile_t *src1, const char *name1, |
| 675 | mmfile_t *src2, const char *name2, |
| 676 | mmbuffer_t *result) |
| 677 | { |
| 678 | xpparam_t xpp; |
| 679 | |
Johannes Schindelin | 9f30855 | 2007-06-05 03:36:49 +0100 | [diff] [blame] | 680 | if (buffer_is_binary(orig->ptr, orig->size) || |
Junio C Hamano | b798671 | 2007-08-14 15:33:07 -0700 | [diff] [blame] | 681 | buffer_is_binary(src1->ptr, src1->size) || |
| 682 | buffer_is_binary(src2->ptr, src2->size)) { |
| 683 | warning("Cannot merge binary files: %s vs. %s\n", |
Johannes Schindelin | 9f30855 | 2007-06-05 03:36:49 +0100 | [diff] [blame] | 684 | name1, name2); |
Junio C Hamano | b798671 | 2007-08-14 15:33:07 -0700 | [diff] [blame] | 685 | return ll_binary_merge(drv_unused, path_unused, |
| 686 | orig, src1, name1, |
| 687 | src2, name2, |
| 688 | result); |
| 689 | } |
Johannes Schindelin | 9f30855 | 2007-06-05 03:36:49 +0100 | [diff] [blame] | 690 | |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 691 | memset(&xpp, 0, sizeof(xpp)); |
| 692 | return xdl_merge(orig, |
| 693 | src1, name1, |
| 694 | src2, name2, |
| 695 | &xpp, XDL_MERGE_ZEALOUS, |
| 696 | result); |
| 697 | } |
| 698 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 699 | static int ll_union_merge(const struct ll_merge_driver *drv_unused, |
| 700 | const char *path_unused, |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 701 | mmfile_t *orig, |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 702 | mmfile_t *src1, const char *name1, |
| 703 | mmfile_t *src2, const char *name2, |
| 704 | mmbuffer_t *result) |
| 705 | { |
| 706 | char *src, *dst; |
| 707 | long size; |
| 708 | const int marker_size = 7; |
| 709 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 710 | int status = ll_xdl_merge(drv_unused, path_unused, |
| 711 | orig, src1, NULL, src2, NULL, result); |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 712 | if (status <= 0) |
| 713 | return status; |
| 714 | size = result->size; |
| 715 | src = dst = result->ptr; |
| 716 | while (size) { |
| 717 | char ch; |
| 718 | if ((marker_size < size) && |
| 719 | (*src == '<' || *src == '=' || *src == '>')) { |
| 720 | int i; |
| 721 | ch = *src; |
| 722 | for (i = 0; i < marker_size; i++) |
| 723 | if (src[i] != ch) |
| 724 | goto not_a_marker; |
| 725 | if (src[marker_size] != '\n') |
| 726 | goto not_a_marker; |
| 727 | src += marker_size + 1; |
| 728 | size -= marker_size + 1; |
| 729 | continue; |
| 730 | } |
| 731 | not_a_marker: |
| 732 | do { |
| 733 | ch = *src++; |
| 734 | *dst++ = ch; |
| 735 | size--; |
| 736 | } while (ch != '\n' && size); |
| 737 | } |
| 738 | result->size = dst - result->ptr; |
| 739 | return 0; |
| 740 | } |
| 741 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 742 | #define LL_BINARY_MERGE 0 |
| 743 | #define LL_TEXT_MERGE 1 |
| 744 | #define LL_UNION_MERGE 2 |
| 745 | static struct ll_merge_driver ll_merge_drv[] = { |
| 746 | { "binary", "built-in binary merge", ll_binary_merge }, |
| 747 | { "text", "built-in 3-way text merge", ll_xdl_merge }, |
| 748 | { "union", "built-in union merge", ll_union_merge }, |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 749 | }; |
| 750 | |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 751 | static void create_temp(mmfile_t *src, char *path) |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 752 | { |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 753 | int fd; |
| 754 | |
| 755 | strcpy(path, ".merge_file_XXXXXX"); |
Luiz Fernando N. Capitulino | 7647b17 | 2007-08-14 16:45:58 -0300 | [diff] [blame] | 756 | fd = xmkstemp(path); |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 757 | if (write_in_full(fd, src->ptr, src->size) != src->size) |
| 758 | die("unable to write temp-file"); |
| 759 | close(fd); |
| 760 | } |
| 761 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 762 | /* |
| 763 | * User defined low-level merge driver support. |
| 764 | */ |
| 765 | static int ll_ext_merge(const struct ll_merge_driver *fn, |
| 766 | const char *path, |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 767 | mmfile_t *orig, |
| 768 | mmfile_t *src1, const char *name1, |
| 769 | mmfile_t *src2, const char *name2, |
| 770 | mmbuffer_t *result) |
| 771 | { |
| 772 | char temp[3][50]; |
| 773 | char cmdbuf[2048]; |
| 774 | struct interp table[] = { |
| 775 | { "%O" }, |
| 776 | { "%A" }, |
| 777 | { "%B" }, |
| 778 | }; |
| 779 | struct child_process child; |
| 780 | const char *args[20]; |
| 781 | int status, fd, i; |
| 782 | struct stat st; |
| 783 | |
Junio C Hamano | 15ba3af | 2007-04-18 19:05:57 -0700 | [diff] [blame] | 784 | if (fn->cmdline == NULL) |
| 785 | die("custom merge driver %s lacks command line.", fn->name); |
| 786 | |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 787 | result->ptr = NULL; |
| 788 | result->size = 0; |
| 789 | create_temp(orig, temp[0]); |
| 790 | create_temp(src1, temp[1]); |
| 791 | create_temp(src2, temp[2]); |
| 792 | |
| 793 | interp_set_entry(table, 0, temp[0]); |
| 794 | interp_set_entry(table, 1, temp[1]); |
| 795 | interp_set_entry(table, 2, temp[2]); |
| 796 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 797 | output(1, "merging %s using %s", path, |
| 798 | fn->description ? fn->description : fn->name); |
| 799 | |
| 800 | interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3); |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 801 | |
| 802 | memset(&child, 0, sizeof(child)); |
| 803 | child.argv = args; |
| 804 | args[0] = "sh"; |
| 805 | args[1] = "-c"; |
| 806 | args[2] = cmdbuf; |
| 807 | args[3] = NULL; |
| 808 | |
| 809 | status = run_command(&child); |
| 810 | if (status < -ERR_RUN_COMMAND_FORK) |
| 811 | ; /* failure in run-command */ |
| 812 | else |
| 813 | status = -status; |
| 814 | fd = open(temp[1], O_RDONLY); |
| 815 | if (fd < 0) |
| 816 | goto bad; |
| 817 | if (fstat(fd, &st)) |
| 818 | goto close_bad; |
| 819 | result->size = st.st_size; |
| 820 | result->ptr = xmalloc(result->size + 1); |
| 821 | if (read_in_full(fd, result->ptr, result->size) != result->size) { |
| 822 | free(result->ptr); |
| 823 | result->ptr = NULL; |
| 824 | result->size = 0; |
| 825 | } |
| 826 | close_bad: |
| 827 | close(fd); |
| 828 | bad: |
| 829 | for (i = 0; i < 3; i++) |
| 830 | unlink(temp[i]); |
| 831 | return status; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * merge.default and merge.driver configuration items |
| 836 | */ |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 837 | static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; |
Junio C Hamano | be89cb2 | 2007-04-18 01:47:21 -0700 | [diff] [blame] | 838 | static const char *default_ll_merge; |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 839 | |
| 840 | static int read_merge_config(const char *var, const char *value) |
| 841 | { |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 842 | struct ll_merge_driver *fn; |
| 843 | const char *ep, *name; |
| 844 | int namelen; |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 845 | |
Junio C Hamano | be89cb2 | 2007-04-18 01:47:21 -0700 | [diff] [blame] | 846 | if (!strcmp(var, "merge.default")) { |
Junio C Hamano | e098368 | 2008-02-11 10:59:17 -0800 | [diff] [blame] | 847 | if (!value) |
| 848 | return config_error_nonbool(var); |
| 849 | default_ll_merge = strdup(value); |
Junio C Hamano | be89cb2 | 2007-04-18 01:47:21 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 853 | /* |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 854 | * We are not interested in anything but "merge.<name>.variable"; |
| 855 | * especially, we do not want to look at variables such as |
| 856 | * "merge.summary", "merge.tool", and "merge.verbosity". |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 857 | */ |
Junio C Hamano | 15ba3af | 2007-04-18 19:05:57 -0700 | [diff] [blame] | 858 | if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 859 | return 0; |
| 860 | |
| 861 | /* |
| 862 | * Find existing one as we might be processing merge.<name>.var2 |
| 863 | * after seeing merge.<name>.var1. |
| 864 | */ |
| 865 | name = var + 6; |
| 866 | namelen = ep - name; |
| 867 | for (fn = ll_user_merge; fn; fn = fn->next) |
| 868 | if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) |
| 869 | break; |
| 870 | if (!fn) { |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 871 | fn = xcalloc(1, sizeof(struct ll_merge_driver)); |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 872 | fn->name = xmemdupz(name, namelen); |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 873 | fn->fn = ll_ext_merge; |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 874 | *ll_user_merge_tail = fn; |
Junio C Hamano | e87b1c9 | 2007-04-21 00:05:31 -0700 | [diff] [blame] | 875 | ll_user_merge_tail = &(fn->next); |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | ep++; |
| 879 | |
| 880 | if (!strcmp("name", ep)) { |
| 881 | if (!value) |
Junio C Hamano | e098368 | 2008-02-11 10:59:17 -0800 | [diff] [blame] | 882 | return config_error_nonbool(var); |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 883 | fn->description = strdup(value); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | if (!strcmp("driver", ep)) { |
| 888 | if (!value) |
Junio C Hamano | e098368 | 2008-02-11 10:59:17 -0800 | [diff] [blame] | 889 | return config_error_nonbool(var); |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 890 | /* |
| 891 | * merge.<name>.driver specifies the command line: |
| 892 | * |
| 893 | * command-line |
| 894 | * |
| 895 | * The command-line will be interpolated with the following |
| 896 | * tokens and is given to the shell: |
| 897 | * |
| 898 | * %O - temporary file name for the merge base. |
| 899 | * %A - temporary file name for our version. |
| 900 | * %B - temporary file name for the other branches' version. |
| 901 | * |
| 902 | * The external merge driver should write the results in the |
| 903 | * file named by %A, and signal that it has done with zero exit |
| 904 | * status. |
| 905 | */ |
| 906 | fn->cmdline = strdup(value); |
| 907 | return 0; |
| 908 | } |
| 909 | |
Junio C Hamano | 3086486 | 2007-04-18 12:18:25 -0700 | [diff] [blame] | 910 | if (!strcmp("recursive", ep)) { |
| 911 | if (!value) |
Junio C Hamano | e098368 | 2008-02-11 10:59:17 -0800 | [diff] [blame] | 912 | return config_error_nonbool(var); |
Junio C Hamano | 3086486 | 2007-04-18 12:18:25 -0700 | [diff] [blame] | 913 | fn->recursive = strdup(value); |
| 914 | return 0; |
| 915 | } |
| 916 | |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | static void initialize_ll_merge(void) |
| 921 | { |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 922 | if (ll_user_merge_tail) |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 923 | return; |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 924 | ll_user_merge_tail = &ll_user_merge; |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 925 | git_config(read_merge_config); |
| 926 | } |
| 927 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 928 | static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 929 | { |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 930 | struct ll_merge_driver *fn; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 931 | const char *name; |
| 932 | int i; |
| 933 | |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 934 | initialize_ll_merge(); |
| 935 | |
| 936 | if (ATTR_TRUE(merge_attr)) |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 937 | return &ll_merge_drv[LL_TEXT_MERGE]; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 938 | else if (ATTR_FALSE(merge_attr)) |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 939 | return &ll_merge_drv[LL_BINARY_MERGE]; |
Junio C Hamano | be89cb2 | 2007-04-18 01:47:21 -0700 | [diff] [blame] | 940 | else if (ATTR_UNSET(merge_attr)) { |
| 941 | if (!default_ll_merge) |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 942 | return &ll_merge_drv[LL_TEXT_MERGE]; |
Junio C Hamano | be89cb2 | 2007-04-18 01:47:21 -0700 | [diff] [blame] | 943 | else |
| 944 | name = default_ll_merge; |
| 945 | } |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 946 | else |
| 947 | name = merge_attr; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 948 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 949 | for (fn = ll_user_merge; fn; fn = fn->next) |
| 950 | if (!strcmp(fn->name, name)) |
| 951 | return fn; |
Junio C Hamano | f3ef6b6 | 2007-04-17 22:51:45 -0700 | [diff] [blame] | 952 | |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 953 | for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) |
| 954 | if (!strcmp(ll_merge_drv[i].name, name)) |
| 955 | return &ll_merge_drv[i]; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 956 | |
| 957 | /* default to the 3-way */ |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 958 | return &ll_merge_drv[LL_TEXT_MERGE]; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 961 | static const char *git_path_check_merge(const char *path) |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 962 | { |
| 963 | static struct git_attr_check attr_merge_check; |
| 964 | |
| 965 | if (!attr_merge_check.attr) |
| 966 | attr_merge_check.attr = git_attr("merge", 5); |
| 967 | |
| 968 | if (git_checkattr(path, 1, &attr_merge_check)) |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 969 | return NULL; |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 970 | return attr_merge_check.value; |
| 971 | } |
| 972 | |
Junio C Hamano | 3e5261a | 2007-04-16 21:58:01 -0700 | [diff] [blame] | 973 | static int ll_merge(mmbuffer_t *result_buf, |
| 974 | struct diff_filespec *o, |
| 975 | struct diff_filespec *a, |
| 976 | struct diff_filespec *b, |
| 977 | const char *branch1, |
| 978 | const char *branch2) |
| 979 | { |
| 980 | mmfile_t orig, src1, src2; |
Junio C Hamano | 3e5261a | 2007-04-16 21:58:01 -0700 | [diff] [blame] | 981 | char *name1, *name2; |
| 982 | int merge_status; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 983 | const char *ll_driver_name; |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 984 | const struct ll_merge_driver *driver; |
Junio C Hamano | 3e5261a | 2007-04-16 21:58:01 -0700 | [diff] [blame] | 985 | |
| 986 | name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); |
| 987 | name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); |
| 988 | |
| 989 | fill_mm(o->sha1, &orig); |
| 990 | fill_mm(a->sha1, &src1); |
| 991 | fill_mm(b->sha1, &src2); |
| 992 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 993 | ll_driver_name = git_path_check_merge(a->path); |
| 994 | driver = find_ll_merge_driver(ll_driver_name); |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 995 | |
Junio C Hamano | d56dbd6 | 2007-04-18 19:22:57 -0700 | [diff] [blame] | 996 | if (index_only && driver->recursive) |
| 997 | driver = find_ll_merge_driver(driver->recursive); |
Junio C Hamano | 153920d | 2007-04-18 11:27:32 -0700 | [diff] [blame] | 998 | merge_status = driver->fn(driver, a->path, |
| 999 | &orig, &src1, name1, &src2, name2, |
| 1000 | result_buf); |
Junio C Hamano | a129d96 | 2007-04-16 22:59:18 -0700 | [diff] [blame] | 1001 | |
Junio C Hamano | 3e5261a | 2007-04-16 21:58:01 -0700 | [diff] [blame] | 1002 | free(name1); |
| 1003 | free(name2); |
| 1004 | free(orig.ptr); |
| 1005 | free(src1.ptr); |
| 1006 | free(src2.ptr); |
| 1007 | return merge_status; |
| 1008 | } |
| 1009 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1010 | static struct merge_file_info merge_file(struct diff_filespec *o, |
| 1011 | struct diff_filespec *a, struct diff_filespec *b, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1012 | const char *branch1, const char *branch2) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1013 | { |
| 1014 | struct merge_file_info result; |
| 1015 | result.merge = 0; |
| 1016 | result.clean = 1; |
| 1017 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1018 | if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1019 | result.clean = 0; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1020 | if (S_ISREG(a->mode)) { |
| 1021 | result.mode = a->mode; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1022 | hashcpy(result.sha, a->sha1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1023 | } else { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1024 | result.mode = b->mode; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1025 | hashcpy(result.sha, b->sha1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1026 | } |
| 1027 | } else { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1028 | if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1029 | result.merge = 1; |
| 1030 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1031 | result.mode = a->mode == o->mode ? b->mode: a->mode; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1032 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1033 | if (sha_eq(a->sha1, o->sha1)) |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1034 | hashcpy(result.sha, b->sha1); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1035 | else if (sha_eq(b->sha1, o->sha1)) |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1036 | hashcpy(result.sha, a->sha1); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1037 | else if (S_ISREG(a->mode)) { |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 1038 | mmbuffer_t result_buf; |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 1039 | int merge_status; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1040 | |
Junio C Hamano | 3e5261a | 2007-04-16 21:58:01 -0700 | [diff] [blame] | 1041 | merge_status = ll_merge(&result_buf, o, a, b, |
| 1042 | branch1, branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1043 | |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 1044 | if ((merge_status < 0) || !result_buf.ptr) |
| 1045 | die("Failed to execute internal merge"); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1046 | |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 1047 | if (write_sha1_file(result_buf.ptr, result_buf.size, |
| 1048 | blob_type, result.sha)) |
| 1049 | die("Unable to add %s to database", |
| 1050 | a->path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1051 | |
Junio C Hamano | c2b4fae | 2006-11-29 12:07:37 -0800 | [diff] [blame] | 1052 | free(result_buf.ptr); |
| 1053 | result.clean = (merge_status == 0); |
Junio C Hamano | ff72af0 | 2007-12-10 11:22:05 -0800 | [diff] [blame] | 1054 | } else if (S_ISGITLINK(a->mode)) { |
| 1055 | result.clean = 0; |
| 1056 | hashcpy(result.sha, a->sha1); |
| 1057 | } else if (S_ISLNK(a->mode)) { |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1058 | hashcpy(result.sha, a->sha1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1059 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1060 | if (!sha_eq(a->sha1, b->sha1)) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1061 | result.clean = 0; |
Junio C Hamano | ff72af0 | 2007-12-10 11:22:05 -0800 | [diff] [blame] | 1062 | } else { |
| 1063 | die("unsupported object type in the tree"); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | return result; |
| 1068 | } |
| 1069 | |
| 1070 | static void conflict_rename_rename(struct rename *ren1, |
| 1071 | const char *branch1, |
| 1072 | struct rename *ren2, |
| 1073 | const char *branch2) |
| 1074 | { |
| 1075 | char *del[2]; |
| 1076 | int delp = 0; |
| 1077 | const char *ren1_dst = ren1->pair->two->path; |
| 1078 | const char *ren2_dst = ren2->pair->two->path; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1079 | const char *dst_name1 = ren1_dst; |
| 1080 | const char *dst_name2 = ren2_dst; |
| 1081 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { |
| 1082 | dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1083 | output(1, "%s is a directory in %s added as %s instead", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1084 | ren1_dst, branch2, dst_name1); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1085 | remove_file(0, ren1_dst, 0); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1086 | } |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1087 | if (path_list_has_path(¤t_directory_set, ren2_dst)) { |
| 1088 | dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1089 | output(1, "%s is a directory in %s added as %s instead", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1090 | ren2_dst, branch1, dst_name2); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1091 | remove_file(0, ren2_dst, 0); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1092 | } |
Alex Riesen | a97e407 | 2007-03-31 13:49:38 +0200 | [diff] [blame] | 1093 | if (index_only) { |
| 1094 | remove_file_from_cache(dst_name1); |
| 1095 | remove_file_from_cache(dst_name2); |
| 1096 | /* |
| 1097 | * Uncomment to leave the conflicting names in the resulting tree |
| 1098 | * |
| 1099 | * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); |
| 1100 | * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); |
| 1101 | */ |
| 1102 | } else { |
| 1103 | update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); |
| 1104 | update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1); |
| 1105 | } |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1106 | while (delp--) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1107 | free(del[delp]); |
| 1108 | } |
| 1109 | |
| 1110 | static void conflict_rename_dir(struct rename *ren1, |
| 1111 | const char *branch1) |
| 1112 | { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1113 | char *new_path = unique_path(ren1->pair->two->path, branch1); |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1114 | output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1115 | remove_file(0, ren1->pair->two->path, 0); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1116 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); |
| 1117 | free(new_path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static void conflict_rename_rename_2(struct rename *ren1, |
| 1121 | const char *branch1, |
| 1122 | struct rename *ren2, |
| 1123 | const char *branch2) |
| 1124 | { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1125 | char *new_path1 = unique_path(ren1->pair->two->path, branch1); |
| 1126 | char *new_path2 = unique_path(ren2->pair->two->path, branch2); |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1127 | output(1, "Renamed %s to %s and %s to %s instead", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1128 | ren1->pair->one->path, new_path1, |
| 1129 | ren2->pair->one->path, new_path2); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1130 | remove_file(0, ren1->pair->two->path, 0); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1131 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); |
| 1132 | update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); |
| 1133 | free(new_path2); |
| 1134 | free(new_path1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1137 | static int process_renames(struct path_list *a_renames, |
| 1138 | struct path_list *b_renames, |
| 1139 | const char *a_branch, |
| 1140 | const char *b_branch) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1141 | { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1142 | int clean_merge = 1, i, j; |
| 1143 | struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1144 | const struct rename *sre; |
| 1145 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1146 | for (i = 0; i < a_renames->nr; i++) { |
| 1147 | sre = a_renames->items[i].util; |
| 1148 | path_list_insert(sre->pair->two->path, &a_by_dst)->util |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1149 | = sre->dst_entry; |
| 1150 | } |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1151 | for (i = 0; i < b_renames->nr; i++) { |
| 1152 | sre = b_renames->items[i].util; |
| 1153 | path_list_insert(sre->pair->two->path, &b_by_dst)->util |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1154 | = sre->dst_entry; |
| 1155 | } |
| 1156 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1157 | for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1158 | int compare; |
| 1159 | char *src; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1160 | struct path_list *renames1, *renames2, *renames2Dst; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1161 | struct rename *ren1 = NULL, *ren2 = NULL; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1162 | const char *branch1, *branch2; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1163 | const char *ren1_src, *ren1_dst; |
| 1164 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1165 | if (i >= a_renames->nr) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1166 | compare = 1; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1167 | ren2 = b_renames->items[j++].util; |
| 1168 | } else if (j >= b_renames->nr) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1169 | compare = -1; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1170 | ren1 = a_renames->items[i++].util; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1171 | } else { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1172 | compare = strcmp(a_renames->items[i].path, |
| 1173 | b_renames->items[j].path); |
Johannes Schindelin | 3d234d0 | 2006-08-04 18:21:41 +0200 | [diff] [blame] | 1174 | if (compare <= 0) |
| 1175 | ren1 = a_renames->items[i++].util; |
| 1176 | if (compare >= 0) |
| 1177 | ren2 = b_renames->items[j++].util; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1178 | } |
| 1179 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1180 | /* TODO: refactor, so that 1/2 are not needed */ |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1181 | if (ren1) { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1182 | renames1 = a_renames; |
| 1183 | renames2 = b_renames; |
| 1184 | renames2Dst = &b_by_dst; |
| 1185 | branch1 = a_branch; |
| 1186 | branch2 = b_branch; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1187 | } else { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1188 | struct rename *tmp; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1189 | renames1 = b_renames; |
| 1190 | renames2 = a_renames; |
| 1191 | renames2Dst = &a_by_dst; |
| 1192 | branch1 = b_branch; |
| 1193 | branch2 = a_branch; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1194 | tmp = ren2; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1195 | ren2 = ren1; |
| 1196 | ren1 = tmp; |
| 1197 | } |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1198 | src = ren1->pair->one->path; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1199 | |
| 1200 | ren1->dst_entry->processed = 1; |
| 1201 | ren1->src_entry->processed = 1; |
| 1202 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1203 | if (ren1->processed) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1204 | continue; |
| 1205 | ren1->processed = 1; |
| 1206 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1207 | ren1_src = ren1->pair->one->path; |
| 1208 | ren1_dst = ren1->pair->two->path; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1209 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1210 | if (ren2) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1211 | const char *ren2_src = ren2->pair->one->path; |
| 1212 | const char *ren2_dst = ren2->pair->two->path; |
| 1213 | /* Renamed in 1 and renamed in 2 */ |
| 1214 | if (strcmp(ren1_src, ren2_src) != 0) |
| 1215 | die("ren1.src != ren2.src"); |
| 1216 | ren2->dst_entry->processed = 1; |
| 1217 | ren2->processed = 1; |
| 1218 | if (strcmp(ren1_dst, ren2_dst) != 0) { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1219 | clean_merge = 0; |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1220 | output(1, "CONFLICT (rename/rename): " |
Alex Riesen | a97e407 | 2007-03-31 13:49:38 +0200 | [diff] [blame] | 1221 | "Rename \"%s\"->\"%s\" in branch \"%s\" " |
| 1222 | "rename \"%s\"->\"%s\" in \"%s\"%s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1223 | src, ren1_dst, branch1, |
Alex Riesen | a97e407 | 2007-03-31 13:49:38 +0200 | [diff] [blame] | 1224 | src, ren2_dst, branch2, |
| 1225 | index_only ? " (left unresolved)": ""); |
| 1226 | if (index_only) { |
| 1227 | remove_file_from_cache(src); |
| 1228 | update_file(0, ren1->pair->one->sha1, |
| 1229 | ren1->pair->one->mode, src); |
| 1230 | } |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1231 | conflict_rename_rename(ren1, branch1, ren2, branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1232 | } else { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1233 | struct merge_file_info mfi; |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1234 | remove_file(1, ren1_src, 1); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1235 | mfi = merge_file(ren1->pair->one, |
| 1236 | ren1->pair->two, |
| 1237 | ren2->pair->two, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1238 | branch1, |
| 1239 | branch2); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1240 | if (mfi.merge || !mfi.clean) |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1241 | output(1, "Renamed %s->%s", src, ren1_dst); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1242 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1243 | if (mfi.merge) |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1244 | output(2, "Auto-merged %s", ren1_dst); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1245 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1246 | if (!mfi.clean) { |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1247 | output(1, "CONFLICT (content): merge conflict in %s", |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1248 | ren1_dst); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1249 | clean_merge = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1250 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1251 | if (!index_only) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1252 | update_stages(ren1_dst, |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1253 | ren1->pair->one, |
| 1254 | ren1->pair->two, |
| 1255 | ren2->pair->two, |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1256 | 1 /* clear */); |
| 1257 | } |
| 1258 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); |
| 1259 | } |
| 1260 | } else { |
| 1261 | /* Renamed in 1, maybe changed in 2 */ |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1262 | struct path_list_item *item; |
| 1263 | /* we only use sha1 and mode of these */ |
| 1264 | struct diff_filespec src_other, dst_other; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1265 | int try_merge, stage = a_renames == renames1 ? 3: 2; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1266 | |
Shawn O. Pearce | 183d797 | 2007-02-04 00:45:54 -0500 | [diff] [blame] | 1267 | remove_file(1, ren1_src, index_only || stage == 3); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1268 | |
Junio C Hamano | 87cb004 | 2006-08-23 14:31:20 -0700 | [diff] [blame] | 1269 | hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1270 | src_other.mode = ren1->src_entry->stages[stage].mode; |
Junio C Hamano | 87cb004 | 2006-08-23 14:31:20 -0700 | [diff] [blame] | 1271 | hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1272 | dst_other.mode = ren1->dst_entry->stages[stage].mode; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1273 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1274 | try_merge = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1275 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1276 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { |
| 1277 | clean_merge = 0; |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1278 | output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s " |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1279 | " directory %s added in %s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1280 | ren1_src, ren1_dst, branch1, |
| 1281 | ren1_dst, branch2); |
| 1282 | conflict_rename_dir(ren1, branch1); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1283 | } else if (sha_eq(src_other.sha1, null_sha1)) { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1284 | clean_merge = 0; |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1285 | output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s " |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1286 | "and deleted in %s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1287 | ren1_src, ren1_dst, branch1, |
| 1288 | branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1289 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1290 | } else if (!sha_eq(dst_other.sha1, null_sha1)) { |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1291 | const char *new_path; |
| 1292 | clean_merge = 0; |
| 1293 | try_merge = 1; |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1294 | output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. " |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1295 | "%s added in %s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1296 | ren1_src, ren1_dst, branch1, |
| 1297 | ren1_dst, branch2); |
| 1298 | new_path = unique_path(ren1_dst, branch2); |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1299 | output(1, "Added as %s instead", new_path); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1300 | update_file(0, dst_other.sha1, dst_other.mode, new_path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1301 | } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) { |
| 1302 | ren2 = item->util; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1303 | clean_merge = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1304 | ren2->processed = 1; |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1305 | output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. " |
| 1306 | "Renamed %s->%s in %s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1307 | ren1_src, ren1_dst, branch1, |
| 1308 | ren2->pair->one->path, ren2->pair->two->path, branch2); |
| 1309 | conflict_rename_rename_2(ren1, branch1, ren2, branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1310 | } else |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1311 | try_merge = 1; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1312 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1313 | if (try_merge) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1314 | struct diff_filespec *o, *a, *b; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1315 | struct merge_file_info mfi; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1316 | src_other.path = (char *)ren1_src; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1317 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1318 | o = ren1->pair->one; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1319 | if (a_renames == renames1) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1320 | a = ren1->pair->two; |
| 1321 | b = &src_other; |
| 1322 | } else { |
| 1323 | b = ren1->pair->two; |
| 1324 | a = &src_other; |
| 1325 | } |
| 1326 | mfi = merge_file(o, a, b, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1327 | a_branch, b_branch); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1328 | |
Alex Riesen | 0d5e6c9 | 2007-04-26 21:13:49 +0200 | [diff] [blame] | 1329 | if (mfi.clean && |
Alex Riesen | c135ee8 | 2007-04-25 22:06:59 +0200 | [diff] [blame] | 1330 | sha_eq(mfi.sha, ren1->pair->two->sha1) && |
| 1331 | mfi.mode == ren1->pair->two->mode) |
Alex Riesen | 8a35981 | 2007-04-25 22:07:45 +0200 | [diff] [blame] | 1332 | /* |
| 1333 | * This messaged is part of |
| 1334 | * t6022 test. If you change |
| 1335 | * it update the test too. |
| 1336 | */ |
Alex Riesen | c135ee8 | 2007-04-25 22:06:59 +0200 | [diff] [blame] | 1337 | output(3, "Skipped %s (merged same as existing)", ren1_dst); |
| 1338 | else { |
| 1339 | if (mfi.merge || !mfi.clean) |
| 1340 | output(1, "Renamed %s => %s", ren1_src, ren1_dst); |
| 1341 | if (mfi.merge) |
| 1342 | output(2, "Auto-merged %s", ren1_dst); |
| 1343 | if (!mfi.clean) { |
| 1344 | output(1, "CONFLICT (rename/modify): Merge conflict in %s", |
| 1345 | ren1_dst); |
| 1346 | clean_merge = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1347 | |
Alex Riesen | c135ee8 | 2007-04-25 22:06:59 +0200 | [diff] [blame] | 1348 | if (!index_only) |
| 1349 | update_stages(ren1_dst, |
| 1350 | o, a, b, 1); |
| 1351 | } |
| 1352 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1353 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | } |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1357 | path_list_clear(&a_by_dst, 0); |
| 1358 | path_list_clear(&b_by_dst, 0); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1359 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1360 | return clean_merge; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Junio C Hamano | ac7f0f4 | 2007-04-07 05:52:57 -0700 | [diff] [blame] | 1363 | static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1364 | { |
Junio C Hamano | ac7f0f4 | 2007-04-07 05:52:57 -0700 | [diff] [blame] | 1365 | return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | /* Per entry merge function */ |
| 1369 | static int process_entry(const char *path, struct stage_data *entry, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1370 | const char *branch1, |
| 1371 | const char *branch2) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1372 | { |
| 1373 | /* |
| 1374 | printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); |
| 1375 | print_index_entry("\tpath: ", entry); |
| 1376 | */ |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1377 | int clean_merge = 1; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1378 | unsigned o_mode = entry->stages[1].mode; |
| 1379 | unsigned a_mode = entry->stages[2].mode; |
| 1380 | unsigned b_mode = entry->stages[3].mode; |
Junio C Hamano | ac7f0f4 | 2007-04-07 05:52:57 -0700 | [diff] [blame] | 1381 | unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode); |
| 1382 | unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode); |
| 1383 | unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1384 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1385 | if (o_sha && (!a_sha || !b_sha)) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1386 | /* Case A: Deleted in one */ |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1387 | if ((!a_sha && !b_sha) || |
| 1388 | (sha_eq(a_sha, o_sha) && !b_sha) || |
| 1389 | (!a_sha && sha_eq(b_sha, o_sha))) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1390 | /* Deleted in both or deleted in one and |
| 1391 | * unchanged in the other */ |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1392 | if (a_sha) |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1393 | output(2, "Removed %s", path); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1394 | /* do not touch working file if it did not exist */ |
| 1395 | remove_file(1, path, !a_sha); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1396 | } else { |
| 1397 | /* Deleted in one and changed in the other */ |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1398 | clean_merge = 0; |
| 1399 | if (!a_sha) { |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1400 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1401 | "and modified in %s. Version %s of %s left in tree.", |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1402 | path, branch1, |
| 1403 | branch2, branch2, path); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1404 | update_file(0, b_sha, b_mode, path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1405 | } else { |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1406 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1407 | "and modified in %s. Version %s of %s left in tree.", |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1408 | path, branch2, |
| 1409 | branch1, branch1, path); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1410 | update_file(0, a_sha, a_mode, path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1414 | } else if ((!o_sha && a_sha && !b_sha) || |
| 1415 | (!o_sha && !a_sha && b_sha)) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1416 | /* Case B: Added in one. */ |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1417 | const char *add_branch; |
| 1418 | const char *other_branch; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1419 | unsigned mode; |
| 1420 | const unsigned char *sha; |
| 1421 | const char *conf; |
| 1422 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1423 | if (a_sha) { |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1424 | add_branch = branch1; |
| 1425 | other_branch = branch2; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1426 | mode = a_mode; |
| 1427 | sha = a_sha; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1428 | conf = "file/directory"; |
| 1429 | } else { |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1430 | add_branch = branch2; |
| 1431 | other_branch = branch1; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1432 | mode = b_mode; |
| 1433 | sha = b_sha; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1434 | conf = "directory/file"; |
| 1435 | } |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1436 | if (path_list_has_path(¤t_directory_set, path)) { |
| 1437 | const char *new_path = unique_path(path, add_branch); |
| 1438 | clean_merge = 0; |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1439 | output(1, "CONFLICT (%s): There is a directory with name %s in %s. " |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1440 | "Added %s as %s", |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1441 | conf, path, other_branch, path, new_path); |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1442 | remove_file(0, path, 0); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1443 | update_file(0, sha, mode, new_path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1444 | } else { |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1445 | output(2, "Added %s", path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1446 | update_file(1, sha, mode, path); |
| 1447 | } |
Johannes Schindelin | f953831 | 2006-12-13 04:05:39 +0100 | [diff] [blame] | 1448 | } else if (a_sha && b_sha) { |
| 1449 | /* Case C: Added in both (check for same permissions) and */ |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1450 | /* case D: Modified in both, but differently. */ |
Johannes Schindelin | f953831 | 2006-12-13 04:05:39 +0100 | [diff] [blame] | 1451 | const char *reason = "content"; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1452 | struct merge_file_info mfi; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1453 | struct diff_filespec o, a, b; |
| 1454 | |
Johannes Schindelin | f953831 | 2006-12-13 04:05:39 +0100 | [diff] [blame] | 1455 | if (!o_sha) { |
| 1456 | reason = "add/add"; |
| 1457 | o_sha = (unsigned char *)null_sha1; |
| 1458 | } |
Shawn O. Pearce | 89f40be | 2007-01-14 03:11:28 -0500 | [diff] [blame] | 1459 | output(2, "Auto-merged %s", path); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1460 | o.path = a.path = b.path = (char *)path; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1461 | hashcpy(o.sha1, o_sha); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1462 | o.mode = o_mode; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1463 | hashcpy(a.sha1, a_sha); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1464 | a.mode = a_mode; |
Shawn Pearce | 8da7149 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1465 | hashcpy(b.sha1, b_sha); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1466 | b.mode = b_mode; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1467 | |
| 1468 | mfi = merge_file(&o, &a, &b, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1469 | branch1, branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1470 | |
Finn Arne Gangstad | 20b178d | 2007-12-18 20:50:28 +0100 | [diff] [blame] | 1471 | clean_merge = mfi.clean; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1472 | if (mfi.clean) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1473 | update_file(1, mfi.sha, mfi.mode, path); |
Finn Arne Gangstad | 20b178d | 2007-12-18 20:50:28 +0100 | [diff] [blame] | 1474 | else if (S_ISGITLINK(mfi.mode)) |
| 1475 | output(1, "CONFLICT (submodule): Merge conflict in %s " |
| 1476 | "- needs %s", path, sha1_to_hex(b.sha1)); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1477 | else { |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1478 | output(1, "CONFLICT (%s): Merge conflict in %s", |
Johannes Schindelin | f953831 | 2006-12-13 04:05:39 +0100 | [diff] [blame] | 1479 | reason, path); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1480 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1481 | if (index_only) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1482 | update_file(0, mfi.sha, mfi.mode, path); |
| 1483 | else |
| 1484 | update_file_flags(mfi.sha, mfi.mode, path, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1485 | 0 /* update_cache */, 1 /* update_working_directory */); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1486 | } |
Junio C Hamano | ac7f0f4 | 2007-04-07 05:52:57 -0700 | [diff] [blame] | 1487 | } else if (!o_sha && !a_sha && !b_sha) { |
| 1488 | /* |
| 1489 | * this entry was deleted altogether. a_mode == 0 means |
| 1490 | * we had that path and want to actively remove it. |
| 1491 | */ |
| 1492 | remove_file(1, path, !a_mode); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1493 | } else |
| 1494 | die("Fatal merge failure, shouldn't happen."); |
| 1495 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1496 | return clean_merge; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1499 | static int merge_trees(struct tree *head, |
| 1500 | struct tree *merge, |
| 1501 | struct tree *common, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1502 | const char *branch1, |
| 1503 | const char *branch2, |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1504 | struct tree **result) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1505 | { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1506 | int code, clean; |
Junio C Hamano | 68faf68 | 2007-02-15 16:32:45 -0800 | [diff] [blame] | 1507 | |
| 1508 | if (subtree_merge) { |
| 1509 | merge = shift_tree_object(head, merge); |
| 1510 | common = shift_tree_object(head, common); |
| 1511 | } |
| 1512 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1513 | if (sha_eq(common->object.sha1, merge->object.sha1)) { |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1514 | output(0, "Already uptodate!"); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1515 | *result = head; |
| 1516 | return 1; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1517 | } |
| 1518 | |
Johannes Schindelin | 7a85b84 | 2006-07-30 20:27:10 +0200 | [diff] [blame] | 1519 | code = git_merge_trees(index_only, common, head, merge); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1520 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1521 | if (code != 0) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1522 | die("merging of trees %s and %s failed", |
| 1523 | sha1_to_hex(head->object.sha1), |
| 1524 | sha1_to_hex(merge->object.sha1)); |
| 1525 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1526 | if (unmerged_index()) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1527 | struct path_list *entries, *re_head, *re_merge; |
| 1528 | int i; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1529 | path_list_clear(¤t_file_set, 1); |
| 1530 | path_list_clear(¤t_directory_set, 1); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1531 | get_files_dirs(head); |
| 1532 | get_files_dirs(merge); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1533 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1534 | entries = get_unmerged(); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1535 | re_head = get_renames(head, common, head, merge, entries); |
| 1536 | re_merge = get_renames(merge, common, head, merge, entries); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1537 | clean = process_renames(re_head, re_merge, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1538 | branch1, branch2); |
Shawn O. Pearce | ad57cbc | 2007-04-20 02:37:18 -0400 | [diff] [blame] | 1539 | for (i = 0; i < entries->nr; i++) { |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1540 | const char *path = entries->items[i].path; |
| 1541 | struct stage_data *e = entries->items[i].util; |
Shawn O. Pearce | 3f6ee2d | 2007-01-14 00:28:58 -0500 | [diff] [blame] | 1542 | if (!e->processed |
| 1543 | && !process_entry(path, e, branch1, branch2)) |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1544 | clean = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1547 | path_list_clear(re_merge, 0); |
| 1548 | path_list_clear(re_head, 0); |
| 1549 | path_list_clear(entries, 1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1550 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1551 | } |
Junio C Hamano | 1cf716a | 2007-01-12 12:05:58 -0800 | [diff] [blame] | 1552 | else |
| 1553 | clean = 1; |
| 1554 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1555 | if (index_only) |
| 1556 | *result = git_write_tree(); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1557 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1558 | return clean; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Johannes Schindelin | 8918b0c | 2006-08-09 22:30:58 +0200 | [diff] [blame] | 1561 | static struct commit_list *reverse_commit_list(struct commit_list *list) |
| 1562 | { |
| 1563 | struct commit_list *next = NULL, *current, *backup; |
| 1564 | for (current = list; current; current = backup) { |
| 1565 | backup = current->next; |
| 1566 | current->next = next; |
| 1567 | next = current; |
| 1568 | } |
| 1569 | return next; |
| 1570 | } |
| 1571 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1572 | /* |
| 1573 | * Merge the commits h1 and h2, return the resulting virtual |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 1574 | * commit object and a flag indicating the cleanness of the merge. |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1575 | */ |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1576 | static int merge(struct commit *h1, |
| 1577 | struct commit *h2, |
| 1578 | const char *branch1, |
| 1579 | const char *branch2, |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1580 | struct commit_list *ca, |
Junio C Hamano | 65ac6e9 | 2006-10-27 14:08:14 -0700 | [diff] [blame] | 1581 | struct commit **result) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1582 | { |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1583 | struct commit_list *iter; |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1584 | struct commit *merged_common_ancestors; |
Junio C Hamano | f120ae2 | 2007-10-29 12:00:55 -0700 | [diff] [blame] | 1585 | struct tree *mrtree = mrtree; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1586 | int clean; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1587 | |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1588 | if (show(4)) { |
| 1589 | output(4, "Merging:"); |
| 1590 | output_commit_title(h1); |
| 1591 | output_commit_title(h2); |
| 1592 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1593 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1594 | if (!ca) { |
| 1595 | ca = get_merge_bases(h1, h2, 1); |
| 1596 | ca = reverse_commit_list(ca); |
| 1597 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1598 | |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1599 | if (show(5)) { |
| 1600 | output(5, "found %u common ancestor(s):", commit_list_count(ca)); |
| 1601 | for (iter = ca; iter; iter = iter->next) |
| 1602 | output_commit_title(iter->item); |
| 1603 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1604 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1605 | merged_common_ancestors = pop_commit(&ca); |
Johannes Schindelin | 934d9a2 | 2006-08-09 18:43:03 +0200 | [diff] [blame] | 1606 | if (merged_common_ancestors == NULL) { |
| 1607 | /* if there is no common ancestor, make an empty tree */ |
| 1608 | struct tree *tree = xcalloc(1, sizeof(struct tree)); |
Johannes Schindelin | 934d9a2 | 2006-08-09 18:43:03 +0200 | [diff] [blame] | 1609 | |
| 1610 | tree->object.parsed = 1; |
| 1611 | tree->object.type = OBJ_TREE; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 1612 | pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); |
Johannes Schindelin | 934d9a2 | 2006-08-09 18:43:03 +0200 | [diff] [blame] | 1613 | merged_common_ancestors = make_virtual_commit(tree, "ancestor"); |
| 1614 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1615 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1616 | for (iter = ca; iter; iter = iter->next) { |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 1617 | call_depth++; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * When the merge fails, the result contains files |
| 1620 | * with conflict markers. The cleanness flag is |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 1621 | * ignored, it was never actually used, as result of |
| 1622 | * merge_trees has always overwritten it: the committed |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1623 | * "conflicts" were already resolved. |
| 1624 | */ |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1625 | discard_cache(); |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1626 | merge(merged_common_ancestors, iter->item, |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1627 | "Temporary merge branch 1", |
| 1628 | "Temporary merge branch 2", |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1629 | NULL, |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1630 | &merged_common_ancestors); |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 1631 | call_depth--; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1632 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1633 | if (!merged_common_ancestors) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1634 | die("merge returned no commit"); |
| 1635 | } |
| 1636 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1637 | discard_cache(); |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 1638 | if (!call_depth) { |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1639 | read_cache(); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1640 | index_only = 0; |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1641 | } else |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1642 | index_only = 1; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1643 | |
Johannes Schindelin | 5a75361 | 2006-07-27 19:12:29 +0200 | [diff] [blame] | 1644 | clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, |
Johannes Schindelin | c1d2084 | 2006-07-27 19:13:47 +0200 | [diff] [blame] | 1645 | branch1, branch2, &mrtree); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1646 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1647 | if (index_only) { |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1648 | *result = make_virtual_commit(mrtree, "merged tree"); |
| 1649 | commit_list_insert(h1, &(*result)->parents); |
| 1650 | commit_list_insert(h2, &(*result)->parents->next); |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1651 | } |
Shawn O. Pearce | 66a155b | 2007-01-14 00:28:53 -0500 | [diff] [blame] | 1652 | flush_output(); |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1653 | return clean; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Shawn O. Pearce | 7ba3c07 | 2006-12-28 02:35:20 -0500 | [diff] [blame] | 1656 | static const char *better_branch_name(const char *branch) |
| 1657 | { |
| 1658 | static char githead_env[8 + 40 + 1]; |
| 1659 | char *name; |
| 1660 | |
| 1661 | if (strlen(branch) != 40) |
| 1662 | return branch; |
| 1663 | sprintf(githead_env, "GITHEAD_%s", branch); |
| 1664 | name = getenv(githead_env); |
| 1665 | return name ? name : branch; |
| 1666 | } |
| 1667 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1668 | static struct commit *get_ref(const char *ref) |
| 1669 | { |
| 1670 | unsigned char sha1[20]; |
| 1671 | struct object *object; |
| 1672 | |
| 1673 | if (get_sha1(ref, sha1)) |
| 1674 | die("Could not resolve ref '%s'", ref); |
| 1675 | object = deref_tag(parse_object(sha1), ref, strlen(ref)); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 1676 | if (!object) |
| 1677 | return NULL; |
Shawn O. Pearce | a970e84 | 2006-12-28 02:35:24 -0500 | [diff] [blame] | 1678 | if (object->type == OBJ_TREE) |
| 1679 | return make_virtual_commit((struct tree*)object, |
| 1680 | better_branch_name(ref)); |
Junio C Hamano | bf6d324 | 2006-07-13 23:38:11 -0700 | [diff] [blame] | 1681 | if (object->type != OBJ_COMMIT) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1682 | return NULL; |
| 1683 | if (parse_commit((struct commit *)object)) |
| 1684 | die("Could not parse commit '%s'", sha1_to_hex(object->sha1)); |
| 1685 | return (struct commit *)object; |
| 1686 | } |
| 1687 | |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1688 | static int merge_config(const char *var, const char *value) |
| 1689 | { |
| 1690 | if (!strcasecmp(var, "merge.verbosity")) { |
| 1691 | verbosity = git_config_int(var, value); |
| 1692 | return 0; |
| 1693 | } |
Lars Hjemli | df3a02f | 2007-09-25 08:36:38 +0200 | [diff] [blame] | 1694 | if (!strcasecmp(var, "diff.renamelimit")) { |
| 1695 | rename_limit = git_config_int(var, value); |
| 1696 | return 0; |
| 1697 | } |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1698 | return git_default_config(var, value); |
| 1699 | } |
| 1700 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1701 | int main(int argc, char *argv[]) |
| 1702 | { |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1703 | static const char *bases[20]; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1704 | static unsigned bases_count = 0; |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1705 | int i, clean; |
| 1706 | const char *branch1, *branch2; |
| 1707 | struct commit *result, *h1, *h2; |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1708 | struct commit_list *ca = NULL; |
| 1709 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); |
| 1710 | int index_fd; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1711 | |
Junio C Hamano | 68faf68 | 2007-02-15 16:32:45 -0800 | [diff] [blame] | 1712 | if (argv[0]) { |
| 1713 | int namelen = strlen(argv[0]); |
| 1714 | if (8 < namelen && |
| 1715 | !strcmp(argv[0] + namelen - 8, "-subtree")) |
| 1716 | subtree_merge = 1; |
| 1717 | } |
| 1718 | |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1719 | git_config(merge_config); |
| 1720 | if (getenv("GIT_MERGE_VERBOSITY")) |
| 1721 | verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1722 | |
| 1723 | if (argc < 4) |
| 1724 | die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]); |
| 1725 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1726 | for (i = 1; i < argc; ++i) { |
| 1727 | if (!strcmp(argv[i], "--")) |
| 1728 | break; |
| 1729 | if (bases_count < sizeof(bases)/sizeof(*bases)) |
| 1730 | bases[bases_count++] = argv[i]; |
| 1731 | } |
| 1732 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ |
| 1733 | die("Not handling anything other than two heads merge."); |
Shawn O. Pearce | ad57cbc | 2007-04-20 02:37:18 -0400 | [diff] [blame] | 1734 | if (verbosity >= 5) |
Shawn O. Pearce | 3f6ee2d | 2007-01-14 00:28:58 -0500 | [diff] [blame] | 1735 | buffer_output = 0; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1736 | |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1737 | branch1 = argv[++i]; |
| 1738 | branch2 = argv[++i]; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1739 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1740 | h1 = get_ref(branch1); |
| 1741 | h2 = get_ref(branch2); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1742 | |
Shawn O. Pearce | e0ec181 | 2006-12-23 03:44:47 -0500 | [diff] [blame] | 1743 | branch1 = better_branch_name(branch1); |
| 1744 | branch2 = better_branch_name(branch2); |
Shawn O. Pearce | 3f6ee2d | 2007-01-14 00:28:58 -0500 | [diff] [blame] | 1745 | |
Shawn O. Pearce | 8c3275a | 2007-01-14 00:28:48 -0500 | [diff] [blame] | 1746 | if (show(3)) |
| 1747 | printf("Merging %s with %s\n", branch1, branch2); |
Shawn O. Pearce | e0ec181 | 2006-12-23 03:44:47 -0500 | [diff] [blame] | 1748 | |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 1749 | index_fd = hold_locked_index(lock, 1); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1750 | |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1751 | for (i = 0; i < bases_count; i++) { |
| 1752 | struct commit *ancestor = get_ref(bases[i]); |
| 1753 | ca = commit_list_insert(ancestor, &ca); |
| 1754 | } |
Shawn O. Pearce | 6388963 | 2007-01-14 00:28:33 -0500 | [diff] [blame] | 1755 | clean = merge(h1, h2, branch1, branch2, ca, &result); |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1756 | |
| 1757 | if (active_cache_changed && |
| 1758 | (write_cache(index_fd, active_cache, active_nr) || |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 1759 | commit_locked_index(lock))) |
Junio C Hamano | 8b944b5 | 2007-01-10 11:20:58 -0800 | [diff] [blame] | 1760 | die ("unable to write %s", get_index_file()); |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1761 | |
Johannes Schindelin | 3af244c | 2006-07-27 13:17:07 +0200 | [diff] [blame] | 1762 | return clean ? 0: 1; |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 1763 | } |