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