Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for tree diff generation |
| 3 | */ |
| 4 | #include "cache.h" |
| 5 | #include "diff.h" |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 6 | #include "diffcore.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 7 | #include "tree.h" |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 8 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 9 | static char *malloc_base(const char *base, int baselen, const char *path, int pathlen) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 10 | { |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 11 | char *newbase = xmalloc(baselen + pathlen + 2); |
| 12 | memcpy(newbase, base, baselen); |
| 13 | memcpy(newbase + baselen, path, pathlen); |
| 14 | memcpy(newbase + baselen + pathlen, "/", 2); |
| 15 | return newbase; |
| 16 | } |
| 17 | |
David Rientjes | cf995ed | 2006-08-14 13:39:27 -0700 | [diff] [blame] | 18 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 19 | const char *base, int baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 21 | static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 22 | { |
| 23 | unsigned mode1, mode2; |
| 24 | const char *path1, *path2; |
| 25 | const unsigned char *sha1, *sha2; |
| 26 | int cmp, pathlen1, pathlen2; |
| 27 | |
Linus Torvalds | 50f9a85 | 2006-01-31 14:10:56 -0800 | [diff] [blame] | 28 | sha1 = tree_entry_extract(t1, &path1, &mode1); |
| 29 | sha2 = tree_entry_extract(t2, &path2, &mode2); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 31 | pathlen1 = tree_entry_len(path1, sha1); |
| 32 | pathlen2 = tree_entry_len(path2, sha2); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 33 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
| 34 | if (cmp < 0) { |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 35 | show_entry(opt, "-", t1, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | if (cmp > 0) { |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 39 | show_entry(opt, "+", t2, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 40 | return 1; |
| 41 | } |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 42 | if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | |
| 45 | /* |
| 46 | * If the filemode has changed to/from a directory from/to a regular |
| 47 | * file, we need to consider it a remove and an add. |
| 48 | */ |
| 49 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 50 | show_entry(opt, "-", t1, base, baselen); |
| 51 | show_entry(opt, "+", t2, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 55 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) { |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 56 | int retval; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 57 | char *newbase = malloc_base(base, baselen, path1, pathlen1); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 58 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 59 | opt->change(opt, mode1, mode2, |
| 60 | sha1, sha2, base, path1); |
| 61 | retval = diff_tree_sha1(sha1, sha2, newbase, opt); |
| 62 | free(newbase); |
| 63 | return retval; |
| 64 | } |
| 65 | |
| 66 | opt->change(opt, mode1, mode2, sha1, sha2, base, path1); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Is a tree entry interesting given the pathspec we have? |
| 72 | * |
| 73 | * Return: |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 74 | * - 2 for "yes, and all subsequent entries will be" |
| 75 | * - 1 for yes |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 76 | * - zero for no |
| 77 | * - negative for "no, and no subsequent entries will be either" |
| 78 | */ |
| 79 | static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 80 | { |
| 81 | const char *path; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 82 | const unsigned char *sha1; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 83 | unsigned mode; |
| 84 | int i; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 85 | int pathlen; |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 86 | int never_interesting = -1; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 87 | |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 88 | if (!opt->nr_paths) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 89 | return 1; |
| 90 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 91 | sha1 = tree_entry_extract(desc, &path, &mode); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 92 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 93 | pathlen = tree_entry_len(path, sha1); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 94 | |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 95 | for (i = 0; i < opt->nr_paths; i++) { |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 96 | const char *match = opt->paths[i]; |
| 97 | int matchlen = opt->pathlens[i]; |
Junio C Hamano | ccc744a | 2007-03-21 12:34:46 -0700 | [diff] [blame] | 98 | int m = -1; /* signals that we haven't called strncmp() */ |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 99 | |
| 100 | if (baselen >= matchlen) { |
| 101 | /* If it doesn't match, move along... */ |
| 102 | if (strncmp(base, match, matchlen)) |
| 103 | continue; |
| 104 | |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 105 | /* |
| 106 | * The base is a subdirectory of a path which |
| 107 | * was specified, so all of them are interesting. |
| 108 | */ |
| 109 | return 2; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* Does the base match? */ |
| 113 | if (strncmp(base, match, baselen)) |
| 114 | continue; |
| 115 | |
| 116 | match += baselen; |
| 117 | matchlen -= baselen; |
| 118 | |
Junio C Hamano | ccc744a | 2007-03-21 12:34:46 -0700 | [diff] [blame] | 119 | if (never_interesting) { |
| 120 | /* |
| 121 | * We have not seen any match that sorts later |
| 122 | * than the current path. |
| 123 | */ |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 124 | |
Junio C Hamano | ccc744a | 2007-03-21 12:34:46 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Does match sort strictly earlier than path |
| 127 | * with their common parts? |
| 128 | */ |
| 129 | m = strncmp(match, path, |
| 130 | (matchlen < pathlen) ? matchlen : pathlen); |
| 131 | if (m < 0) |
| 132 | continue; |
| 133 | |
| 134 | /* |
| 135 | * If we come here even once, that means there is at |
| 136 | * least one pathspec that would sort equal to or |
| 137 | * later than the path we are currently looking at. |
| 138 | * In other words, if we have never reached this point |
| 139 | * after iterating all pathspecs, it means all |
| 140 | * pathspecs are either outside of base, or inside the |
| 141 | * base but sorts strictly earlier than the current |
| 142 | * one. In either case, they will never match the |
| 143 | * subsequent entries. In such a case, we initialized |
| 144 | * the variable to -1 and that is what will be |
| 145 | * returned, allowing the caller to terminate early. |
| 146 | */ |
| 147 | never_interesting = 0; |
| 148 | } |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 149 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 150 | if (pathlen > matchlen) |
| 151 | continue; |
| 152 | |
| 153 | if (matchlen > pathlen) { |
| 154 | if (match[pathlen] != '/') |
| 155 | continue; |
| 156 | if (!S_ISDIR(mode)) |
| 157 | continue; |
| 158 | } |
| 159 | |
Junio C Hamano | ccc744a | 2007-03-21 12:34:46 -0700 | [diff] [blame] | 160 | if (m == -1) |
| 161 | /* |
| 162 | * we cheated and did not do strncmp(), so we do |
| 163 | * that here. |
| 164 | */ |
| 165 | m = strncmp(match, path, pathlen); |
| 166 | |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 167 | /* |
| 168 | * If common part matched earlier then it is a hit, |
| 169 | * because we rejected the case where path is not a |
| 170 | * leading directory and is shorter than match. |
| 171 | */ |
| 172 | if (!m) |
| 173 | return 1; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 174 | } |
Junio C Hamano | 7d2f667 | 2007-03-21 09:51:47 -0700 | [diff] [blame] | 175 | return never_interesting; /* No matches */ |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* A whole sub-tree went away or appeared */ |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 179 | static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 180 | { |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 181 | int all_interesting = 0; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 182 | while (desc->size) { |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 183 | int show; |
| 184 | |
| 185 | if (all_interesting) |
| 186 | show = 1; |
| 187 | else { |
| 188 | show = tree_entry_interesting(desc, base, baselen, |
| 189 | opt); |
| 190 | if (show == 2) |
| 191 | all_interesting = 1; |
| 192 | } |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 193 | if (show < 0) |
| 194 | break; |
| 195 | if (show) |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 196 | show_entry(opt, prefix, desc, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 197 | update_tree_entry(desc); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* A file entry went away or appeared */ |
David Rientjes | cf995ed | 2006-08-14 13:39:27 -0700 | [diff] [blame] | 202 | static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 203 | const char *base, int baselen) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 204 | { |
| 205 | unsigned mode; |
| 206 | const char *path; |
Linus Torvalds | 50f9a85 | 2006-01-31 14:10:56 -0800 | [diff] [blame] | 207 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 208 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 209 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 210 | enum object_type type; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 211 | int pathlen = tree_entry_len(path, sha1); |
| 212 | char *newbase = malloc_base(base, baselen, path, pathlen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 213 | struct tree_desc inner; |
| 214 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 215 | unsigned long size; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 216 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 217 | tree = read_sha1_file(sha1, &type, &size); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 218 | if (!tree || type != OBJ_TREE) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 219 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
| 220 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 221 | init_tree_desc(&inner, tree, size); |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 222 | show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 223 | |
| 224 | free(tree); |
| 225 | free(newbase); |
David Rientjes | cf995ed | 2006-08-14 13:39:27 -0700 | [diff] [blame] | 226 | } else { |
| 227 | opt->add_remove(opt, prefix[0], mode, sha1, base, path); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 228 | } |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 231 | static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt) |
| 232 | { |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 233 | int all_interesting = 0; |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 234 | while (t->size) { |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 235 | int show; |
| 236 | |
| 237 | if (all_interesting) |
| 238 | show = 1; |
| 239 | else { |
| 240 | show = tree_entry_interesting(t, base, baselen, opt); |
| 241 | if (show == 2) |
| 242 | all_interesting = 1; |
| 243 | } |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 244 | if (!show) { |
| 245 | update_tree_entry(t); |
| 246 | continue; |
| 247 | } |
| 248 | /* Skip it all? */ |
| 249 | if (show < 0) |
| 250 | t->size = 0; |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 255 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) |
| 256 | { |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 257 | int baselen = strlen(base); |
| 258 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 259 | for (;;) { |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 260 | if (DIFF_OPT_TST(opt, QUIET) && DIFF_OPT_TST(opt, HAS_CHANGES)) |
Junio C Hamano | 822cac0 | 2007-03-14 11:12:51 -0700 | [diff] [blame] | 261 | break; |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 262 | if (opt->nr_paths) { |
| 263 | skip_uninteresting(t1, base, baselen, opt); |
| 264 | skip_uninteresting(t2, base, baselen, opt); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 265 | } |
| 266 | if (!t1->size) { |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 267 | if (!t2->size) |
| 268 | break; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 269 | show_entry(opt, "+", t2, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 270 | update_tree_entry(t2); |
| 271 | continue; |
| 272 | } |
| 273 | if (!t2->size) { |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 274 | show_entry(opt, "-", t1, base, baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 275 | update_tree_entry(t1); |
| 276 | continue; |
| 277 | } |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 278 | switch (compare_tree_entry(t1, t2, base, baselen, opt)) { |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 279 | case -1: |
| 280 | update_tree_entry(t1); |
| 281 | continue; |
| 282 | case 0: |
| 283 | update_tree_entry(t1); |
| 284 | /* Fallthrough */ |
| 285 | case 1: |
| 286 | update_tree_entry(t2); |
| 287 | continue; |
| 288 | } |
| 289 | die("git-diff-tree: internal error"); |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 294 | /* |
| 295 | * Does it look like the resulting diff might be due to a rename? |
| 296 | * - single entry |
| 297 | * - not a valid previous file |
| 298 | */ |
| 299 | static inline int diff_might_be_rename(void) |
| 300 | { |
| 301 | return diff_queued_diff.nr == 1 && |
| 302 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); |
| 303 | } |
| 304 | |
| 305 | static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) |
| 306 | { |
| 307 | struct diff_options diff_opts; |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 308 | struct diff_queue_struct *q = &diff_queued_diff; |
| 309 | struct diff_filepair *choice; |
| 310 | const char *paths[1]; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 311 | int i; |
| 312 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 313 | /* Remove the file creation entry from the diff queue, and remember it */ |
| 314 | choice = q->queue[0]; |
| 315 | q->nr = 0; |
| 316 | |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 317 | diff_setup(&diff_opts); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 318 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 319 | diff_opts.detect_rename = DIFF_DETECT_RENAME; |
| 320 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
| 321 | diff_opts.single_follow = opt->paths[0]; |
Linus Torvalds | 6dd4b66 | 2007-10-20 12:31:31 -0700 | [diff] [blame] | 322 | diff_opts.break_opt = opt->break_opt; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 323 | paths[0] = NULL; |
| 324 | diff_tree_setup_paths(paths, &diff_opts); |
| 325 | if (diff_setup_done(&diff_opts) < 0) |
| 326 | die("unable to set up diff options to follow renames"); |
| 327 | diff_tree(t1, t2, base, &diff_opts); |
| 328 | diffcore_std(&diff_opts); |
Mike Hommey | 03b69c7 | 2007-12-11 22:59:55 +0100 | [diff] [blame] | 329 | diff_tree_release_paths(&diff_opts); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 330 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 331 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
| 332 | for (i = 0; i < q->nr; i++) { |
| 333 | struct diff_filepair *p = q->queue[i]; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * Found a source? Not only do we use that for the new |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 337 | * diff_queued_diff, we will also use that as the path in |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 338 | * the future! |
| 339 | */ |
| 340 | if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) { |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 341 | /* Switch the file-pairs around */ |
| 342 | q->queue[i] = choice; |
| 343 | choice = p; |
| 344 | |
| 345 | /* Update the path we use from now on.. */ |
Mike Hommey | 03b69c7 | 2007-12-11 22:59:55 +0100 | [diff] [blame] | 346 | diff_tree_release_paths(opt); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 347 | opt->paths[0] = xstrdup(p->one->path); |
| 348 | diff_tree_setup_paths(opt->paths, opt); |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 354 | * Then, discard all the non-relevane file pairs... |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 355 | */ |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 356 | for (i = 0; i < q->nr; i++) { |
| 357 | struct diff_filepair *p = q->queue[i]; |
| 358 | diff_free_filepair(p); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * .. and re-instate the one we want (which might be either the |
| 363 | * original one, or the rename/copy we found) |
| 364 | */ |
| 365 | q->queue[0] = choice; |
| 366 | q->nr = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 369 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) |
| 370 | { |
| 371 | void *tree1, *tree2; |
| 372 | struct tree_desc t1, t2; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 373 | unsigned long size1, size2; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 374 | int retval; |
| 375 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 376 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 377 | if (!tree1) |
| 378 | die("unable to read source tree (%s)", sha1_to_hex(old)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 379 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 380 | if (!tree2) |
| 381 | die("unable to read destination tree (%s)", sha1_to_hex(new)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 382 | init_tree_desc(&t1, tree1, size1); |
| 383 | init_tree_desc(&t2, tree2, size2); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 384 | retval = diff_tree(&t1, &t2, base, opt); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 385 | if (DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 386 | init_tree_desc(&t1, tree1, size1); |
| 387 | init_tree_desc(&t2, tree2, size2); |
| 388 | try_to_follow_renames(&t1, &t2, base, opt); |
| 389 | } |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 390 | free(tree1); |
| 391 | free(tree2); |
| 392 | return retval; |
| 393 | } |
| 394 | |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 395 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
| 396 | { |
| 397 | int retval; |
| 398 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 399 | unsigned long size; |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 400 | struct tree_desc empty, real; |
| 401 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 402 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 403 | if (!tree) |
| 404 | die("unable to read root tree (%s)", sha1_to_hex(new)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 405 | init_tree_desc(&real, tree, size); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 406 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 407 | init_tree_desc(&empty, "", 0); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 408 | retval = diff_tree(&empty, &real, base, opt); |
| 409 | free(tree); |
| 410 | return retval; |
| 411 | } |
| 412 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 413 | static int count_paths(const char **paths) |
| 414 | { |
| 415 | int i = 0; |
| 416 | while (*paths++) |
| 417 | i++; |
| 418 | return i; |
| 419 | } |
| 420 | |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 421 | void diff_tree_release_paths(struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 422 | { |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 423 | free(opt->pathlens); |
| 424 | } |
| 425 | |
| 426 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) |
| 427 | { |
| 428 | opt->nr_paths = 0; |
| 429 | opt->pathlens = NULL; |
| 430 | opt->paths = NULL; |
| 431 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 432 | if (p) { |
| 433 | int i; |
| 434 | |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 435 | opt->paths = p; |
| 436 | opt->nr_paths = count_paths(p); |
| 437 | if (opt->nr_paths == 0) { |
| 438 | opt->pathlens = NULL; |
Junio C Hamano | 7e4a2a8 | 2005-12-26 12:34:56 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 441 | opt->pathlens = xmalloc(opt->nr_paths * sizeof(int)); |
| 442 | for (i=0; i < opt->nr_paths; i++) |
| 443 | opt->pathlens[i] = strlen(p[i]); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 444 | } |
| 445 | } |