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