2 * Helper functions for tree diff generation
8 // What paths are we interested in?
9 static int nr_paths
= 0;
10 static const char **paths
= NULL
;
11 static int *pathlens
= NULL
;
13 static char *malloc_base(const char *base
, const char *path
, int pathlen
)
15 int baselen
= strlen(base
);
16 char *newbase
= xmalloc(baselen
+ pathlen
+ 2);
17 memcpy(newbase
, base
, baselen
);
18 memcpy(newbase
+ baselen
, path
, pathlen
);
19 memcpy(newbase
+ baselen
+ pathlen
, "/", 2);
23 static int show_entry(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
, const char *base
);
25 static int compare_tree_entry(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
27 unsigned mode1
, mode2
;
28 const char *path1
, *path2
;
29 const unsigned char *sha1
, *sha2
;
30 int cmp
, pathlen1
, pathlen2
;
32 sha1
= tree_entry_extract(t1
, &path1
, &mode1
);
33 sha2
= tree_entry_extract(t2
, &path2
, &mode2
);
35 pathlen1
= strlen(path1
);
36 pathlen2
= strlen(path2
);
37 cmp
= base_name_compare(path1
, pathlen1
, mode1
, path2
, pathlen2
, mode2
);
39 show_entry(opt
, "-", t1
, base
);
43 show_entry(opt
, "+", t2
, base
);
46 if (!opt
->find_copies_harder
&&
47 !memcmp(sha1
, sha2
, 20) && mode1
== mode2
)
51 * If the filemode has changed to/from a directory from/to a regular
52 * file, we need to consider it a remove and an add.
54 if (S_ISDIR(mode1
) != S_ISDIR(mode2
)) {
55 show_entry(opt
, "-", t1
, base
);
56 show_entry(opt
, "+", t2
, base
);
60 if (opt
->recursive
&& S_ISDIR(mode1
)) {
62 char *newbase
= malloc_base(base
, path1
, pathlen1
);
63 if (opt
->tree_in_recursive
)
64 opt
->change(opt
, mode1
, mode2
,
65 sha1
, sha2
, base
, path1
);
66 retval
= diff_tree_sha1(sha1
, sha2
, newbase
, opt
);
71 opt
->change(opt
, mode1
, mode2
, sha1
, sha2
, base
, path1
);
75 static int interesting(struct tree_desc
*desc
, const char *base
)
85 (void)tree_entry_extract(desc
, &path
, &mode
);
87 pathlen
= strlen(path
);
88 baselen
= strlen(base
);
90 for (i
=0; i
< nr_paths
; i
++) {
91 const char *match
= paths
[i
];
92 int matchlen
= pathlens
[i
];
94 if (baselen
>= matchlen
) {
95 /* If it doesn't match, move along... */
96 if (strncmp(base
, match
, matchlen
))
99 /* The base is a subdirectory of a path which was specified. */
103 /* Does the base match? */
104 if (strncmp(base
, match
, baselen
))
110 if (pathlen
> matchlen
)
113 if (matchlen
> pathlen
) {
114 if (match
[pathlen
] != '/')
120 if (strncmp(path
, match
, pathlen
))
125 return 0; /* No matches */
128 /* A whole sub-tree went away or appeared */
129 static void show_tree(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
, const char *base
)
132 if (interesting(desc
, base
))
133 show_entry(opt
, prefix
, desc
, base
);
134 update_tree_entry(desc
);
138 /* A file entry went away or appeared */
139 static int show_entry(struct diff_options
*opt
, const char *prefix
, struct tree_desc
*desc
, const char *base
)
143 const unsigned char *sha1
= tree_entry_extract(desc
, &path
, &mode
);
145 if (opt
->recursive
&& S_ISDIR(mode
)) {
147 char *newbase
= malloc_base(base
, path
, strlen(path
));
148 struct tree_desc inner
;
151 tree
= read_sha1_file(sha1
, type
, &inner
.size
);
152 if (!tree
|| strcmp(type
, tree_type
))
153 die("corrupt tree sha %s", sha1_to_hex(sha1
));
156 show_tree(opt
, prefix
, &inner
, newbase
);
163 opt
->add_remove(opt
, prefix
[0], mode
, sha1
, base
, path
);
167 int diff_tree(struct tree_desc
*t1
, struct tree_desc
*t2
, const char *base
, struct diff_options
*opt
)
169 while (t1
->size
| t2
->size
) {
170 if (nr_paths
&& t1
->size
&& !interesting(t1
, base
)) {
171 update_tree_entry(t1
);
174 if (nr_paths
&& t2
->size
&& !interesting(t2
, base
)) {
175 update_tree_entry(t2
);
179 show_entry(opt
, "+", t2
, base
);
180 update_tree_entry(t2
);
184 show_entry(opt
, "-", t1
, base
);
185 update_tree_entry(t1
);
188 switch (compare_tree_entry(t1
, t2
, base
, opt
)) {
190 update_tree_entry(t1
);
193 update_tree_entry(t1
);
196 update_tree_entry(t2
);
199 die("git-diff-tree: internal error");
204 int diff_tree_sha1(const unsigned char *old
, const unsigned char *new, const char *base
, struct diff_options
*opt
)
207 struct tree_desc t1
, t2
;
210 tree1
= read_object_with_reference(old
, tree_type
, &t1
.size
, NULL
);
212 die("unable to read source tree (%s)", sha1_to_hex(old
));
213 tree2
= read_object_with_reference(new, tree_type
, &t2
.size
, NULL
);
215 die("unable to read destination tree (%s)", sha1_to_hex(new));
218 retval
= diff_tree(&t1
, &t2
, base
, opt
);
224 static int count_paths(const char **paths
)
232 void diff_tree_setup_paths(const char **p
)
238 nr_paths
= count_paths(paths
);
243 pathlens
= xmalloc(nr_paths
* sizeof(int));
244 for (i
=0; i
<nr_paths
; i
++)
245 pathlens
[i
] = strlen(paths
[i
]);