Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 2 | #include "tree-walk.h" |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 3 | #include "xdiff-interface.h" |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 4 | #include "blob.h" |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 5 | |
| 6 | static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>"; |
| 7 | static int resolve_directories = 1; |
| 8 | |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 9 | struct merge_list { |
| 10 | struct merge_list *next; |
| 11 | struct merge_list *link; /* other stages for this object */ |
| 12 | |
| 13 | unsigned int stage : 2, |
| 14 | flags : 30; |
| 15 | unsigned int mode; |
| 16 | const char *path; |
| 17 | struct blob *blob; |
| 18 | }; |
| 19 | |
| 20 | static struct merge_list *merge_result, **merge_result_end = &merge_result; |
| 21 | |
| 22 | static void add_merge_entry(struct merge_list *entry) |
| 23 | { |
| 24 | *merge_result_end = entry; |
| 25 | merge_result_end = &entry->next; |
| 26 | } |
| 27 | |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 28 | static void merge_trees(struct tree_desc t[3], const char *base); |
| 29 | |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 30 | static const char *explanation(struct merge_list *entry) |
| 31 | { |
| 32 | switch (entry->stage) { |
| 33 | case 0: |
| 34 | return "merged"; |
| 35 | case 3: |
| 36 | return "added in remote"; |
| 37 | case 2: |
| 38 | if (entry->link) |
| 39 | return "added in both"; |
| 40 | return "added in local"; |
| 41 | } |
| 42 | |
| 43 | /* Existed in base */ |
| 44 | entry = entry->link; |
| 45 | if (!entry) |
| 46 | return "removed in both"; |
| 47 | |
| 48 | if (entry->link) |
| 49 | return "changed in both"; |
| 50 | |
| 51 | if (entry->stage == 3) |
| 52 | return "removed in local"; |
| 53 | return "removed in remote"; |
| 54 | } |
| 55 | |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 56 | extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned long *); |
| 57 | |
| 58 | static void *result(struct merge_list *entry, unsigned long *size) |
| 59 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 60 | enum object_type type; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 61 | struct blob *base, *our, *their; |
| 62 | |
| 63 | if (!entry->stage) |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 64 | return read_sha1_file(entry->blob->object.sha1, &type, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 65 | base = NULL; |
| 66 | if (entry->stage == 1) { |
| 67 | base = entry->blob; |
| 68 | entry = entry->link; |
| 69 | } |
| 70 | our = NULL; |
| 71 | if (entry && entry->stage == 2) { |
| 72 | our = entry->blob; |
| 73 | entry = entry->link; |
| 74 | } |
| 75 | their = NULL; |
| 76 | if (entry) |
| 77 | their = entry->blob; |
| 78 | return merge_file(base, our, their, size); |
| 79 | } |
| 80 | |
| 81 | static void *origin(struct merge_list *entry, unsigned long *size) |
| 82 | { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 83 | enum object_type type; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 84 | while (entry) { |
| 85 | if (entry->stage == 2) |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 86 | return read_sha1_file(entry->blob->object.sha1, &type, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 87 | entry = entry->link; |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf) |
| 93 | { |
| 94 | int i; |
| 95 | for (i = 0; i < nbuf; i++) |
| 96 | printf("%.*s", (int) mb[i].size, mb[i].ptr); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static void show_diff(struct merge_list *entry) |
| 101 | { |
| 102 | unsigned long size; |
| 103 | mmfile_t src, dst; |
| 104 | xpparam_t xpp; |
| 105 | xdemitconf_t xecfg; |
| 106 | xdemitcb_t ecb; |
| 107 | |
| 108 | xpp.flags = XDF_NEED_MINIMAL; |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 109 | memset(&xecfg, 0, sizeof(xecfg)); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 110 | xecfg.ctxlen = 3; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 111 | ecb.outf = show_outf; |
| 112 | ecb.priv = NULL; |
| 113 | |
| 114 | src.ptr = origin(entry, &size); |
| 115 | if (!src.ptr) |
| 116 | size = 0; |
| 117 | src.size = size; |
| 118 | dst.ptr = result(entry, &size); |
| 119 | if (!dst.ptr) |
| 120 | size = 0; |
| 121 | dst.size = size; |
Junio C Hamano | c279d7e | 2007-12-13 13:25:07 -0800 | [diff] [blame] | 122 | xdi_diff(&src, &dst, &xpp, &xecfg, &ecb); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 123 | free(src.ptr); |
| 124 | free(dst.ptr); |
| 125 | } |
| 126 | |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 127 | static void show_result_list(struct merge_list *entry) |
| 128 | { |
| 129 | printf("%s\n", explanation(entry)); |
| 130 | do { |
| 131 | struct merge_list *link = entry->link; |
| 132 | static const char *desc[4] = { "result", "base", "our", "their" }; |
| 133 | printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path); |
| 134 | entry = link; |
| 135 | } while (entry); |
| 136 | } |
| 137 | |
| 138 | static void show_result(void) |
| 139 | { |
| 140 | struct merge_list *walk; |
| 141 | |
| 142 | walk = merge_result; |
| 143 | while (walk) { |
| 144 | show_result_list(walk); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 145 | show_diff(walk); |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 146 | walk = walk->next; |
| 147 | } |
| 148 | } |
| 149 | |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 150 | /* An empty entry never compares same, not even to another empty entry */ |
| 151 | static int same_entry(struct name_entry *a, struct name_entry *b) |
| 152 | { |
| 153 | return a->sha1 && |
| 154 | b->sha1 && |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 155 | !hashcmp(a->sha1, b->sha1) && |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 156 | a->mode == b->mode; |
| 157 | } |
| 158 | |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 159 | static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 160 | { |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 161 | struct merge_list *res = xmalloc(sizeof(*res)); |
| 162 | |
| 163 | memset(res, 0, sizeof(*res)); |
| 164 | res->stage = stage; |
| 165 | res->path = path; |
| 166 | res->mode = mode; |
| 167 | res->blob = lookup_blob(sha1); |
| 168 | return res; |
Linus Torvalds | 01df529 | 2006-02-14 18:33:02 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 171 | static char *traverse_path(const struct traverse_info *info, const struct name_entry *n) |
| 172 | { |
| 173 | char *path = xmalloc(traverse_path_len(info, n) + 1); |
| 174 | return make_traverse_path(path, info, n); |
| 175 | } |
| 176 | |
| 177 | static void resolve(const struct traverse_info *info, struct name_entry *branch1, struct name_entry *result) |
Linus Torvalds | 01df529 | 2006-02-14 18:33:02 -0800 | [diff] [blame] | 178 | { |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 179 | struct merge_list *orig, *final; |
| 180 | const char *path; |
| 181 | |
Linus Torvalds | 01df529 | 2006-02-14 18:33:02 -0800 | [diff] [blame] | 182 | /* If it's already branch1, don't bother showing it */ |
| 183 | if (!branch1) |
| 184 | return; |
Linus Torvalds | 01df529 | 2006-02-14 18:33:02 -0800 | [diff] [blame] | 185 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 186 | path = traverse_path(info, result); |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 187 | orig = create_entry(2, branch1->mode, branch1->sha1, path); |
| 188 | final = create_entry(0, result->mode, result->sha1, path); |
| 189 | |
| 190 | final->link = orig; |
| 191 | |
| 192 | add_merge_entry(final); |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 195 | static int unresolved_directory(const struct traverse_info *info, struct name_entry n[3]) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 196 | { |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 197 | char *newbase; |
| 198 | struct name_entry *p; |
| 199 | struct tree_desc t[3]; |
| 200 | void *buf0, *buf1, *buf2; |
| 201 | |
| 202 | if (!resolve_directories) |
| 203 | return 0; |
| 204 | p = n; |
| 205 | if (!p->mode) { |
| 206 | p++; |
| 207 | if (!p->mode) |
| 208 | p++; |
| 209 | } |
| 210 | if (!S_ISDIR(p->mode)) |
| 211 | return 0; |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 212 | newbase = traverse_path(info, p); |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 213 | buf0 = fill_tree_descriptor(t+0, n[0].sha1); |
| 214 | buf1 = fill_tree_descriptor(t+1, n[1].sha1); |
| 215 | buf2 = fill_tree_descriptor(t+2, n[2].sha1); |
| 216 | merge_trees(t, newbase); |
| 217 | |
| 218 | free(buf0); |
| 219 | free(buf1); |
| 220 | free(buf2); |
| 221 | free(newbase); |
| 222 | return 1; |
| 223 | } |
| 224 | |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 225 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 226 | static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry) |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 227 | { |
| 228 | const char *path; |
| 229 | struct merge_list *link; |
| 230 | |
| 231 | if (!n->mode) |
| 232 | return entry; |
| 233 | if (entry) |
| 234 | path = entry->path; |
| 235 | else |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 236 | path = traverse_path(info, n); |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 237 | link = create_entry(stage, n->mode, n->sha1, path); |
| 238 | link->link = entry; |
| 239 | return link; |
| 240 | } |
| 241 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 242 | static void unresolved(const struct traverse_info *info, struct name_entry n[3]) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 243 | { |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 244 | struct merge_list *entry = NULL; |
| 245 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 246 | if (unresolved_directory(info, n)) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 247 | return; |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * Do them in reverse order so that the resulting link |
| 251 | * list has the stages in order - link_entry adds new |
| 252 | * links at the front. |
| 253 | */ |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 254 | entry = link_entry(3, info, n + 2, entry); |
| 255 | entry = link_entry(2, info, n + 1, entry); |
| 256 | entry = link_entry(1, info, n + 0, entry); |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 257 | |
| 258 | add_merge_entry(entry); |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 261 | /* |
| 262 | * Merge two trees together (t[1] and t[2]), using a common base (t[0]) |
| 263 | * as the origin. |
| 264 | * |
| 265 | * This walks the (sorted) trees in lock-step, checking every possible |
| 266 | * name. Note that directories automatically sort differently from other |
| 267 | * files (see "base_name_compare"), so you'll never see file/directory |
| 268 | * conflicts, because they won't ever compare the same. |
| 269 | * |
| 270 | * IOW, if a directory changes to a filename, it will automatically be |
| 271 | * seen as the directory going away, and the filename being created. |
| 272 | * |
| 273 | * Think of this as a three-way diff. |
| 274 | * |
| 275 | * The output will be either: |
| 276 | * - successful merge |
| 277 | * "0 mode sha1 filename" |
| 278 | * NOTE NOTE NOTE! FIXME! We really really need to walk the index |
| 279 | * in parallel with this too! |
| 280 | * |
| 281 | * - conflict: |
| 282 | * "1 mode sha1 filename" |
| 283 | * "2 mode sha1 filename" |
| 284 | * "3 mode sha1 filename" |
| 285 | * where not all of the 1/2/3 lines may exist, of course. |
| 286 | * |
| 287 | * The successful merge rules are the same as for the three-way merge |
| 288 | * in git-read-tree. |
| 289 | */ |
Linus Torvalds | 91e4f03 | 2008-03-05 20:06:18 -0800 | [diff] [blame] | 290 | static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info) |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 291 | { |
| 292 | /* Same in both? */ |
| 293 | if (same_entry(entry+1, entry+2)) { |
| 294 | if (entry[0].sha1) { |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 295 | resolve(info, NULL, entry+1); |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 296 | return mask; |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | if (same_entry(entry+0, entry+1)) { |
| 301 | if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) { |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 302 | resolve(info, entry+1, entry+2); |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 303 | return mask; |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | if (same_entry(entry+0, entry+2)) { |
| 308 | if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) { |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 309 | resolve(info, NULL, entry+1); |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 310 | return mask; |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 314 | unresolved(info, entry); |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 315 | return mask; |
Linus Torvalds | 164dcb9 | 2006-02-15 19:25:32 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void merge_trees(struct tree_desc t[3], const char *base) |
| 319 | { |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 320 | struct traverse_info info; |
| 321 | |
| 322 | setup_traverse_info(&info, base); |
| 323 | info.fn = threeway_callback; |
| 324 | traverse_trees(3, t, &info); |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void *get_tree_descriptor(struct tree_desc *desc, const char *rev) |
| 328 | { |
| 329 | unsigned char sha1[20]; |
| 330 | void *buf; |
| 331 | |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 332 | if (get_sha1(rev, sha1)) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 333 | die("unknown rev %s", rev); |
| 334 | buf = fill_tree_descriptor(desc, sha1); |
| 335 | if (!buf) |
| 336 | die("%s is not a tree", rev); |
| 337 | return buf; |
| 338 | } |
| 339 | |
| 340 | int main(int argc, char **argv) |
| 341 | { |
| 342 | struct tree_desc t[3]; |
| 343 | void *buf1, *buf2, *buf3; |
| 344 | |
Dmitry V. Levin | 5b6df8e | 2006-09-14 05:04:09 +0400 | [diff] [blame] | 345 | if (argc != 4) |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 346 | usage(merge_tree_usage); |
| 347 | |
Dmitry V. Levin | 8112894 | 2006-09-14 05:03:59 +0400 | [diff] [blame] | 348 | setup_git_directory(); |
| 349 | |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 350 | buf1 = get_tree_descriptor(t+0, argv[1]); |
| 351 | buf2 = get_tree_descriptor(t+1, argv[2]); |
| 352 | buf3 = get_tree_descriptor(t+2, argv[3]); |
| 353 | merge_trees(t, ""); |
| 354 | free(buf1); |
| 355 | free(buf2); |
| 356 | free(buf3); |
Linus Torvalds | 8378807 | 2006-06-28 11:18:27 -0700 | [diff] [blame] | 357 | |
| 358 | show_result(); |
Linus Torvalds | 492e075 | 2006-02-14 18:05:30 -0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |