Merge branch 'maint'
[git.git] / merge-tree.c
1 #include "cache.h"
2 #include "tree-walk.h"
3
4 static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
5 static int resolve_directories = 1;
6
7 static void merge_trees(struct tree_desc t[3], const char *base);
8
9 /* An empty entry never compares same, not even to another empty entry */
10 static int same_entry(struct name_entry *a, struct name_entry *b)
11 {
12         return  a->sha1 &&
13                 b->sha1 &&
14                 !memcmp(a->sha1, b->sha1, 20) &&
15                 a->mode == b->mode;
16 }
17
18 static const char *sha1_to_hex_zero(const unsigned char *sha1)
19 {
20         if (sha1)
21                 return sha1_to_hex(sha1);
22         return "0000000000000000000000000000000000000000";
23 }
24
25 static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
26 {
27         char branch1_sha1[50];
28
29         /* If it's already branch1, don't bother showing it */
30         if (!branch1)
31                 return;
32         memcpy(branch1_sha1, sha1_to_hex_zero(branch1->sha1), 41);
33
34         printf("0 %06o->%06o %s->%s %s%s\n",
35                 branch1->mode, result->mode,
36                 branch1_sha1, sha1_to_hex_zero(result->sha1),
37                 base, result->path);
38 }
39
40 static int unresolved_directory(const char *base, struct name_entry n[3])
41 {
42         int baselen;
43         char *newbase;
44         struct name_entry *p;
45         struct tree_desc t[3];
46         void *buf0, *buf1, *buf2;
47
48         if (!resolve_directories)
49                 return 0;
50         p = n;
51         if (!p->mode) {
52                 p++;
53                 if (!p->mode)
54                         p++;
55         }
56         if (!S_ISDIR(p->mode))
57                 return 0;
58         baselen = strlen(base);
59         newbase = xmalloc(baselen + p->pathlen + 2);
60         memcpy(newbase, base, baselen);
61         memcpy(newbase + baselen, p->path, p->pathlen);
62         memcpy(newbase + baselen + p->pathlen, "/", 2);
63
64         buf0 = fill_tree_descriptor(t+0, n[0].sha1);
65         buf1 = fill_tree_descriptor(t+1, n[1].sha1);
66         buf2 = fill_tree_descriptor(t+2, n[2].sha1);
67         merge_trees(t, newbase);
68
69         free(buf0);
70         free(buf1);
71         free(buf2);
72         free(newbase);
73         return 1;
74 }
75
76 static void unresolved(const char *base, struct name_entry n[3])
77 {
78         if (unresolved_directory(base, n))
79                 return;
80         if (n[0].sha1)
81                 printf("1 %06o %s %s%s\n", n[0].mode, sha1_to_hex(n[0].sha1), base, n[0].path);
82         if (n[1].sha1)
83                 printf("2 %06o %s %s%s\n", n[1].mode, sha1_to_hex(n[1].sha1), base, n[1].path);
84         if (n[2].sha1)
85                 printf("3 %06o %s %s%s\n", n[2].mode, sha1_to_hex(n[2].sha1), base, n[2].path);
86 }
87
88 /*
89  * Merge two trees together (t[1] and t[2]), using a common base (t[0])
90  * as the origin.
91  *
92  * This walks the (sorted) trees in lock-step, checking every possible
93  * name. Note that directories automatically sort differently from other
94  * files (see "base_name_compare"), so you'll never see file/directory
95  * conflicts, because they won't ever compare the same.
96  *
97  * IOW, if a directory changes to a filename, it will automatically be
98  * seen as the directory going away, and the filename being created.
99  *
100  * Think of this as a three-way diff.
101  *
102  * The output will be either:
103  *  - successful merge
104  *       "0 mode sha1 filename"
105  *    NOTE NOTE NOTE! FIXME! We really really need to walk the index
106  *    in parallel with this too!
107  *
108  *  - conflict:
109  *      "1 mode sha1 filename"
110  *      "2 mode sha1 filename"
111  *      "3 mode sha1 filename"
112  *    where not all of the 1/2/3 lines may exist, of course.
113  *
114  * The successful merge rules are the same as for the three-way merge
115  * in git-read-tree.
116  */
117 static void threeway_callback(int n, unsigned long mask, struct name_entry *entry, const char *base)
118 {
119         /* Same in both? */
120         if (same_entry(entry+1, entry+2)) {
121                 if (entry[0].sha1) {
122                         resolve(base, NULL, entry+1);
123                         return;
124                 }
125         }
126
127         if (same_entry(entry+0, entry+1)) {
128                 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
129                         resolve(base, entry+1, entry+2);
130                         return;
131                 }
132         }
133
134         if (same_entry(entry+0, entry+2)) {
135                 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
136                         resolve(base, NULL, entry+1);
137                         return;
138                 }
139         }
140
141         unresolved(base, entry);
142 }
143
144 static void merge_trees(struct tree_desc t[3], const char *base)
145 {
146         traverse_trees(3, t, base, threeway_callback);
147 }
148
149 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
150 {
151         unsigned char sha1[20];
152         void *buf;
153
154         if (get_sha1(rev, sha1) < 0)
155                 die("unknown rev %s", rev);
156         buf = fill_tree_descriptor(desc, sha1);
157         if (!buf)
158                 die("%s is not a tree", rev);
159         return buf;
160 }
161
162 int main(int argc, char **argv)
163 {
164         struct tree_desc t[3];
165         void *buf1, *buf2, *buf3;
166
167         if (argc < 4)
168                 usage(merge_tree_usage);
169
170         buf1 = get_tree_descriptor(t+0, argv[1]);
171         buf2 = get_tree_descriptor(t+1, argv[2]);
172         buf3 = get_tree_descriptor(t+2, argv[3]);
173         merge_trees(t, "");
174         free(buf1);
175         free(buf2);
176         free(buf3);
177         return 0;
178 }