Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 2 | #include "dir.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 3 | #include "tree.h" |
| 4 | #include "tree-walk.h" |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 5 | #include "cache-tree.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 6 | #include "unpack-trees.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 7 | #include "progress.h" |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 8 | #include "refs.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 9 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 10 | #define DBRT_DEBUG 1 |
| 11 | |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 12 | struct tree_entry_list { |
| 13 | struct tree_entry_list *next; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 14 | unsigned int mode; |
| 15 | const char *name; |
| 16 | const unsigned char *sha1; |
| 17 | }; |
| 18 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 19 | static struct tree_entry_list *create_tree_entry_list(struct tree_desc *desc) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 20 | { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 21 | struct name_entry one; |
| 22 | struct tree_entry_list *ret = NULL; |
| 23 | struct tree_entry_list **list_p = &ret; |
| 24 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 25 | while (tree_entry(desc, &one)) { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 26 | struct tree_entry_list *entry; |
| 27 | |
| 28 | entry = xmalloc(sizeof(struct tree_entry_list)); |
| 29 | entry->name = one.path; |
| 30 | entry->sha1 = one.sha1; |
| 31 | entry->mode = one.mode; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 32 | entry->next = NULL; |
| 33 | |
| 34 | *list_p = entry; |
| 35 | list_p = &entry->next; |
| 36 | } |
| 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | static int entcmp(const char *name1, int dir1, const char *name2, int dir2) |
| 41 | { |
| 42 | int len1 = strlen(name1); |
| 43 | int len2 = strlen(name2); |
| 44 | int len = len1 < len2 ? len1 : len2; |
| 45 | int ret = memcmp(name1, name2, len); |
| 46 | unsigned char c1, c2; |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | c1 = name1[len]; |
| 50 | c2 = name2[len]; |
| 51 | if (!c1 && dir1) |
| 52 | c1 = '/'; |
| 53 | if (!c2 && dir2) |
| 54 | c2 = '/'; |
| 55 | ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; |
| 56 | if (c1 && c2 && !ret) |
| 57 | ret = len1 - len2; |
| 58 | return ret; |
| 59 | } |
| 60 | |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 61 | static inline void remove_entry(int remove) |
| 62 | { |
| 63 | if (remove >= 0) |
| 64 | remove_cache_entry_at(remove); |
| 65 | } |
| 66 | |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 67 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, |
| 68 | const char *base, struct unpack_trees_options *o, |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 69 | struct tree_entry_list *df_conflict_list) |
| 70 | { |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 71 | int remove; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 72 | int baselen = strlen(base); |
| 73 | int src_size = len + 1; |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 74 | int i_stk = i_stk; |
| 75 | int retval = 0; |
| 76 | |
| 77 | if (o->dir) |
| 78 | i_stk = push_exclude_per_directory(o->dir, base, strlen(base)); |
| 79 | |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 80 | do { |
| 81 | int i; |
| 82 | const char *first; |
| 83 | int firstdir = 0; |
| 84 | int pathlen; |
| 85 | unsigned ce_size; |
| 86 | struct tree_entry_list **subposns; |
| 87 | struct cache_entry **src; |
| 88 | int any_files = 0; |
| 89 | int any_dirs = 0; |
| 90 | char *cache_name; |
| 91 | int ce_stage; |
| 92 | |
| 93 | /* Find the first name in the input. */ |
| 94 | |
| 95 | first = NULL; |
| 96 | cache_name = NULL; |
| 97 | |
| 98 | /* Check the cache */ |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 99 | if (o->merge && o->pos < active_nr) { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 100 | /* This is a bit tricky: */ |
| 101 | /* If the index has a subdirectory (with |
| 102 | * contents) as the first name, it'll get a |
| 103 | * filename like "foo/bar". But that's after |
| 104 | * "foo", so the entry in trees will get |
| 105 | * handled first, at which point we'll go into |
| 106 | * "foo", and deal with "bar" from the index, |
| 107 | * because the base will be "foo/". The only |
| 108 | * way we can actually have "foo/bar" first of |
| 109 | * all the things is if the trees don't |
| 110 | * contain "foo" at all, in which case we'll |
| 111 | * handle "foo/bar" without going into the |
| 112 | * directory, but that's fine (and will return |
| 113 | * an error anyway, with the added unknown |
| 114 | * file case. |
| 115 | */ |
| 116 | |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 117 | cache_name = active_cache[o->pos]->name; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 118 | if (strlen(cache_name) > baselen && |
| 119 | !memcmp(cache_name, base, baselen)) { |
| 120 | cache_name += baselen; |
| 121 | first = cache_name; |
| 122 | } else { |
| 123 | cache_name = NULL; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #if DBRT_DEBUG > 1 |
| 128 | if (first) |
| 129 | printf("index %s\n", first); |
| 130 | #endif |
| 131 | for (i = 0; i < len; i++) { |
| 132 | if (!posns[i] || posns[i] == df_conflict_list) |
| 133 | continue; |
| 134 | #if DBRT_DEBUG > 1 |
| 135 | printf("%d %s\n", i + 1, posns[i]->name); |
| 136 | #endif |
| 137 | if (!first || entcmp(first, firstdir, |
| 138 | posns[i]->name, |
René Scharfe | 1843d8d | 2007-07-24 23:54:25 +0200 | [diff] [blame] | 139 | S_ISDIR(posns[i]->mode)) > 0) { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 140 | first = posns[i]->name; |
René Scharfe | 1843d8d | 2007-07-24 23:54:25 +0200 | [diff] [blame] | 141 | firstdir = S_ISDIR(posns[i]->mode); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | /* No name means we're done */ |
| 145 | if (!first) |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 146 | goto leave_directory; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 147 | |
| 148 | pathlen = strlen(first); |
| 149 | ce_size = cache_entry_size(baselen + pathlen); |
| 150 | |
| 151 | src = xcalloc(src_size, sizeof(struct cache_entry *)); |
| 152 | |
| 153 | subposns = xcalloc(len, sizeof(struct tree_list_entry *)); |
| 154 | |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 155 | remove = -1; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 156 | if (cache_name && !strcmp(cache_name, first)) { |
| 157 | any_files = 1; |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 158 | src[0] = active_cache[o->pos]; |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 159 | remove = o->pos; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | for (i = 0; i < len; i++) { |
| 163 | struct cache_entry *ce; |
| 164 | |
| 165 | if (!posns[i] || |
| 166 | (posns[i] != df_conflict_list && |
| 167 | strcmp(first, posns[i]->name))) { |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | if (posns[i] == df_conflict_list) { |
| 172 | src[i + o->merge] = o->df_conflict_entry; |
| 173 | continue; |
| 174 | } |
| 175 | |
René Scharfe | 1843d8d | 2007-07-24 23:54:25 +0200 | [diff] [blame] | 176 | if (S_ISDIR(posns[i]->mode)) { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 177 | struct tree *tree = lookup_tree(posns[i]->sha1); |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 178 | struct tree_desc t; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 179 | any_dirs = 1; |
| 180 | parse_tree(tree); |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 181 | init_tree_desc(&t, tree->buffer, tree->size); |
| 182 | subposns[i] = create_tree_entry_list(&t); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 183 | posns[i] = posns[i]->next; |
| 184 | src[i + o->merge] = o->df_conflict_entry; |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | if (!o->merge) |
| 189 | ce_stage = 0; |
| 190 | else if (i + 1 < o->head_idx) |
| 191 | ce_stage = 1; |
| 192 | else if (i + 1 > o->head_idx) |
| 193 | ce_stage = 3; |
| 194 | else |
| 195 | ce_stage = 2; |
| 196 | |
| 197 | ce = xcalloc(1, ce_size); |
| 198 | ce->ce_mode = create_ce_mode(posns[i]->mode); |
| 199 | ce->ce_flags = create_ce_flags(baselen + pathlen, |
| 200 | ce_stage); |
| 201 | memcpy(ce->name, base, baselen); |
| 202 | memcpy(ce->name + baselen, first, pathlen + 1); |
| 203 | |
| 204 | any_files = 1; |
| 205 | |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 206 | hashcpy(ce->sha1, posns[i]->sha1); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 207 | src[i + o->merge] = ce; |
| 208 | subposns[i] = df_conflict_list; |
| 209 | posns[i] = posns[i]->next; |
| 210 | } |
| 211 | if (any_files) { |
| 212 | if (o->merge) { |
| 213 | int ret; |
| 214 | |
| 215 | #if DBRT_DEBUG > 1 |
| 216 | printf("%s:\n", first); |
| 217 | for (i = 0; i < src_size; i++) { |
| 218 | printf(" %d ", i); |
| 219 | if (src[i]) |
| 220 | printf("%s\n", sha1_to_hex(src[i]->sha1)); |
| 221 | else |
| 222 | printf("\n"); |
| 223 | } |
| 224 | #endif |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 225 | ret = o->fn(src, o, remove); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 226 | |
| 227 | #if DBRT_DEBUG > 1 |
| 228 | printf("Added %d entries\n", ret); |
| 229 | #endif |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 230 | o->pos += ret; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 231 | } else { |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 232 | remove_entry(remove); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 233 | for (i = 0; i < src_size; i++) { |
| 234 | if (src[i]) { |
| 235 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | if (any_dirs) { |
| 241 | char *newbase = xmalloc(baselen + 2 + pathlen); |
| 242 | memcpy(newbase, base, baselen); |
| 243 | memcpy(newbase + baselen, first, pathlen); |
| 244 | newbase[baselen + pathlen] = '/'; |
| 245 | newbase[baselen + pathlen + 1] = '\0'; |
| 246 | if (unpack_trees_rec(subposns, len, newbase, o, |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 247 | df_conflict_list)) { |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 248 | retval = -1; |
| 249 | goto leave_directory; |
| 250 | } |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 251 | free(newbase); |
| 252 | } |
| 253 | free(subposns); |
| 254 | free(src); |
| 255 | } while (1); |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 256 | |
| 257 | leave_directory: |
| 258 | if (o->dir) |
| 259 | pop_exclude_per_directory(o->dir, i_stk); |
| 260 | return retval; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* Unlink the last component and attempt to remove leading |
| 264 | * directories, in case this unlink is the removal of the |
| 265 | * last entry in the directory -- empty directories are removed. |
| 266 | */ |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 267 | static void unlink_entry(char *name, char *last_symlink) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 268 | { |
| 269 | char *cp, *prev; |
| 270 | |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 271 | if (has_symlink_leading_path(name, last_symlink)) |
| 272 | return; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 273 | if (unlink(name)) |
| 274 | return; |
| 275 | prev = NULL; |
| 276 | while (1) { |
| 277 | int status; |
| 278 | cp = strrchr(name, '/'); |
| 279 | if (prev) |
| 280 | *prev = '/'; |
| 281 | if (!cp) |
| 282 | break; |
| 283 | |
| 284 | *cp = 0; |
| 285 | status = rmdir(name); |
| 286 | if (status) { |
| 287 | *cp = '/'; |
| 288 | break; |
| 289 | } |
| 290 | prev = cp; |
| 291 | } |
| 292 | } |
| 293 | |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 294 | static struct checkout state; |
| 295 | static void check_updates(struct cache_entry **src, int nr, |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 296 | struct unpack_trees_options *o) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 297 | { |
| 298 | unsigned short mask = htons(CE_UPDATE); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 299 | unsigned cnt = 0, total = 0; |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame^] | 300 | struct progress *progress = NULL; |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 301 | char last_symlink[PATH_MAX]; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 302 | |
| 303 | if (o->update && o->verbose_update) { |
| 304 | for (total = cnt = 0; cnt < nr; cnt++) { |
| 305 | struct cache_entry *ce = src[cnt]; |
| 306 | if (!ce->ce_mode || ce->ce_flags & mask) |
| 307 | total++; |
| 308 | } |
| 309 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame^] | 310 | progress = start_progress_delay("Checking out files", |
| 311 | total, 50, 2); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 312 | cnt = 0; |
| 313 | } |
| 314 | |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 315 | *last_symlink = '\0'; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 316 | while (nr--) { |
| 317 | struct cache_entry *ce = *src++; |
| 318 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 319 | if (total) |
| 320 | if (!ce->ce_mode || ce->ce_flags & mask) |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame^] | 321 | display_progress(progress, ++cnt); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 322 | if (!ce->ce_mode) { |
| 323 | if (o->update) |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 324 | unlink_entry(ce->name, last_symlink); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | if (ce->ce_flags & mask) { |
| 328 | ce->ce_flags &= ~mask; |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 329 | if (o->update) { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 330 | checkout_entry(ce, &state, NULL); |
Junio C Hamano | 16a4c61 | 2007-05-10 23:44:53 -0700 | [diff] [blame] | 331 | *last_symlink = '\0'; |
| 332 | } |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 333 | } |
| 334 | } |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 335 | if (total) |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame^] | 336 | stop_progress(&progress); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 339 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 340 | { |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 341 | struct tree_entry_list **posns; |
| 342 | int i; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 343 | struct tree_entry_list df_conflict_list; |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 344 | static struct cache_entry *dfc; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 345 | |
| 346 | memset(&df_conflict_list, 0, sizeof(df_conflict_list)); |
| 347 | df_conflict_list.next = &df_conflict_list; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 348 | memset(&state, 0, sizeof(state)); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 349 | state.base_dir = ""; |
| 350 | state.force = 1; |
| 351 | state.quiet = 1; |
| 352 | state.refresh_cache = 1; |
| 353 | |
| 354 | o->merge_size = len; |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 355 | |
| 356 | if (!dfc) |
| 357 | dfc = xcalloc(1, sizeof(struct cache_entry) + 1); |
| 358 | o->df_conflict_entry = dfc; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 359 | |
| 360 | if (len) { |
| 361 | posns = xmalloc(len * sizeof(struct tree_entry_list *)); |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 362 | for (i = 0; i < len; i++) |
| 363 | posns[i] = create_tree_entry_list(t+i); |
| 364 | |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 365 | if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "", |
Junio C Hamano | 9a4d8fd | 2007-04-02 15:06:59 -0700 | [diff] [blame] | 366 | o, &df_conflict_list)) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | if (o->trivial_merges_only && o->nontrivial_merge) |
| 371 | die("Merge requires file-level merging"); |
| 372 | |
| 373 | check_updates(active_cache, active_nr, o); |
| 374 | return 0; |
| 375 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 376 | |
| 377 | /* Here come the merge functions */ |
| 378 | |
| 379 | static void reject_merge(struct cache_entry *ce) |
| 380 | { |
| 381 | die("Entry '%s' would be overwritten by merge. Cannot merge.", |
| 382 | ce->name); |
| 383 | } |
| 384 | |
| 385 | static int same(struct cache_entry *a, struct cache_entry *b) |
| 386 | { |
| 387 | if (!!a != !!b) |
| 388 | return 0; |
| 389 | if (!a && !b) |
| 390 | return 1; |
| 391 | return a->ce_mode == b->ce_mode && |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 392 | !hashcmp(a->sha1, b->sha1); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | /* |
| 397 | * When a CE gets turned into an unmerged entry, we |
| 398 | * want it to be up-to-date |
| 399 | */ |
| 400 | static void verify_uptodate(struct cache_entry *ce, |
| 401 | struct unpack_trees_options *o) |
| 402 | { |
| 403 | struct stat st; |
| 404 | |
| 405 | if (o->index_only || o->reset) |
| 406 | return; |
| 407 | |
| 408 | if (!lstat(ce->name, &st)) { |
| 409 | unsigned changed = ce_match_stat(ce, &st, 1); |
| 410 | if (!changed) |
| 411 | return; |
Junio C Hamano | 936492d | 2007-08-03 22:13:09 -0700 | [diff] [blame] | 412 | /* |
| 413 | * NEEDSWORK: the current default policy is to allow |
| 414 | * submodule to be out of sync wrt the supermodule |
| 415 | * index. This needs to be tightened later for |
| 416 | * submodules that are marked to be automatically |
| 417 | * checked out. |
| 418 | */ |
| 419 | if (S_ISGITLINK(ntohl(ce->ce_mode))) |
| 420 | return; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 421 | errno = 0; |
| 422 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 423 | if (errno == ENOENT) |
| 424 | return; |
| 425 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); |
| 426 | } |
| 427 | |
| 428 | static void invalidate_ce_path(struct cache_entry *ce) |
| 429 | { |
| 430 | if (ce) |
| 431 | cache_tree_invalidate_path(active_cache_tree, ce->name); |
| 432 | } |
| 433 | |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 434 | /* |
| 435 | * Check that checking out ce->sha1 in subdir ce->name is not |
| 436 | * going to overwrite any working files. |
| 437 | * |
| 438 | * Currently, git does not checkout subprojects during a superproject |
| 439 | * checkout, so it is not going to overwrite anything. |
| 440 | */ |
| 441 | static int verify_clean_submodule(struct cache_entry *ce, const char *action, |
| 442 | struct unpack_trees_options *o) |
| 443 | { |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int verify_clean_subdirectory(struct cache_entry *ce, const char *action, |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 448 | struct unpack_trees_options *o) |
| 449 | { |
| 450 | /* |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 451 | * we are about to extract "ce->name"; we would not want to lose |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 452 | * anything in the existing directory there. |
| 453 | */ |
| 454 | int namelen; |
| 455 | int pos, i; |
| 456 | struct dir_struct d; |
| 457 | char *pathbuf; |
| 458 | int cnt = 0; |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 459 | unsigned char sha1[20]; |
| 460 | |
| 461 | if (S_ISGITLINK(ntohl(ce->ce_mode)) && |
| 462 | resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) { |
| 463 | /* If we are not going to update the submodule, then |
| 464 | * we don't care. |
| 465 | */ |
| 466 | if (!hashcmp(sha1, ce->sha1)) |
| 467 | return 0; |
| 468 | return verify_clean_submodule(ce, action, o); |
| 469 | } |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * First let's make sure we do not have a local modification |
| 473 | * in that directory. |
| 474 | */ |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 475 | namelen = strlen(ce->name); |
| 476 | pos = cache_name_pos(ce->name, namelen); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 477 | if (0 <= pos) |
| 478 | return cnt; /* we have it as nondirectory */ |
| 479 | pos = -pos - 1; |
| 480 | for (i = pos; i < active_nr; i++) { |
| 481 | struct cache_entry *ce = active_cache[i]; |
| 482 | int len = ce_namelen(ce); |
| 483 | if (len < namelen || |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 484 | strncmp(ce->name, ce->name, namelen) || |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 485 | ce->name[namelen] != '/') |
| 486 | break; |
| 487 | /* |
| 488 | * ce->name is an entry in the subdirectory. |
| 489 | */ |
| 490 | if (!ce_stage(ce)) { |
| 491 | verify_uptodate(ce, o); |
| 492 | ce->ce_mode = 0; |
| 493 | } |
| 494 | cnt++; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Then we need to make sure that we do not lose a locally |
| 499 | * present file that is not ignored. |
| 500 | */ |
| 501 | pathbuf = xmalloc(namelen + 2); |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 502 | memcpy(pathbuf, ce->name, namelen); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 503 | strcpy(pathbuf+namelen, "/"); |
| 504 | |
| 505 | memset(&d, 0, sizeof(d)); |
| 506 | if (o->dir) |
| 507 | d.exclude_per_dir = o->dir->exclude_per_dir; |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 508 | i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 509 | if (i) |
| 510 | die("Updating '%s' would lose untracked files in it", |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 511 | ce->name); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 512 | free(pathbuf); |
| 513 | return cnt; |
| 514 | } |
| 515 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 516 | /* |
| 517 | * We do not want to remove or overwrite a working tree file that |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 518 | * is not tracked, unless it is ignored. |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 519 | */ |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 520 | static void verify_absent(struct cache_entry *ce, const char *action, |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 521 | struct unpack_trees_options *o) |
| 522 | { |
| 523 | struct stat st; |
| 524 | |
| 525 | if (o->index_only || o->reset || !o->update) |
| 526 | return; |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 527 | |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 528 | if (has_symlink_leading_path(ce->name, NULL)) |
Junio C Hamano | ec0603e | 2007-07-12 01:04:16 -0700 | [diff] [blame] | 529 | return; |
| 530 | |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 531 | if (!lstat(ce->name, &st)) { |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 532 | int cnt; |
| 533 | |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 534 | if (o->dir && excluded(o->dir, ce->name)) |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 535 | /* |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 536 | * ce->name is explicitly excluded, so it is Ok to |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 537 | * overwrite it. |
| 538 | */ |
| 539 | return; |
| 540 | if (S_ISDIR(st.st_mode)) { |
| 541 | /* |
| 542 | * We are checking out path "foo" and |
| 543 | * found "foo/." in the working tree. |
| 544 | * This is tricky -- if we have modified |
| 545 | * files that are in "foo/" we would lose |
| 546 | * it. |
| 547 | */ |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 548 | cnt = verify_clean_subdirectory(ce, action, o); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 549 | |
| 550 | /* |
| 551 | * If this removed entries from the index, |
| 552 | * what that means is: |
| 553 | * |
| 554 | * (1) the caller unpack_trees_rec() saw path/foo |
| 555 | * in the index, and it has not removed it because |
| 556 | * it thinks it is handling 'path' as blob with |
| 557 | * D/F conflict; |
| 558 | * (2) we will return "ok, we placed a merged entry |
| 559 | * in the index" which would cause o->pos to be |
| 560 | * incremented by one; |
| 561 | * (3) however, original o->pos now has 'path/foo' |
| 562 | * marked with "to be removed". |
| 563 | * |
| 564 | * We need to increment it by the number of |
| 565 | * deleted entries here. |
| 566 | */ |
| 567 | o->pos += cnt; |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * The previous round may already have decided to |
| 573 | * delete this path, which is in a subdirectory that |
| 574 | * is being replaced with a blob. |
| 575 | */ |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 576 | cnt = cache_name_pos(ce->name, strlen(ce->name)); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 577 | if (0 <= cnt) { |
| 578 | struct cache_entry *ce = active_cache[cnt]; |
| 579 | if (!ce_stage(ce) && !ce->ce_mode) |
| 580 | return; |
| 581 | } |
| 582 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 583 | die("Untracked working tree file '%s' " |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 584 | "would be %s by merge.", ce->name, action); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 585 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, |
| 589 | struct unpack_trees_options *o) |
| 590 | { |
| 591 | merge->ce_flags |= htons(CE_UPDATE); |
| 592 | if (old) { |
| 593 | /* |
| 594 | * See if we can re-use the old CE directly? |
| 595 | * That way we get the uptodate stat info. |
| 596 | * |
| 597 | * This also removes the UPDATE flag on |
| 598 | * a match. |
| 599 | */ |
| 600 | if (same(old, merge)) { |
| 601 | *merge = *old; |
| 602 | } else { |
| 603 | verify_uptodate(old, o); |
| 604 | invalidate_ce_path(old); |
| 605 | } |
| 606 | } |
| 607 | else { |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 608 | verify_absent(merge, "overwritten", o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 609 | invalidate_ce_path(merge); |
| 610 | } |
| 611 | |
| 612 | merge->ce_flags &= ~htons(CE_STAGEMASK); |
| 613 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
| 614 | return 1; |
| 615 | } |
| 616 | |
| 617 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, |
| 618 | struct unpack_trees_options *o) |
| 619 | { |
| 620 | if (old) |
| 621 | verify_uptodate(old, o); |
| 622 | else |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 623 | verify_absent(ce, "removed", o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 624 | ce->ce_mode = 0; |
| 625 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
| 626 | invalidate_ce_path(ce); |
| 627 | return 1; |
| 628 | } |
| 629 | |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 630 | static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 631 | { |
| 632 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
| 633 | return 1; |
| 634 | } |
| 635 | |
| 636 | #if DBRT_DEBUG |
| 637 | static void show_stage_entry(FILE *o, |
| 638 | const char *label, const struct cache_entry *ce) |
| 639 | { |
| 640 | if (!ce) |
| 641 | fprintf(o, "%s (missing)\n", label); |
| 642 | else |
| 643 | fprintf(o, "%s%06o %s %d\t%s\n", |
| 644 | label, |
| 645 | ntohl(ce->ce_mode), |
| 646 | sha1_to_hex(ce->sha1), |
| 647 | ce_stage(ce), |
| 648 | ce->name); |
| 649 | } |
| 650 | #endif |
| 651 | |
| 652 | int threeway_merge(struct cache_entry **stages, |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 653 | struct unpack_trees_options *o, |
| 654 | int remove) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 655 | { |
| 656 | struct cache_entry *index; |
| 657 | struct cache_entry *head; |
| 658 | struct cache_entry *remote = stages[o->head_idx + 1]; |
| 659 | int count; |
| 660 | int head_match = 0; |
| 661 | int remote_match = 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 662 | |
| 663 | int df_conflict_head = 0; |
| 664 | int df_conflict_remote = 0; |
| 665 | |
| 666 | int any_anc_missing = 0; |
| 667 | int no_anc_exists = 1; |
| 668 | int i; |
| 669 | |
| 670 | for (i = 1; i < o->head_idx; i++) { |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 671 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 672 | any_anc_missing = 1; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 673 | else |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 674 | no_anc_exists = 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | index = stages[0]; |
| 678 | head = stages[o->head_idx]; |
| 679 | |
| 680 | if (head == o->df_conflict_entry) { |
| 681 | df_conflict_head = 1; |
| 682 | head = NULL; |
| 683 | } |
| 684 | |
| 685 | if (remote == o->df_conflict_entry) { |
| 686 | df_conflict_remote = 1; |
| 687 | remote = NULL; |
| 688 | } |
| 689 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 690 | /* First, if there's a #16 situation, note that to prevent #13 |
| 691 | * and #14. |
| 692 | */ |
| 693 | if (!same(remote, head)) { |
| 694 | for (i = 1; i < o->head_idx; i++) { |
| 695 | if (same(stages[i], head)) { |
| 696 | head_match = i; |
| 697 | } |
| 698 | if (same(stages[i], remote)) { |
| 699 | remote_match = i; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* We start with cases where the index is allowed to match |
| 705 | * something other than the head: #14(ALT) and #2ALT, where it |
| 706 | * is permitted to match the result instead. |
| 707 | */ |
| 708 | /* #14, #14ALT, #2ALT */ |
| 709 | if (remote && !df_conflict_head && head_match && !remote_match) { |
| 710 | if (index && !same(index, remote) && !same(index, head)) |
| 711 | reject_merge(index); |
| 712 | return merged_entry(remote, index, o); |
| 713 | } |
| 714 | /* |
| 715 | * If we have an entry in the index cache, then we want to |
| 716 | * make sure that it matches head. |
| 717 | */ |
| 718 | if (index && !same(index, head)) { |
| 719 | reject_merge(index); |
| 720 | } |
| 721 | |
| 722 | if (head) { |
| 723 | /* #5ALT, #15 */ |
| 724 | if (same(head, remote)) |
| 725 | return merged_entry(head, index, o); |
| 726 | /* #13, #3ALT */ |
| 727 | if (!df_conflict_remote && remote_match && !head_match) |
| 728 | return merged_entry(head, index, o); |
| 729 | } |
| 730 | |
| 731 | /* #1 */ |
Linus Torvalds | 566b5c0 | 2007-08-10 12:53:51 -0700 | [diff] [blame] | 732 | if (!head && !remote && any_anc_missing) { |
| 733 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 734 | return 0; |
Linus Torvalds | 566b5c0 | 2007-08-10 12:53:51 -0700 | [diff] [blame] | 735 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 736 | |
| 737 | /* Under the new "aggressive" rule, we resolve mostly trivial |
| 738 | * cases that we historically had git-merge-one-file resolve. |
| 739 | */ |
| 740 | if (o->aggressive) { |
| 741 | int head_deleted = !head && !df_conflict_head; |
| 742 | int remote_deleted = !remote && !df_conflict_remote; |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 743 | struct cache_entry *ce = NULL; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 744 | |
| 745 | if (index) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 746 | ce = index; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 747 | else if (head) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 748 | ce = head; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 749 | else if (remote) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 750 | ce = remote; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 751 | else { |
| 752 | for (i = 1; i < o->head_idx; i++) { |
| 753 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 754 | ce = stages[i]; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Deleted in both. |
| 762 | * Deleted in one and unchanged in the other. |
| 763 | */ |
| 764 | if ((head_deleted && remote_deleted) || |
| 765 | (head_deleted && remote && remote_match) || |
| 766 | (remote_deleted && head && head_match)) { |
Linus Torvalds | 566b5c0 | 2007-08-10 12:53:51 -0700 | [diff] [blame] | 767 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 768 | if (index) |
| 769 | return deleted_entry(index, index, o); |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 770 | else if (ce && !head_deleted) |
| 771 | verify_absent(ce, "removed", o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | /* |
| 775 | * Added in both, identically. |
| 776 | */ |
| 777 | if (no_anc_exists && head && remote && same(head, remote)) |
| 778 | return merged_entry(head, index, o); |
| 779 | |
| 780 | } |
| 781 | |
| 782 | /* Below are "no merge" cases, which require that the index be |
| 783 | * up-to-date to avoid the files getting overwritten with |
| 784 | * conflict resolution files. |
| 785 | */ |
| 786 | if (index) { |
| 787 | verify_uptodate(index, o); |
| 788 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 789 | |
Linus Torvalds | 566b5c0 | 2007-08-10 12:53:51 -0700 | [diff] [blame] | 790 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 791 | o->nontrivial_merge = 1; |
| 792 | |
Junio C Hamano | ea4b52a | 2007-04-07 05:42:01 -0700 | [diff] [blame] | 793 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 794 | count = 0; |
| 795 | if (!head_match || !remote_match) { |
| 796 | for (i = 1; i < o->head_idx; i++) { |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 797 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 798 | keep_entry(stages[i], o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 799 | count++; |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | #if DBRT_DEBUG |
| 805 | else { |
| 806 | fprintf(stderr, "read-tree: warning #16 detected\n"); |
| 807 | show_stage_entry(stderr, "head ", stages[head_match]); |
| 808 | show_stage_entry(stderr, "remote ", stages[remote_match]); |
| 809 | } |
| 810 | #endif |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 811 | if (head) { count += keep_entry(head, o); } |
| 812 | if (remote) { count += keep_entry(remote, o); } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 813 | return count; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Two-way merge. |
| 818 | * |
| 819 | * The rule is to "carry forward" what is in the index without losing |
| 820 | * information across a "fast forward", favoring a successful merge |
| 821 | * over a merge failure when it makes sense. For details of the |
| 822 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. |
| 823 | * |
| 824 | */ |
| 825 | int twoway_merge(struct cache_entry **src, |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 826 | struct unpack_trees_options *o, |
| 827 | int remove) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 828 | { |
| 829 | struct cache_entry *current = src[0]; |
Junio C Hamano | b8ba153 | 2007-04-02 16:29:56 -0700 | [diff] [blame] | 830 | struct cache_entry *oldtree = src[1]; |
| 831 | struct cache_entry *newtree = src[2]; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 832 | |
| 833 | if (o->merge_size != 2) |
| 834 | return error("Cannot do a twoway merge of %d trees", |
| 835 | o->merge_size); |
| 836 | |
Junio C Hamano | b8ba153 | 2007-04-02 16:29:56 -0700 | [diff] [blame] | 837 | if (oldtree == o->df_conflict_entry) |
| 838 | oldtree = NULL; |
| 839 | if (newtree == o->df_conflict_entry) |
| 840 | newtree = NULL; |
| 841 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 842 | if (current) { |
| 843 | if ((!oldtree && !newtree) || /* 4 and 5 */ |
| 844 | (!oldtree && newtree && |
| 845 | same(current, newtree)) || /* 6 and 7 */ |
| 846 | (oldtree && newtree && |
| 847 | same(oldtree, newtree)) || /* 14 and 15 */ |
| 848 | (oldtree && newtree && |
Junio C Hamano | b8ba153 | 2007-04-02 16:29:56 -0700 | [diff] [blame] | 849 | !same(oldtree, newtree) && /* 18 and 19 */ |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 850 | same(current, newtree))) { |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 851 | return keep_entry(current, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 852 | } |
| 853 | else if (oldtree && !newtree && same(current, oldtree)) { |
| 854 | /* 10 or 11 */ |
Linus Torvalds | d699676 | 2007-08-10 12:31:20 -0700 | [diff] [blame] | 855 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 856 | return deleted_entry(oldtree, current, o); |
| 857 | } |
| 858 | else if (oldtree && newtree && |
| 859 | same(current, oldtree) && !same(current, newtree)) { |
| 860 | /* 20 or 21 */ |
| 861 | return merged_entry(newtree, current, o); |
| 862 | } |
| 863 | else { |
| 864 | /* all other failures */ |
Linus Torvalds | d699676 | 2007-08-10 12:31:20 -0700 | [diff] [blame] | 865 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 866 | if (oldtree) |
| 867 | reject_merge(oldtree); |
| 868 | if (current) |
| 869 | reject_merge(current); |
| 870 | if (newtree) |
| 871 | reject_merge(newtree); |
| 872 | return -1; |
| 873 | } |
| 874 | } |
| 875 | else if (newtree) |
| 876 | return merged_entry(newtree, current, o); |
Linus Torvalds | d699676 | 2007-08-10 12:31:20 -0700 | [diff] [blame] | 877 | remove_entry(remove); |
| 878 | return deleted_entry(oldtree, current, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Bind merge. |
| 883 | * |
| 884 | * Keep the index entries at stage0, collapse stage1 but make sure |
| 885 | * stage0 does not have anything there. |
| 886 | */ |
| 887 | int bind_merge(struct cache_entry **src, |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 888 | struct unpack_trees_options *o, |
| 889 | int remove) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 890 | { |
| 891 | struct cache_entry *old = src[0]; |
| 892 | struct cache_entry *a = src[1]; |
| 893 | |
| 894 | if (o->merge_size != 1) |
| 895 | return error("Cannot do a bind merge of %d trees\n", |
| 896 | o->merge_size); |
| 897 | if (a && old) |
| 898 | die("Entry '%s' overlaps. Cannot bind.", a->name); |
| 899 | if (!a) |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 900 | return keep_entry(old, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 901 | else |
| 902 | return merged_entry(a, NULL, o); |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * One-way merge. |
| 907 | * |
| 908 | * The rule is: |
| 909 | * - take the stat information from stage0, take the data from stage1 |
| 910 | */ |
| 911 | int oneway_merge(struct cache_entry **src, |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 912 | struct unpack_trees_options *o, |
| 913 | int remove) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 914 | { |
| 915 | struct cache_entry *old = src[0]; |
| 916 | struct cache_entry *a = src[1]; |
| 917 | |
| 918 | if (o->merge_size != 1) |
| 919 | return error("Cannot do a oneway merge of %d trees", |
| 920 | o->merge_size); |
| 921 | |
Linus Torvalds | 288f072 | 2007-08-10 12:21:20 -0700 | [diff] [blame] | 922 | if (!a) { |
| 923 | remove_entry(remove); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 924 | return deleted_entry(old, old, o); |
Linus Torvalds | 288f072 | 2007-08-10 12:21:20 -0700 | [diff] [blame] | 925 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 926 | if (old && same(old, a)) { |
| 927 | if (o->reset) { |
| 928 | struct stat st; |
| 929 | if (lstat(old->name, &st) || |
| 930 | ce_match_stat(old, &st, 1)) |
| 931 | old->ce_flags |= htons(CE_UPDATE); |
| 932 | } |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 933 | return keep_entry(old, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 934 | } |
| 935 | return merged_entry(a, old, o); |
| 936 | } |