5 static const char merge_tree_usage
[] = "git-merge-tree <base-tree> <branch1> <branch2>";
6 static int resolve_directories
= 1;
9 struct merge_list
*next
;
10 struct merge_list
*link
; /* other stages for this object */
12 unsigned int stage
: 2,
19 static struct merge_list
*merge_result
, **merge_result_end
= &merge_result
;
21 static void add_merge_entry(struct merge_list
*entry
)
23 *merge_result_end
= entry
;
24 merge_result_end
= &entry
->next
;
27 static void merge_trees(struct tree_desc t
[3], const char *base
);
29 static const char *explanation(struct merge_list
*entry
)
31 switch (entry
->stage
) {
35 return "added in remote";
38 return "added in both";
39 return "added in local";
45 return "removed in both";
48 return "changed in both";
50 if (entry
->stage
== 3)
51 return "removed in local";
52 return "removed in remote";
55 static void show_result_list(struct merge_list
*entry
)
57 printf("%s\n", explanation(entry
));
59 struct merge_list
*link
= entry
->link
;
60 static const char *desc
[4] = { "result", "base", "our", "their" };
61 printf(" %-6s %o %s %s\n", desc
[entry
->stage
], entry
->mode
, sha1_to_hex(entry
->blob
->object
.sha1
), entry
->path
);
66 static void show_result(void)
68 struct merge_list
*walk
;
72 show_result_list(walk
);
77 /* An empty entry never compares same, not even to another empty entry */
78 static int same_entry(struct name_entry
*a
, struct name_entry
*b
)
82 !memcmp(a
->sha1
, b
->sha1
, 20) &&
86 static struct merge_list
*create_entry(unsigned stage
, unsigned mode
, const unsigned char *sha1
, const char *path
)
88 struct merge_list
*res
= xmalloc(sizeof(*res
));
90 memset(res
, 0, sizeof(*res
));
94 res
->blob
= lookup_blob(sha1
);
98 static void resolve(const char *base
, struct name_entry
*branch1
, struct name_entry
*result
)
100 struct merge_list
*orig
, *final
;
103 /* If it's already branch1, don't bother showing it */
107 path
= strdup(mkpath("%s%s", base
, result
->path
));
108 orig
= create_entry(2, branch1
->mode
, branch1
->sha1
, path
);
109 final
= create_entry(0, result
->mode
, result
->sha1
, path
);
113 add_merge_entry(final
);
116 static int unresolved_directory(const char *base
, struct name_entry n
[3])
120 struct name_entry
*p
;
121 struct tree_desc t
[3];
122 void *buf0
, *buf1
, *buf2
;
124 if (!resolve_directories
)
132 if (!S_ISDIR(p
->mode
))
134 baselen
= strlen(base
);
135 newbase
= xmalloc(baselen
+ p
->pathlen
+ 2);
136 memcpy(newbase
, base
, baselen
);
137 memcpy(newbase
+ baselen
, p
->path
, p
->pathlen
);
138 memcpy(newbase
+ baselen
+ p
->pathlen
, "/", 2);
140 buf0
= fill_tree_descriptor(t
+0, n
[0].sha1
);
141 buf1
= fill_tree_descriptor(t
+1, n
[1].sha1
);
142 buf2
= fill_tree_descriptor(t
+2, n
[2].sha1
);
143 merge_trees(t
, newbase
);
153 static struct merge_list
*link_entry(unsigned stage
, const char *base
, struct name_entry
*n
, struct merge_list
*entry
)
156 struct merge_list
*link
;
163 path
= strdup(mkpath("%s%s", base
, n
->path
));
164 link
= create_entry(stage
, n
->mode
, n
->sha1
, path
);
169 static void unresolved(const char *base
, struct name_entry n
[3])
171 struct merge_list
*entry
= NULL
;
173 if (unresolved_directory(base
, n
))
177 * Do them in reverse order so that the resulting link
178 * list has the stages in order - link_entry adds new
179 * links at the front.
181 entry
= link_entry(3, base
, n
+ 2, entry
);
182 entry
= link_entry(2, base
, n
+ 1, entry
);
183 entry
= link_entry(1, base
, n
+ 0, entry
);
185 add_merge_entry(entry
);
189 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
192 * This walks the (sorted) trees in lock-step, checking every possible
193 * name. Note that directories automatically sort differently from other
194 * files (see "base_name_compare"), so you'll never see file/directory
195 * conflicts, because they won't ever compare the same.
197 * IOW, if a directory changes to a filename, it will automatically be
198 * seen as the directory going away, and the filename being created.
200 * Think of this as a three-way diff.
202 * The output will be either:
204 * "0 mode sha1 filename"
205 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
206 * in parallel with this too!
209 * "1 mode sha1 filename"
210 * "2 mode sha1 filename"
211 * "3 mode sha1 filename"
212 * where not all of the 1/2/3 lines may exist, of course.
214 * The successful merge rules are the same as for the three-way merge
217 static void threeway_callback(int n
, unsigned long mask
, struct name_entry
*entry
, const char *base
)
220 if (same_entry(entry
+1, entry
+2)) {
222 resolve(base
, NULL
, entry
+1);
227 if (same_entry(entry
+0, entry
+1)) {
228 if (entry
[2].sha1
&& !S_ISDIR(entry
[2].mode
)) {
229 resolve(base
, entry
+1, entry
+2);
234 if (same_entry(entry
+0, entry
+2)) {
235 if (entry
[1].sha1
&& !S_ISDIR(entry
[1].mode
)) {
236 resolve(base
, NULL
, entry
+1);
241 unresolved(base
, entry
);
244 static void merge_trees(struct tree_desc t
[3], const char *base
)
246 traverse_trees(3, t
, base
, threeway_callback
);
249 static void *get_tree_descriptor(struct tree_desc
*desc
, const char *rev
)
251 unsigned char sha1
[20];
254 if (get_sha1(rev
, sha1
))
255 die("unknown rev %s", rev
);
256 buf
= fill_tree_descriptor(desc
, sha1
);
258 die("%s is not a tree", rev
);
262 int main(int argc
, char **argv
)
264 struct tree_desc t
[3];
265 void *buf1
, *buf2
, *buf3
;
268 usage(merge_tree_usage
);
270 buf1
= get_tree_descriptor(t
+0, argv
[1]);
271 buf2
= get_tree_descriptor(t
+1, argv
[2]);
272 buf3
= get_tree_descriptor(t
+2, argv
[3]);