| Linus Torvalds | 8bc9a0c | 2005-04-07 22:16:10 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 6 | #define DBRT_DEBUG 1 |
| 7 | |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 8 | #include "cache.h" |
| 9 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 10 | #include "object.h" |
| 11 | #include "tree.h" |
| 12 | |
| 13 | static int merge = 0; |
| Linus Torvalds | 220a0b5 | 2005-06-06 05:07:31 | [diff] [blame] | 14 | static int update = 0; |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 15 | static int index_only = 0; |
| Linus Torvalds | 23822a3 | 2005-09-29 15:16:12 | [diff] [blame] | 16 | static int nontrivial_merge = 0; |
| 17 | static int trivial_merges_only = 0; |
| Linus Torvalds | d99082e | 2005-04-16 05:53:45 | [diff] [blame] | 18 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 19 | static int head_idx = -1; |
| 20 | static int merge_size = 0; |
| Junio C Hamano | b12ec37 | 2005-04-21 01:06:50 | [diff] [blame] | 21 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 22 | static struct object_list *trees = NULL; |
| 23 | |
| 24 | static struct cache_entry df_conflict_entry = { |
| 25 | }; |
| 26 | |
| 27 | static struct tree_entry_list df_conflict_list = { |
| 28 | .name = NULL, |
| 29 | .next = &df_conflict_list |
| 30 | }; |
| 31 | |
| 32 | typedef int (*merge_fn_t)(struct cache_entry **src); |
| 33 | |
| 34 | static int entcmp(char *name1, int dir1, char *name2, int dir2) |
| 35 | { |
| 36 | int len1 = strlen(name1); |
| 37 | int len2 = strlen(name2); |
| 38 | int len = len1 < len2 ? len1 : len2; |
| 39 | int ret = memcmp(name1, name2, len); |
| 40 | unsigned char c1, c2; |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | c1 = name1[len]; |
| 44 | c2 = name2[len]; |
| 45 | if (!c1 && dir1) |
| 46 | c1 = '/'; |
| 47 | if (!c2 && dir2) |
| 48 | c2 = '/'; |
| 49 | ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; |
| 50 | if (c1 && c2 && !ret) |
| 51 | ret = len1 - len2; |
| Petr Baudis | 1424246 | 2005-05-11 21:16:23 | [diff] [blame] | 52 | return ret; |
| Junio C Hamano | b12ec37 | 2005-04-21 01:06:50 | [diff] [blame] | 53 | } |
| 54 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 55 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, |
| 56 | const char *base, merge_fn_t fn, int *indpos) |
| Linus Torvalds | ca016f0 | 2005-04-19 18:16:12 | [diff] [blame] | 57 | { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 58 | int baselen = strlen(base); |
| 59 | int src_size = len + 1; |
| 60 | do { |
| 61 | int i; |
| 62 | char *first; |
| 63 | int firstdir = 0; |
| 64 | int pathlen; |
| 65 | unsigned ce_size; |
| 66 | struct tree_entry_list **subposns; |
| 67 | struct cache_entry **src; |
| 68 | int any_files = 0; |
| 69 | int any_dirs = 0; |
| 70 | char *cache_name; |
| 71 | int ce_stage; |
| 72 | |
| 73 | /* Find the first name in the input. */ |
| 74 | |
| 75 | first = NULL; |
| 76 | cache_name = NULL; |
| 77 | |
| 78 | /* Check the cache */ |
| 79 | if (merge && *indpos < active_nr) { |
| 80 | /* This is a bit tricky: */ |
| 81 | /* If the index has a subdirectory (with |
| 82 | * contents) as the first name, it'll get a |
| 83 | * filename like "foo/bar". But that's after |
| 84 | * "foo", so the entry in trees will get |
| 85 | * handled first, at which point we'll go into |
| 86 | * "foo", and deal with "bar" from the index, |
| 87 | * because the base will be "foo/". The only |
| 88 | * way we can actually have "foo/bar" first of |
| 89 | * all the things is if the trees don't |
| 90 | * contain "foo" at all, in which case we'll |
| 91 | * handle "foo/bar" without going into the |
| 92 | * directory, but that's fine (and will return |
| 93 | * an error anyway, with the added unknown |
| 94 | * file case. |
| 95 | */ |
| 96 | |
| 97 | cache_name = active_cache[*indpos]->name; |
| 98 | if (strlen(cache_name) > baselen && |
| 99 | !memcmp(cache_name, base, baselen)) { |
| 100 | cache_name += baselen; |
| 101 | first = cache_name; |
| 102 | } else { |
| 103 | cache_name = NULL; |
| 104 | } |
| 105 | } |
| 106 | |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 107 | #if DBRT_DEBUG > 1 |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 108 | if (first) |
| 109 | printf("index %s\n", first); |
| Junio C Hamano | 2ab706a | 2005-09-05 08:13:36 | [diff] [blame] | 110 | #endif |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 111 | for (i = 0; i < len; i++) { |
| 112 | if (!posns[i] || posns[i] == &df_conflict_list) |
| 113 | continue; |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 114 | #if DBRT_DEBUG > 1 |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 115 | printf("%d %s\n", i + 1, posns[i]->name); |
| Junio C Hamano | 2ab706a | 2005-09-05 08:13:36 | [diff] [blame] | 116 | #endif |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 117 | if (!first || entcmp(first, firstdir, |
| 118 | posns[i]->name, |
| 119 | posns[i]->directory) > 0) { |
| 120 | first = posns[i]->name; |
| 121 | firstdir = posns[i]->directory; |
| 122 | } |
| 123 | } |
| 124 | /* No name means we're done */ |
| 125 | if (!first) |
| 126 | return 0; |
| 127 | |
| 128 | pathlen = strlen(first); |
| 129 | ce_size = cache_entry_size(baselen + pathlen); |
| 130 | |
| 131 | src = xmalloc(sizeof(struct cache_entry *) * src_size); |
| 132 | memset(src, 0, sizeof(struct cache_entry *) * src_size); |
| 133 | |
| 134 | subposns = xmalloc(sizeof(struct tree_list_entry *) * len); |
| 135 | memset(subposns, 0, sizeof(struct tree_list_entry *) * len); |
| 136 | |
| 137 | if (cache_name && !strcmp(cache_name, first)) { |
| 138 | any_files = 1; |
| 139 | src[0] = active_cache[*indpos]; |
| 140 | remove_cache_entry_at(*indpos); |
| 141 | } |
| 142 | |
| 143 | for (i = 0; i < len; i++) { |
| 144 | struct cache_entry *ce; |
| 145 | |
| 146 | if (!posns[i] || |
| 147 | (posns[i] != &df_conflict_list && |
| 148 | strcmp(first, posns[i]->name))) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | if (posns[i] == &df_conflict_list) { |
| 153 | src[i + merge] = &df_conflict_entry; |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | if (posns[i]->directory) { |
| 158 | any_dirs = 1; |
| 159 | parse_tree(posns[i]->item.tree); |
| 160 | subposns[i] = posns[i]->item.tree->entries; |
| 161 | posns[i] = posns[i]->next; |
| 162 | src[i + merge] = &df_conflict_entry; |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | if (!merge) |
| 167 | ce_stage = 0; |
| 168 | else if (i + 1 < head_idx) |
| 169 | ce_stage = 1; |
| 170 | else if (i + 1 > head_idx) |
| 171 | ce_stage = 3; |
| 172 | else |
| 173 | ce_stage = 2; |
| 174 | |
| 175 | ce = xmalloc(ce_size); |
| 176 | memset(ce, 0, ce_size); |
| 177 | ce->ce_mode = create_ce_mode(posns[i]->mode); |
| 178 | ce->ce_flags = create_ce_flags(baselen + pathlen, |
| 179 | ce_stage); |
| 180 | memcpy(ce->name, base, baselen); |
| 181 | memcpy(ce->name + baselen, first, pathlen + 1); |
| 182 | |
| 183 | any_files = 1; |
| 184 | |
| 185 | memcpy(ce->sha1, posns[i]->item.any->sha1, 20); |
| 186 | src[i + merge] = ce; |
| 187 | subposns[i] = &df_conflict_list; |
| 188 | posns[i] = posns[i]->next; |
| 189 | } |
| 190 | if (any_files) { |
| 191 | if (merge) { |
| 192 | int ret; |
| 193 | |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 194 | #if DBRT_DEBUG > 1 |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 195 | printf("%s:\n", first); |
| 196 | for (i = 0; i < src_size; i++) { |
| 197 | printf(" %d ", i); |
| 198 | if (src[i]) |
| 199 | printf("%s\n", sha1_to_hex(src[i]->sha1)); |
| 200 | else |
| 201 | printf("\n"); |
| 202 | } |
| Junio C Hamano | 2ab706a | 2005-09-05 08:13:36 | [diff] [blame] | 203 | #endif |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 204 | ret = fn(src); |
| 205 | |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 206 | #if DBRT_DEBUG > 1 |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 207 | printf("Added %d entries\n", ret); |
| Junio C Hamano | 2ab706a | 2005-09-05 08:13:36 | [diff] [blame] | 208 | #endif |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 209 | *indpos += ret; |
| 210 | } else { |
| 211 | for (i = 0; i < src_size; i++) { |
| 212 | if (src[i]) { |
| 213 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | if (any_dirs) { |
| 219 | char *newbase = xmalloc(baselen + 2 + pathlen); |
| 220 | memcpy(newbase, base, baselen); |
| 221 | memcpy(newbase + baselen, first, pathlen); |
| 222 | newbase[baselen + pathlen] = '/'; |
| 223 | newbase[baselen + pathlen + 1] = '\0'; |
| 224 | if (unpack_trees_rec(subposns, len, newbase, fn, |
| 225 | indpos)) |
| 226 | return -1; |
| Junio C Hamano | bb97a2a | 2005-09-11 01:14:14 | [diff] [blame] | 227 | free(newbase); |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 228 | } |
| 229 | free(subposns); |
| 230 | free(src); |
| 231 | } while (1); |
| Linus Torvalds | ca016f0 | 2005-04-19 18:16:12 | [diff] [blame] | 232 | } |
| 233 | |
| Linus Torvalds | 76f3834 | 2005-06-06 03:28:33 | [diff] [blame] | 234 | static void reject_merge(struct cache_entry *ce) |
| Linus Torvalds | 02ede67 | 2005-06-06 03:02:31 | [diff] [blame] | 235 | { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 236 | die("Entry '%s' would be overwritten by merge. Cannot merge.", |
| 237 | ce->name); |
| Linus Torvalds | d99082e | 2005-04-16 05:53:45 | [diff] [blame] | 238 | } |
| 239 | |
| Junio C Hamano | 340e4f8 | 2005-10-11 00:34:08 | [diff] [blame] | 240 | /* Unlink the last component and attempt to remove leading |
| 241 | * directories, in case this unlink is the removal of the |
| 242 | * last entry in the directory -- empty directories are removed. |
| 243 | */ |
| 244 | static void unlink_entry(char *name) |
| 245 | { |
| 246 | char *cp, *prev; |
| 247 | |
| 248 | if (unlink(name)) |
| 249 | return; |
| 250 | prev = NULL; |
| 251 | while (1) { |
| 252 | int status; |
| 253 | cp = strrchr(name, '/'); |
| 254 | if (prev) |
| 255 | *prev = '/'; |
| 256 | if (!cp) |
| 257 | break; |
| 258 | |
| 259 | *cp = 0; |
| 260 | status = rmdir(name); |
| 261 | if (status) { |
| 262 | *cp = '/'; |
| 263 | break; |
| 264 | } |
| 265 | prev = cp; |
| 266 | } |
| 267 | } |
| 268 | |
| Linus Torvalds | 220a0b5 | 2005-06-06 05:07:31 | [diff] [blame] | 269 | static void check_updates(struct cache_entry **src, int nr) |
| 270 | { |
| 271 | static struct checkout state = { |
| 272 | .base_dir = "", |
| 273 | .force = 1, |
| 274 | .quiet = 1, |
| 275 | .refresh_cache = 1, |
| 276 | }; |
| 277 | unsigned short mask = htons(CE_UPDATE); |
| 278 | while (nr--) { |
| 279 | struct cache_entry *ce = *src++; |
| Linus Torvalds | aa16021 | 2005-06-09 22:34:04 | [diff] [blame] | 280 | if (!ce->ce_mode) { |
| 281 | if (update) |
| Junio C Hamano | 340e4f8 | 2005-10-11 00:34:08 | [diff] [blame] | 282 | unlink_entry(ce->name); |
| Linus Torvalds | aa16021 | 2005-06-09 22:34:04 | [diff] [blame] | 283 | continue; |
| 284 | } |
| Linus Torvalds | 220a0b5 | 2005-06-06 05:07:31 | [diff] [blame] | 285 | if (ce->ce_flags & mask) { |
| 286 | ce->ce_flags &= ~mask; |
| 287 | if (update) |
| 288 | checkout_entry(ce, &state); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 293 | static int unpack_trees(merge_fn_t fn) |
| Linus Torvalds | d723c69 | 2005-06-06 21:01:58 | [diff] [blame] | 294 | { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 295 | int indpos = 0; |
| 296 | unsigned len = object_list_length(trees); |
| Junio C Hamano | 7e4a2a8 | 2005-12-26 20:34:56 | [diff] [blame] | 297 | struct tree_entry_list **posns; |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 298 | int i; |
| 299 | struct object_list *posn = trees; |
| 300 | merge_size = len; |
| Junio C Hamano | 7e4a2a8 | 2005-12-26 20:34:56 | [diff] [blame] | 301 | |
| 302 | if (len) { |
| 303 | posns = xmalloc(len * sizeof(struct tree_entry_list *)); |
| 304 | for (i = 0; i < len; i++) { |
| 305 | posns[i] = ((struct tree *) posn->item)->entries; |
| 306 | posn = posn->next; |
| 307 | } |
| 308 | if (unpack_trees_rec(posns, len, "", fn, &indpos)) |
| 309 | return -1; |
| Linus Torvalds | d723c69 | 2005-06-06 21:01:58 | [diff] [blame] | 310 | } |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 311 | |
| Linus Torvalds | 23822a3 | 2005-09-29 15:16:12 | [diff] [blame] | 312 | if (trivial_merges_only && nontrivial_merge) |
| 313 | die("Merge requires file-level merging"); |
| 314 | |
| Linus Torvalds | d723c69 | 2005-06-06 21:01:58 | [diff] [blame] | 315 | check_updates(active_cache, active_nr); |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int list_tree(unsigned char *sha1) |
| 320 | { |
| 321 | struct tree *tree = parse_tree_indirect(sha1); |
| 322 | if (!tree) |
| 323 | return -1; |
| 324 | object_list_append(&tree->object, &trees); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int same(struct cache_entry *a, struct cache_entry *b) |
| 329 | { |
| 330 | if (!!a != !!b) |
| 331 | return 0; |
| 332 | if (!a && !b) |
| 333 | return 1; |
| 334 | return a->ce_mode == b->ce_mode && |
| 335 | !memcmp(a->sha1, b->sha1, 20); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | * When a CE gets turned into an unmerged entry, we |
| 341 | * want it to be up-to-date |
| 342 | */ |
| 343 | static void verify_uptodate(struct cache_entry *ce) |
| 344 | { |
| 345 | struct stat st; |
| 346 | |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 347 | if (index_only) |
| 348 | return; |
| 349 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 350 | if (!lstat(ce->name, &st)) { |
| 351 | unsigned changed = ce_match_stat(ce, &st); |
| 352 | if (!changed) |
| 353 | return; |
| 354 | errno = 0; |
| 355 | } |
| 356 | if (errno == ENOENT) |
| 357 | return; |
| 358 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); |
| 359 | } |
| 360 | |
| 361 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old) |
| 362 | { |
| 363 | merge->ce_flags |= htons(CE_UPDATE); |
| 364 | if (old) { |
| 365 | /* |
| 366 | * See if we can re-use the old CE directly? |
| 367 | * That way we get the uptodate stat info. |
| 368 | * |
| 369 | * This also removes the UPDATE flag on |
| 370 | * a match. |
| 371 | */ |
| 372 | if (same(old, merge)) { |
| 373 | *merge = *old; |
| 374 | } else { |
| 375 | verify_uptodate(old); |
| 376 | } |
| 377 | } |
| 378 | merge->ce_flags &= ~htons(CE_STAGEMASK); |
| 379 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD); |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old) |
| 384 | { |
| 385 | if (old) |
| 386 | verify_uptodate(old); |
| 387 | ce->ce_mode = 0; |
| 388 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
| 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | static int keep_entry(struct cache_entry *ce) |
| 393 | { |
| 394 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
| 395 | return 1; |
| 396 | } |
| 397 | |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 398 | #if DBRT_DEBUG |
| 399 | static void show_stage_entry(FILE *o, |
| 400 | const char *label, const struct cache_entry *ce) |
| 401 | { |
| Junio C Hamano | 2ba6c47 | 2005-09-14 05:27:42 | [diff] [blame] | 402 | if (!ce) |
| 403 | fprintf(o, "%s (missing)\n", label); |
| 404 | else |
| 405 | fprintf(o, "%s%06o %s %d\t%s\n", |
| 406 | label, |
| 407 | ntohl(ce->ce_mode), |
| 408 | sha1_to_hex(ce->sha1), |
| 409 | ce_stage(ce), |
| 410 | ce->name); |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 411 | } |
| 412 | #endif |
| 413 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 414 | static int threeway_merge(struct cache_entry **stages) |
| 415 | { |
| 416 | struct cache_entry *index; |
| 417 | struct cache_entry *head; |
| 418 | struct cache_entry *remote = stages[head_idx + 1]; |
| 419 | int count; |
| 420 | int head_match = 0; |
| 421 | int remote_match = 0; |
| 422 | |
| 423 | int df_conflict_head = 0; |
| 424 | int df_conflict_remote = 0; |
| 425 | |
| 426 | int any_anc_missing = 0; |
| 427 | int i; |
| 428 | |
| 429 | for (i = 1; i < head_idx; i++) { |
| 430 | if (!stages[i]) |
| 431 | any_anc_missing = 1; |
| 432 | } |
| 433 | |
| 434 | index = stages[0]; |
| 435 | head = stages[head_idx]; |
| 436 | |
| 437 | if (head == &df_conflict_entry) { |
| 438 | df_conflict_head = 1; |
| 439 | head = NULL; |
| 440 | } |
| 441 | |
| 442 | if (remote == &df_conflict_entry) { |
| 443 | df_conflict_remote = 1; |
| 444 | remote = NULL; |
| 445 | } |
| 446 | |
| 447 | /* First, if there's a #16 situation, note that to prevent #13 |
| 448 | * and #14. |
| 449 | */ |
| 450 | if (!same(remote, head)) { |
| 451 | for (i = 1; i < head_idx; i++) { |
| 452 | if (same(stages[i], head)) { |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 453 | head_match = i; |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 454 | } |
| 455 | if (same(stages[i], remote)) { |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 456 | remote_match = i; |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /* We start with cases where the index is allowed to match |
| 462 | * something other than the head: #14(ALT) and #2ALT, where it |
| 463 | * is permitted to match the result instead. |
| 464 | */ |
| 465 | /* #14, #14ALT, #2ALT */ |
| 466 | if (remote && !df_conflict_head && head_match && !remote_match) { |
| 467 | if (index && !same(index, remote) && !same(index, head)) |
| 468 | reject_merge(index); |
| 469 | return merged_entry(remote, index); |
| 470 | } |
| 471 | /* |
| 472 | * If we have an entry in the index cache, then we want to |
| 473 | * make sure that it matches head. |
| 474 | */ |
| 475 | if (index && !same(index, head)) { |
| 476 | reject_merge(index); |
| 477 | } |
| 478 | |
| 479 | if (head) { |
| 480 | /* #5ALT, #15 */ |
| 481 | if (same(head, remote)) |
| 482 | return merged_entry(head, index); |
| 483 | /* #13, #3ALT */ |
| 484 | if (!df_conflict_remote && remote_match && !head_match) |
| 485 | return merged_entry(head, index); |
| 486 | } |
| 487 | |
| 488 | /* #1 */ |
| 489 | if (!head && !remote && any_anc_missing) |
| 490 | return 0; |
| 491 | |
| 492 | /* Below are "no merge" cases, which require that the index be |
| 493 | * up-to-date to avoid the files getting overwritten with |
| 494 | * conflict resolution files. |
| 495 | */ |
| 496 | if (index) { |
| 497 | verify_uptodate(index); |
| 498 | } |
| 499 | |
| Linus Torvalds | 23822a3 | 2005-09-29 15:16:12 | [diff] [blame] | 500 | nontrivial_merge = 1; |
| 501 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 502 | /* #2, #3, #4, #6, #7, #9, #11. */ |
| 503 | count = 0; |
| 504 | if (!head_match || !remote_match) { |
| 505 | for (i = 1; i < head_idx; i++) { |
| 506 | if (stages[i]) { |
| 507 | keep_entry(stages[i]); |
| 508 | count++; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| Junio C Hamano | 4d3fe0c | 2005-09-06 19:53:56 | [diff] [blame] | 513 | #if DBRT_DEBUG |
| 514 | else { |
| 515 | fprintf(stderr, "read-tree: warning #16 detected\n"); |
| 516 | show_stage_entry(stderr, "head ", stages[head_match]); |
| 517 | show_stage_entry(stderr, "remote ", stages[remote_match]); |
| 518 | } |
| 519 | #endif |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 520 | if (head) { count += keep_entry(head); } |
| 521 | if (remote) { count += keep_entry(remote); } |
| 522 | return count; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Two-way merge. |
| 527 | * |
| 528 | * The rule is to "carry forward" what is in the index without losing |
| 529 | * information across a "fast forward", favoring a successful merge |
| 530 | * over a merge failure when it makes sense. For details of the |
| 531 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. |
| 532 | * |
| 533 | */ |
| 534 | static int twoway_merge(struct cache_entry **src) |
| 535 | { |
| 536 | struct cache_entry *current = src[0]; |
| 537 | struct cache_entry *oldtree = src[1], *newtree = src[2]; |
| 538 | |
| 539 | if (merge_size != 2) |
| 540 | return error("Cannot do a twoway merge of %d trees\n", |
| 541 | merge_size); |
| 542 | |
| 543 | if (current) { |
| 544 | if ((!oldtree && !newtree) || /* 4 and 5 */ |
| 545 | (!oldtree && newtree && |
| 546 | same(current, newtree)) || /* 6 and 7 */ |
| 547 | (oldtree && newtree && |
| 548 | same(oldtree, newtree)) || /* 14 and 15 */ |
| 549 | (oldtree && newtree && |
| 550 | !same(oldtree, newtree) && /* 18 and 19*/ |
| 551 | same(current, newtree))) { |
| 552 | return keep_entry(current); |
| 553 | } |
| 554 | else if (oldtree && !newtree && same(current, oldtree)) { |
| 555 | /* 10 or 11 */ |
| 556 | return deleted_entry(oldtree, current); |
| 557 | } |
| 558 | else if (oldtree && newtree && |
| 559 | same(current, oldtree) && !same(current, newtree)) { |
| 560 | /* 20 or 21 */ |
| 561 | return merged_entry(newtree, current); |
| 562 | } |
| 563 | else { |
| 564 | /* all other failures */ |
| 565 | if (oldtree) |
| 566 | reject_merge(oldtree); |
| 567 | if (current) |
| 568 | reject_merge(current); |
| 569 | if (newtree) |
| 570 | reject_merge(newtree); |
| 571 | return -1; |
| 572 | } |
| 573 | } |
| 574 | else if (newtree) |
| 575 | return merged_entry(newtree, current); |
| 576 | else |
| 577 | return deleted_entry(oldtree, current); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * One-way merge. |
| 582 | * |
| 583 | * The rule is: |
| 584 | * - take the stat information from stage0, take the data from stage1 |
| 585 | */ |
| 586 | static int oneway_merge(struct cache_entry **src) |
| 587 | { |
| 588 | struct cache_entry *old = src[0]; |
| 589 | struct cache_entry *a = src[1]; |
| 590 | |
| 591 | if (merge_size != 1) |
| 592 | return error("Cannot do a oneway merge of %d trees\n", |
| 593 | merge_size); |
| 594 | |
| 595 | if (!a) |
| 596 | return 0; |
| 597 | if (old && same(old, a)) { |
| 598 | return keep_entry(old); |
| 599 | } |
| 600 | return merged_entry(a, NULL); |
| Linus Torvalds | d723c69 | 2005-06-06 21:01:58 | [diff] [blame] | 601 | } |
| 602 | |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 603 | static int read_cache_unmerged(void) |
| 604 | { |
| 605 | int i, deleted; |
| 606 | struct cache_entry **dst; |
| 607 | |
| 608 | read_cache(); |
| 609 | dst = active_cache; |
| 610 | deleted = 0; |
| 611 | for (i = 0; i < active_nr; i++) { |
| 612 | struct cache_entry *ce = active_cache[i]; |
| 613 | if (ce_stage(ce)) { |
| 614 | deleted++; |
| 615 | continue; |
| 616 | } |
| 617 | if (deleted) |
| 618 | *dst = ce; |
| 619 | dst++; |
| 620 | } |
| 621 | active_nr -= deleted; |
| 622 | return deleted; |
| 623 | } |
| 624 | |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 625 | static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])"; |
| Junio C Hamano | c5bac17 | 2005-04-21 02:49:16 | [diff] [blame] | 626 | |
| Junio C Hamano | 96cd542 | 2005-06-06 19:20:55 | [diff] [blame] | 627 | static struct cache_file cache_file; |
| 628 | |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 629 | int main(int argc, char **argv) |
| 630 | { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 631 | int i, newfd, reset, stage = 0; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 632 | unsigned char sha1[20]; |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 633 | merge_fn_t fn = NULL; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 634 | |
| Junio C Hamano | 53228a5 | 2005-11-26 08:50:02 | [diff] [blame] | 635 | setup_git_directory(); |
| 636 | |
| Junio C Hamano | 96cd542 | 2005-06-06 19:20:55 | [diff] [blame] | 637 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 638 | if (newfd < 0) |
| Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 639 | die("unable to create new cachefile"); |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 640 | |
| Alex Riesen | 39b4ac9 | 2005-11-08 08:23:37 | [diff] [blame] | 641 | git_config(git_default_config); |
| 642 | |
| Linus Torvalds | ca016f0 | 2005-04-19 18:16:12 | [diff] [blame] | 643 | merge = 0; |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 644 | reset = 0; |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 645 | for (i = 1; i < argc; i++) { |
| 646 | const char *arg = argv[i]; |
| 647 | |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 648 | /* "-u" means "update", meaning that a merge will update |
| 649 | * the working tree. |
| 650 | */ |
| Linus Torvalds | 220a0b5 | 2005-06-06 05:07:31 | [diff] [blame] | 651 | if (!strcmp(arg, "-u")) { |
| 652 | update = 1; |
| 653 | continue; |
| 654 | } |
| 655 | |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 656 | /* "-i" means "index only", meaning that a merge will |
| 657 | * not even look at the working tree. |
| 658 | */ |
| 659 | if (!strcmp(arg, "-i")) { |
| 660 | index_only = 1; |
| 661 | continue; |
| 662 | } |
| 663 | |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 664 | /* This differs from "-m" in that we'll silently ignore unmerged entries */ |
| 665 | if (!strcmp(arg, "--reset")) { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 666 | if (stage || merge) |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 667 | usage(read_tree_usage); |
| 668 | reset = 1; |
| 669 | merge = 1; |
| 670 | stage = 1; |
| 671 | read_cache_unmerged(); |
| Linus Torvalds | 7875b50 | 2005-06-15 17:25:46 | [diff] [blame] | 672 | continue; |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 673 | } |
| 674 | |
| Linus Torvalds | 23822a3 | 2005-09-29 15:16:12 | [diff] [blame] | 675 | if (!strcmp(arg, "--trivial")) { |
| 676 | trivial_merges_only = 1; |
| 677 | continue; |
| 678 | } |
| 679 | |
| Linus Torvalds | d99082e | 2005-04-16 05:53:45 | [diff] [blame] | 680 | /* "-m" stands for "merge", meaning we start in stage 1 */ |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 681 | if (!strcmp(arg, "-m")) { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 682 | if (stage || merge) |
| Linus Torvalds | 438195c | 2005-06-09 19:51:01 | [diff] [blame] | 683 | usage(read_tree_usage); |
| 684 | if (read_cache_unmerged()) |
| 685 | die("you need to resolve your current index first"); |
| Linus Torvalds | d99082e | 2005-04-16 05:53:45 | [diff] [blame] | 686 | stage = 1; |
| Linus Torvalds | ca016f0 | 2005-04-19 18:16:12 | [diff] [blame] | 687 | merge = 1; |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 688 | continue; |
| 689 | } |
| Junio C Hamano | 03efa6d | 2005-06-11 01:36:08 | [diff] [blame] | 690 | |
| Junio C Hamano | 720d150 | 2005-09-11 00:46:27 | [diff] [blame] | 691 | /* using -u and -i at the same time makes no sense */ |
| 692 | if (1 < index_only + update) |
| 693 | usage(read_tree_usage); |
| 694 | |
| Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 695 | if (get_sha1(arg, sha1) < 0) |
| Junio C Hamano | c5bac17 | 2005-04-21 02:49:16 | [diff] [blame] | 696 | usage(read_tree_usage); |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 697 | if (list_tree(sha1) < 0) |
| Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 698 | die("failed to unpack tree object %s", arg); |
| Linus Torvalds | d99082e | 2005-04-16 05:53:45 | [diff] [blame] | 699 | stage++; |
| Linus Torvalds | 83adac3 | 2005-04-09 19:11:25 | [diff] [blame] | 700 | } |
| Junio C Hamano | f318dd2 | 2005-09-19 18:33:14 | [diff] [blame] | 701 | if ((update||index_only) && !merge) |
| Linus Torvalds | a57f0b5 | 2005-06-07 22:20:39 | [diff] [blame] | 702 | usage(read_tree_usage); |
| Junio C Hamano | 7dd4357 | 2005-10-02 07:50:16 | [diff] [blame] | 703 | |
| 704 | if (merge) { |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 705 | if (stage < 2) |
| Linus Torvalds | a3a6523 | 2005-04-19 18:41:18 | [diff] [blame] | 706 | die("just how do you expect me to merge %d trees?", stage-1); |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 707 | switch (stage - 1) { |
| 708 | case 1: |
| 709 | fn = oneway_merge; |
| 710 | break; |
| 711 | case 2: |
| 712 | fn = twoway_merge; |
| 713 | break; |
| 714 | case 3: |
| 715 | fn = threeway_merge; |
| 716 | break; |
| 717 | default: |
| 718 | fn = threeway_merge; |
| 719 | break; |
| Junio C Hamano | 03efa6d | 2005-06-11 01:36:08 | [diff] [blame] | 720 | } |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 721 | |
| Daniel Barkalow | ee6566e | 2005-09-05 06:04:48 | [diff] [blame] | 722 | if (stage - 1 >= 3) |
| 723 | head_idx = stage - 2; |
| 724 | else |
| 725 | head_idx = 1; |
| 726 | } |
| 727 | |
| 728 | unpack_trees(fn); |
| Junio C Hamano | 96cd542 | 2005-06-06 19:20:55 | [diff] [blame] | 729 | if (write_cache(newfd, active_cache, active_nr) || |
| 730 | commit_index_file(&cache_file)) |
| Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 731 | die("unable to write new index file"); |
| Linus Torvalds | 9614b8d | 2005-04-11 22:39:26 | [diff] [blame] | 732 | return 0; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 733 | } |