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 void resolve(const char *base
, struct name_entry
*result
)
60 printf("0 %06o %s %s%s\n", result
->mode
, sha1_to_hex(result
->sha1
), base
, result
->path
);
63 static int unresolved_directory(const char *base
, struct name_entry n
[3])
68 struct tree_desc t
[3];
69 void *buf0
, *buf1
, *buf2
;
71 if (!resolve_directories
)
79 if (!S_ISDIR(p
->mode
))
81 baselen
= strlen(base
);
82 newbase
= xmalloc(baselen
+ p
->pathlen
+ 2);
83 memcpy(newbase
, base
, baselen
);
84 memcpy(newbase
+ baselen
, p
->path
, p
->pathlen
);
85 memcpy(newbase
+ baselen
+ p
->pathlen
, "/", 2);
87 buf0
= fill_tree_descriptor(t
+0, n
[0].sha1
);
88 buf1
= fill_tree_descriptor(t
+1, n
[1].sha1
);
89 buf2
= fill_tree_descriptor(t
+2, n
[2].sha1
);
90 merge_trees(t
, newbase
);
99 static void unresolved(const char *base
, struct name_entry n
[3])
101 if (unresolved_directory(base
, n
))
103 printf("1 %06o %s %s%s\n", n
[0].mode
, sha1_to_hex(n
[0].sha1
), base
, n
[0].path
);
104 printf("2 %06o %s %s%s\n", n
[1].mode
, sha1_to_hex(n
[1].sha1
), base
, n
[1].path
);
105 printf("3 %06o %s %s%s\n", n
[2].mode
, sha1_to_hex(n
[2].sha1
), base
, n
[2].path
);
109 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
112 * This walks the (sorted) trees in lock-step, checking every possible
113 * name. Note that directories automatically sort differently from other
114 * files (see "base_name_compare"), so you'll never see file/directory
115 * conflicts, because they won't ever compare the same.
117 * IOW, if a directory changes to a filename, it will automatically be
118 * seen as the directory going away, and the filename being created.
120 * Think of this as a three-way diff.
122 * The output will be either:
124 * "0 mode sha1 filename"
125 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
126 * in parallel with this too!
129 * "1 mode sha1 filename"
130 * "2 mode sha1 filename"
131 * "3 mode sha1 filename"
132 * where not all of the 1/2/3 lines may exist, of course.
134 * The successful merge rules are the same as for the three-way merge
137 static void merge_trees(struct tree_desc t
[3], const char *base
)
140 struct name_entry entry
[3];
141 unsigned int mask
= 0;
145 for (i
= 0; i
< 3; i
++) {
148 entry_extract(t
+i
, entry
+i
);
150 int cmp
= entry_compare(entry
+i
, entry
+last
);
153 * Is the new name bigger than the old one?
159 * Is the new name smaller than the old one?
160 * Ignore all old ones
172 * Update the tree entries we've walked, and clear
173 * all the unused name-entries.
175 for (i
= 0; i
< 3; i
++) {
176 if (mask
& (1u << i
)) {
177 update_tree_entry(t
+i
);
180 entry_clear(entry
+ i
);
184 if (same_entry(entry
+1, entry
+2)) {
186 resolve(base
, entry
+1);
191 if (same_entry(entry
+0, entry
+1)) {
193 resolve(base
, entry
+2);
198 if (same_entry(entry
+0, entry
+2)) {
200 resolve(base
, entry
+1);
205 unresolved(base
, entry
);
209 static void *get_tree_descriptor(struct tree_desc
*desc
, const char *rev
)
211 unsigned char sha1
[20];
214 if (get_sha1(rev
, sha1
) < 0)
215 die("unknown rev %s", rev
);
216 buf
= fill_tree_descriptor(desc
, sha1
);
218 die("%s is not a tree", rev
);
222 int main(int argc
, char **argv
)
224 struct tree_desc t
[3];
225 void *buf1
, *buf2
, *buf3
;
228 usage(merge_tree_usage
);
230 buf1
= get_tree_descriptor(t
+0, argv
[1]);
231 buf2
= get_tree_descriptor(t
+1, argv
[2]);
232 buf3
= get_tree_descriptor(t
+2, argv
[3]);