3 static int line_termination
= '\n';
9 struct tree_entry
*next
;
12 static struct tree_entry
*read_tree(unsigned char *sha1
)
16 void *buf
= read_sha1_file(sha1
, type
, &size
);
17 struct tree_entry
*ret
= NULL
, **tp
= &ret
;
19 if (!buf
|| strcmp(type
, "tree"))
20 die("unable to read 'tree' object %s", sha1_to_hex(sha1
));
22 int len
= strlen(buf
)+1;
23 struct tree_entry
* entry
= malloc(sizeof(struct tree_entry
));
24 if (size
< len
+ 20 || sscanf(buf
, "%o", &entry
->mode
) != 1)
25 die("corrupt 'tree' object %s", sha1_to_hex(sha1
));
26 entry
->path
= strchr(buf
, ' ')+1;
27 entry
->sha1
= buf
+ len
;
38 static void show(const struct tree_entry
*a
, const char *path
)
40 printf("select %o %s %s%c", a
->mode
, sha1_to_hex(a
->sha1
), path
,
44 static void merge(const struct tree_entry
*a
, const struct tree_entry
*b
, const struct tree_entry
*c
, const char *path
)
46 char hex_a
[60], hex_b
[60], hex_c
[60];
47 strcpy(hex_a
, sha1_to_hex(a
->sha1
));
48 strcpy(hex_b
, sha1_to_hex(b
->sha1
));
49 strcpy(hex_c
, sha1_to_hex(c
->sha1
));
50 printf("merge %o->%o,%o %s->%s,%s %s%c",
51 a
->mode
, b
->mode
, c
->mode
,
52 hex_a
, hex_b
, hex_c
, path
, line_termination
);
55 static int same(const struct tree_entry
*a
, const struct tree_entry
*b
)
57 return a
->mode
== b
->mode
&& !memcmp(a
->sha1
, b
->sha1
, 20);
60 static void merge_entry(const struct tree_entry
*src
, const struct tree_entry
*dst1
, const struct tree_entry
*dst2
)
62 static unsigned char nullsha1
[20];
63 static const struct tree_entry none
= { 0, nullsha1
, "", NULL
};
64 const char *path
= NULL
;
65 const struct tree_entry
*a
, *b
, *c
;
70 if (src
) { a
= src
; path
= src
->path
; }
71 if (dst1
) { b
= dst1
; path
= dst1
->path
; }
72 if (dst2
) { c
= dst2
; path
= dst2
->path
; }
88 /* For two entries, select the smaller one, clear the bigger one */
89 static void smaller(struct tree_entry
**ap
, struct tree_entry
**bp
)
91 struct tree_entry
*a
= *ap
, *b
= *bp
;
93 int cmp
= cache_name_compare(a
->path
, strlen(a
->path
), b
->path
, strlen(b
->path
));
103 static void merge_tree(struct tree_entry
*src
, struct tree_entry
*dst1
, struct tree_entry
*dst2
)
105 while (src
|| dst1
|| dst2
) {
106 struct tree_entry
*a
, *b
, *c
;
113 if (a
) src
= a
->next
;
114 if (b
) dst1
= b
->next
;
115 if (c
) dst2
= c
->next
;
120 static const char *merge_tree_usage
=
121 "merge-tree [-z] <src> <dst1> <dst2>";
123 int main(int argc
, char **argv
)
125 unsigned char src
[20], dst1
[20], dst2
[20];
127 while ((1 < argc
) && argv
[1][0] == '-') {
128 switch (argv
[1][1]) {
130 line_termination
= 0;
133 usage(merge_tree_usage
);
139 get_sha1_hex(argv
[1], src
) ||
140 get_sha1_hex(argv
[2], dst1
) ||
141 get_sha1_hex(argv
[3], dst2
))
142 usage(merge_tree_usage
);
143 merge_tree(read_tree(src
), read_tree(dst1
), read_tree(dst2
));