4 static const char merge_tree_usage
[] = "git-merge-tree <base-tree> <branch1> <branch2>";
5 static int resolve_directories
= 1;
7 static void merge_trees(struct tree_desc t
[3], const char *base
);
9 static void *fill_tree_descriptor(struct tree_desc
*desc
, const unsigned char *sha1
)
11 unsigned long size
= 0;
15 buf
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
17 die("unable to read tree %s", sha1_to_hex(sha1
));
25 const unsigned char *sha1
;
31 static void entry_clear(struct name_entry
*a
)
33 memset(a
, 0, sizeof(*a
));
36 static int entry_compare(struct name_entry
*a
, struct name_entry
*b
)
38 return base_name_compare(
39 a
->path
, a
->pathlen
, a
->mode
,
40 b
->path
, b
->pathlen
, b
->mode
);
43 static void entry_extract(struct tree_desc
*t
, struct name_entry
*a
)
45 a
->sha1
= tree_entry_extract(t
, &a
->path
, &a
->mode
);
46 a
->pathlen
= strlen(a
->path
);
49 /* An empty entry never compares same, not even to another empty entry */
50 static int same_entry(struct name_entry
*a
, struct name_entry
*b
)
54 !memcmp(a
->sha1
, b
->sha1
, 20) &&
58 static const char *sha1_to_hex_zero(const unsigned char *sha1
)
61 return sha1_to_hex(sha1
);
62 return "0000000000000000000000000000000000000000";
65 static void resolve(const char *base
, struct name_entry
*branch1
, struct name_entry
*result
)
67 char branch1_sha1
[50];
69 /* If it's already branch1, don't bother showing it */
72 memcpy(branch1_sha1
, sha1_to_hex_zero(branch1
->sha1
), 41);
74 printf("0 %06o->%06o %s->%s %s%s\n",
75 branch1
->mode
, result
->mode
,
76 branch1_sha1
, sha1_to_hex_zero(result
->sha1
),
80 static int unresolved_directory(const char *base
, struct name_entry n
[3])
85 struct tree_desc t
[3];
86 void *buf0
, *buf1
, *buf2
;
88 if (!resolve_directories
)
96 if (!S_ISDIR(p
->mode
))
98 baselen
= strlen(base
);
99 newbase
= xmalloc(baselen
+ p
->pathlen
+ 2);
100 memcpy(newbase
, base
, baselen
);
101 memcpy(newbase
+ baselen
, p
->path
, p
->pathlen
);
102 memcpy(newbase
+ baselen
+ p
->pathlen
, "/", 2);
104 buf0
= fill_tree_descriptor(t
+0, n
[0].sha1
);
105 buf1
= fill_tree_descriptor(t
+1, n
[1].sha1
);
106 buf2
= fill_tree_descriptor(t
+2, n
[2].sha1
);
107 merge_trees(t
, newbase
);
116 static void unresolved(const char *base
, struct name_entry n
[3])
118 if (unresolved_directory(base
, n
))
121 printf("1 %06o %s %s%s\n", n
[0].mode
, sha1_to_hex(n
[0].sha1
), base
, n
[0].path
);
123 printf("2 %06o %s %s%s\n", n
[1].mode
, sha1_to_hex(n
[1].sha1
), base
, n
[1].path
);
125 printf("3 %06o %s %s%s\n", n
[2].mode
, sha1_to_hex(n
[2].sha1
), base
, n
[2].path
);
129 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
132 * This walks the (sorted) trees in lock-step, checking every possible
133 * name. Note that directories automatically sort differently from other
134 * files (see "base_name_compare"), so you'll never see file/directory
135 * conflicts, because they won't ever compare the same.
137 * IOW, if a directory changes to a filename, it will automatically be
138 * seen as the directory going away, and the filename being created.
140 * Think of this as a three-way diff.
142 * The output will be either:
144 * "0 mode sha1 filename"
145 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
146 * in parallel with this too!
149 * "1 mode sha1 filename"
150 * "2 mode sha1 filename"
151 * "3 mode sha1 filename"
152 * where not all of the 1/2/3 lines may exist, of course.
154 * The successful merge rules are the same as for the three-way merge
157 static void merge_trees(struct tree_desc t
[3], const char *base
)
160 struct name_entry entry
[3];
161 unsigned int mask
= 0;
165 for (i
= 0; i
< 3; i
++) {
168 entry_extract(t
+i
, entry
+i
);
170 int cmp
= entry_compare(entry
+i
, entry
+last
);
173 * Is the new name bigger than the old one?
179 * Is the new name smaller than the old one?
180 * Ignore all old ones
192 * Update the tree entries we've walked, and clear
193 * all the unused name-entries.
195 for (i
= 0; i
< 3; i
++) {
196 if (mask
& (1u << i
)) {
197 update_tree_entry(t
+i
);
200 entry_clear(entry
+ i
);
204 if (same_entry(entry
+1, entry
+2)) {
206 resolve(base
, NULL
, entry
+1);
211 if (same_entry(entry
+0, entry
+1)) {
212 if (entry
[2].sha1
&& !S_ISDIR(entry
[2].mode
)) {
213 resolve(base
, entry
+1, entry
+2);
218 if (same_entry(entry
+0, entry
+2)) {
219 if (entry
[1].sha1
&& !S_ISDIR(entry
[1].mode
)) {
220 resolve(base
, NULL
, entry
+1);
225 unresolved(base
, entry
);
229 static void *get_tree_descriptor(struct tree_desc
*desc
, const char *rev
)
231 unsigned char sha1
[20];
234 if (get_sha1(rev
, sha1
) < 0)
235 die("unknown rev %s", rev
);
236 buf
= fill_tree_descriptor(desc
, sha1
);
238 die("%s is not a tree", rev
);
242 int main(int argc
, char **argv
)
244 struct tree_desc t
[3];
245 void *buf1
, *buf2
, *buf3
;
248 usage(merge_tree_usage
);
250 buf1
= get_tree_descriptor(t
+0, argv
[1]);
251 buf2
= get_tree_descriptor(t
+1, argv
[2]);
252 buf3
= get_tree_descriptor(t
+2, argv
[3]);