Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 1 | #include <ctype.h> |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 2 | #include "cache.h" |
Junio C Hamano | 3ebfd4a | 2005-04-27 16:21:00 | [diff] [blame] | 3 | #include "diff.h" |
Linus Torvalds | e3bc7a3 | 2005-06-01 15:34:23 | [diff] [blame] | 4 | #include "commit.h" |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 5 | |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 6 | static int show_root_diff = 0; |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 7 | static int verbose_header = 0; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 8 | static int ignore_merges = 1; |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 9 | static int recursive = 0; |
Junio C Hamano | 4cae1a9 | 2005-05-25 06:24:22 | [diff] [blame] | 10 | static int show_tree_entry_in_recursive = 0; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 11 | static int read_stdin = 0; |
Linus Torvalds | e68b6f1 | 2005-07-15 00:59:17 | [diff] [blame] | 12 | static int diff_output_format = DIFF_FORMAT_RAW; |
| 13 | static int diff_line_termination = '\n'; |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 14 | static int detect_rename = 0; |
Junio C Hamano | a7ca654 | 2005-06-11 01:31:02 | [diff] [blame] | 15 | static int find_copies_harder = 0; |
Junio C Hamano | 19feebc | 2005-05-27 22:54:37 | [diff] [blame] | 16 | static int diff_setup_opt = 0; |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 17 | static int diff_score_opt = 0; |
Junio C Hamano | 057c7d3 | 2005-05-21 22:02:51 | [diff] [blame] | 18 | static const char *pickaxe = NULL; |
Junio C Hamano | 367cec1 | 2005-05-27 22:55:28 | [diff] [blame] | 19 | static int pickaxe_opts = 0; |
Junio C Hamano | f345b0a | 2005-05-30 07:08:37 | [diff] [blame] | 20 | static int diff_break_opt = -1; |
Junio C Hamano | af5323e | 2005-05-30 07:09:07 | [diff] [blame] | 21 | static const char *orderfile = NULL; |
Junio C Hamano | f2ce9fd | 2005-06-12 03:57:13 | [diff] [blame] | 22 | static const char *diff_filter = NULL; |
Linus Torvalds | f4f21ce | 2005-05-06 17:56:35 | [diff] [blame] | 23 | static const char *header = NULL; |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 24 | static const char *header_prefix = ""; |
Linus Torvalds | 000182e | 2005-06-05 16:02:03 | [diff] [blame] | 25 | static enum cmit_fmt commit_format = CMIT_FMT_RAW; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 26 | |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 27 | // What paths are we interested in? |
| 28 | static int nr_paths = 0; |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 29 | static const char **paths = NULL; |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 30 | static int *pathlens = NULL; |
| 31 | |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 32 | static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base); |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 33 | |
| 34 | static void update_tree_entry(void **bufp, unsigned long *sizep) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 35 | { |
| 36 | void *buf = *bufp; |
| 37 | unsigned long size = *sizep; |
| 38 | int len = strlen(buf) + 1 + 20; |
| 39 | |
| 40 | if (size < len) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 41 | die("corrupt tree file"); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 42 | *bufp = buf + len; |
| 43 | *sizep = size - len; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep) |
| 47 | { |
| 48 | int len = strlen(tree)+1; |
| 49 | const unsigned char *sha1 = tree + len; |
| 50 | const char *path = strchr(tree, ' '); |
Junio C Hamano | 67574c4 | 2005-06-01 18:38:07 | [diff] [blame] | 51 | unsigned int mode; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 52 | |
Junio C Hamano | 67574c4 | 2005-06-01 18:38:07 | [diff] [blame] | 53 | if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 54 | die("corrupt tree file"); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 55 | *pathp = path+1; |
Junio C Hamano | 67574c4 | 2005-06-01 18:38:07 | [diff] [blame] | 56 | *modep = DIFF_FILE_CANON_MODE(mode); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 57 | return sha1; |
| 58 | } |
| 59 | |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 60 | static char *malloc_base(const char *base, const char *path, int pathlen) |
| 61 | { |
| 62 | int baselen = strlen(base); |
Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 63 | char *newbase = xmalloc(baselen + pathlen + 2); |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 64 | memcpy(newbase, base, baselen); |
| 65 | memcpy(newbase + baselen, path, pathlen); |
| 66 | memcpy(newbase + baselen + pathlen, "/", 2); |
| 67 | return newbase; |
| 68 | } |
| 69 | |
| 70 | static void show_file(const char *prefix, void *tree, unsigned long size, const char *base); |
Linus Torvalds | ed1a368 | 2005-05-18 21:07:42 | [diff] [blame] | 71 | static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base); |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 72 | |
| 73 | /* A file entry went away or appeared */ |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 74 | static void show_file(const char *prefix, void *tree, unsigned long size, const char *base) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 75 | { |
| 76 | unsigned mode; |
| 77 | const char *path; |
| 78 | const unsigned char *sha1 = extract(tree, size, &path, &mode); |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 79 | |
| 80 | if (recursive && S_ISDIR(mode)) { |
| 81 | char type[20]; |
| 82 | unsigned long size; |
| 83 | char *newbase = malloc_base(base, path, strlen(path)); |
| 84 | void *tree; |
| 85 | |
| 86 | tree = read_sha1_file(sha1, type, &size); |
| 87 | if (!tree || strcmp(type, "tree")) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 88 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 89 | |
| 90 | show_tree(prefix, tree, size, newbase); |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 91 | |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 92 | free(tree); |
| 93 | free(newbase); |
| 94 | return; |
| 95 | } |
| 96 | |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 97 | diff_addremove(prefix[0], mode, sha1, base, path); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 98 | } |
| 99 | |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 100 | static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 101 | { |
| 102 | unsigned mode1, mode2; |
| 103 | const char *path1, *path2; |
| 104 | const unsigned char *sha1, *sha2; |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 105 | int cmp, pathlen1, pathlen2; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 106 | |
| 107 | sha1 = extract(tree1, size1, &path1, &mode1); |
| 108 | sha2 = extract(tree2, size2, &path2, &mode2); |
| 109 | |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 110 | pathlen1 = strlen(path1); |
| 111 | pathlen2 = strlen(path2); |
Linus Torvalds | e46091d | 2005-05-20 16:11:46 | [diff] [blame] | 112 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 113 | if (cmp < 0) { |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 114 | show_file("-", tree1, size1, base); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | if (cmp > 0) { |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 118 | show_file("+", tree2, size2, base); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 119 | return 1; |
| 120 | } |
Junio C Hamano | a7ca654 | 2005-06-11 01:31:02 | [diff] [blame] | 121 | if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 122 | return 0; |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * If the filemode has changed to/from a directory from/to a regular |
Ingo Molnar | aebb267 | 2005-04-12 18:36:26 | [diff] [blame] | 126 | * file, we need to consider it a remove and an add. |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 127 | */ |
| 128 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { |
| 129 | show_file("-", tree1, size1, base); |
| 130 | show_file("+", tree2, size2, base); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (recursive && S_ISDIR(mode1)) { |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 135 | int retval; |
Linus Torvalds | 262e82b | 2005-04-11 04:49:26 | [diff] [blame] | 136 | char *newbase = malloc_base(base, path1, pathlen1); |
Junio C Hamano | 4cae1a9 | 2005-05-25 06:24:22 | [diff] [blame] | 137 | if (show_tree_entry_in_recursive) |
| 138 | diff_change(mode1, mode2, sha1, sha2, base, path1); |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 139 | retval = diff_tree_sha1(sha1, sha2, newbase); |
| 140 | free(newbase); |
| 141 | return retval; |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 142 | } |
| 143 | |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 144 | diff_change(mode1, mode2, sha1, sha2, base, path1); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 148 | static int interesting(void *tree, unsigned long size, const char *base) |
| 149 | { |
| 150 | const char *path; |
| 151 | unsigned mode; |
| 152 | int i; |
| 153 | int baselen, pathlen; |
| 154 | |
| 155 | if (!nr_paths) |
| 156 | return 1; |
| 157 | |
| 158 | (void)extract(tree, size, &path, &mode); |
| 159 | |
| 160 | pathlen = strlen(path); |
| 161 | baselen = strlen(base); |
| 162 | |
| 163 | for (i=0; i < nr_paths; i++) { |
| 164 | const char *match = paths[i]; |
| 165 | int matchlen = pathlens[i]; |
| 166 | |
| 167 | if (baselen >= matchlen) { |
| 168 | /* If it doesn't match, move along... */ |
| 169 | if (strncmp(base, match, matchlen)) |
| 170 | continue; |
| 171 | |
| 172 | /* The base is a subdirectory of a path which was specified. */ |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | /* Does the base match? */ |
| 177 | if (strncmp(base, match, baselen)) |
| 178 | continue; |
| 179 | |
| 180 | match += baselen; |
| 181 | matchlen -= baselen; |
| 182 | |
| 183 | if (pathlen > matchlen) |
| 184 | continue; |
| 185 | |
Linus Torvalds | cb6c8ed | 2005-05-18 20:50:24 | [diff] [blame] | 186 | if (matchlen > pathlen) { |
| 187 | if (match[pathlen] != '/') |
| 188 | continue; |
Linus Torvalds | 850e82d | 2005-05-18 21:17:22 | [diff] [blame] | 189 | if (!S_ISDIR(mode)) |
| 190 | continue; |
Linus Torvalds | cb6c8ed | 2005-05-18 20:50:24 | [diff] [blame] | 191 | } |
| 192 | |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 193 | if (strncmp(path, match, pathlen)) |
| 194 | continue; |
| 195 | |
| 196 | return 1; |
| 197 | } |
| 198 | return 0; /* No matches */ |
| 199 | } |
| 200 | |
Linus Torvalds | ed1a368 | 2005-05-18 21:07:42 | [diff] [blame] | 201 | /* A whole sub-tree went away or appeared */ |
| 202 | static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base) |
| 203 | { |
| 204 | while (size) { |
Junio C Hamano | 4727f64 | 2005-06-19 20:14:05 | [diff] [blame] | 205 | if (interesting(tree, size, base)) |
Linus Torvalds | ed1a368 | 2005-05-18 21:07:42 | [diff] [blame] | 206 | show_file(prefix, tree, size, base); |
| 207 | update_tree_entry(&tree, &size); |
| 208 | } |
| 209 | } |
| 210 | |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 211 | static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 212 | { |
| 213 | while (size1 | size2) { |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 214 | if (nr_paths && size1 && !interesting(tree1, size1, base)) { |
| 215 | update_tree_entry(&tree1, &size1); |
| 216 | continue; |
| 217 | } |
| 218 | if (nr_paths && size2 && !interesting(tree2, size2, base)) { |
| 219 | update_tree_entry(&tree2, &size2); |
| 220 | continue; |
| 221 | } |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 222 | if (!size1) { |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 223 | show_file("+", tree2, size2, base); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 224 | update_tree_entry(&tree2, &size2); |
| 225 | continue; |
| 226 | } |
| 227 | if (!size2) { |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 228 | show_file("-", tree1, size1, base); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 229 | update_tree_entry(&tree1, &size1); |
| 230 | continue; |
| 231 | } |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 232 | switch (compare_tree_entry(tree1, size1, tree2, size2, base)) { |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 233 | case -1: |
| 234 | update_tree_entry(&tree1, &size1); |
| 235 | continue; |
| 236 | case 0: |
| 237 | update_tree_entry(&tree1, &size1); |
| 238 | /* Fallthrough */ |
| 239 | case 1: |
| 240 | update_tree_entry(&tree2, &size2); |
| 241 | continue; |
| 242 | } |
Alexey Nezhdanov | 667bb59 | 2005-05-19 11:17:16 | [diff] [blame] | 243 | die("git-diff-tree: internal error"); |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 248 | static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base) |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 249 | { |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 250 | void *tree1, *tree2; |
| 251 | unsigned long size1, size2; |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 252 | int retval; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 253 | |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 254 | tree1 = read_object_with_reference(old, "tree", &size1, NULL); |
Junio C Hamano | c1fdf2a | 2005-04-21 01:06:50 | [diff] [blame] | 255 | if (!tree1) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 256 | die("unable to read source tree (%s)", sha1_to_hex(old)); |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 257 | tree2 = read_object_with_reference(new, "tree", &size2, NULL); |
Junio C Hamano | c1fdf2a | 2005-04-21 01:06:50 | [diff] [blame] | 258 | if (!tree2) |
Petr Baudis | 2de381f | 2005-04-13 09:28:48 | [diff] [blame] | 259 | die("unable to read destination tree (%s)", sha1_to_hex(new)); |
Linus Torvalds | eeb7991 | 2005-04-10 22:08:02 | [diff] [blame] | 260 | retval = diff_tree(tree1, size1, tree2, size2, base); |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 261 | free(tree1); |
| 262 | free(tree2); |
| 263 | return retval; |
| 264 | } |
| 265 | |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 266 | static void call_diff_setup(void) |
| 267 | { |
Junio C Hamano | 19feebc | 2005-05-27 22:54:37 | [diff] [blame] | 268 | diff_setup(diff_setup_opt); |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 269 | } |
| 270 | |
Linus Torvalds | 09d74b3 | 2005-05-22 21:33:43 | [diff] [blame] | 271 | static int call_diff_flush(void) |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 272 | { |
Junio C Hamano | 4727f64 | 2005-06-19 20:14:05 | [diff] [blame] | 273 | diffcore_std(NULL, |
Junio C Hamano | befe863 | 2005-05-29 23:56:13 | [diff] [blame] | 274 | detect_rename, diff_score_opt, |
Junio C Hamano | f345b0a | 2005-05-30 07:08:37 | [diff] [blame] | 275 | pickaxe, pickaxe_opts, |
Junio C Hamano | af5323e | 2005-05-30 07:09:07 | [diff] [blame] | 276 | diff_break_opt, |
Junio C Hamano | f2ce9fd | 2005-06-12 03:57:13 | [diff] [blame] | 277 | orderfile, |
| 278 | diff_filter); |
Linus Torvalds | 9ab55bd | 2005-05-23 23:37:47 | [diff] [blame] | 279 | if (diff_queue_is_empty()) { |
Linus Torvalds | e68b6f1 | 2005-07-15 00:59:17 | [diff] [blame] | 280 | diff_flush(DIFF_FORMAT_NO_OUTPUT, diff_line_termination); |
Linus Torvalds | 9ab55bd | 2005-05-23 23:37:47 | [diff] [blame] | 281 | return 0; |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 282 | } |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 283 | if (header) { |
Linus Torvalds | e68b6f1 | 2005-07-15 00:59:17 | [diff] [blame] | 284 | printf("%s%c", header, diff_line_termination); |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 285 | header = NULL; |
| 286 | } |
Linus Torvalds | e68b6f1 | 2005-07-15 00:59:17 | [diff] [blame] | 287 | diff_flush(diff_output_format, diff_line_termination); |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 288 | return 1; |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 289 | } |
| 290 | |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 291 | static int diff_tree_sha1_top(const unsigned char *old, |
| 292 | const unsigned char *new, const char *base) |
| 293 | { |
| 294 | int ret; |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 295 | |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 296 | call_diff_setup(); |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 297 | ret = diff_tree_sha1(old, new, base); |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 298 | call_diff_flush(); |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 299 | return ret; |
| 300 | } |
| 301 | |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 302 | static int diff_root_tree(const unsigned char *new, const char *base) |
| 303 | { |
| 304 | int retval; |
| 305 | void *tree; |
| 306 | unsigned long size; |
| 307 | |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 308 | call_diff_setup(); |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 309 | tree = read_object_with_reference(new, "tree", &size, NULL); |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 310 | if (!tree) |
| 311 | die("unable to read root tree (%s)", sha1_to_hex(new)); |
| 312 | retval = diff_tree("", 0, tree, size, base); |
| 313 | free(tree); |
Junio C Hamano | 38c6f78 | 2005-05-22 02:40:36 | [diff] [blame] | 314 | call_diff_flush(); |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 315 | return retval; |
| 316 | } |
| 317 | |
Linus Torvalds | 1809266 | 2005-06-23 20:56:55 | [diff] [blame] | 318 | static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len) |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 319 | { |
Linus Torvalds | 3258c90 | 2005-05-21 18:04:19 | [diff] [blame] | 320 | static char this_header[16384]; |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 321 | int offset; |
| 322 | |
Linus Torvalds | 1809266 | 2005-06-23 20:56:55 | [diff] [blame] | 323 | if (!verbose_header) |
| 324 | return commit; |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 325 | |
Linus Torvalds | 1809266 | 2005-06-23 20:56:55 | [diff] [blame] | 326 | offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent); |
| 327 | offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset); |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 328 | return this_header; |
| 329 | } |
| 330 | |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 331 | static int diff_tree_commit(const unsigned char *commit, const char *name) |
| 332 | { |
| 333 | unsigned long size, offset; |
| 334 | char *buf = read_object_with_reference(commit, "commit", &size, NULL); |
| 335 | |
| 336 | if (!buf) |
| 337 | return -1; |
| 338 | |
Linus Torvalds | 7384889 | 2005-05-18 20:43:58 | [diff] [blame] | 339 | if (!name) { |
| 340 | static char commit_name[60]; |
| 341 | strcpy(commit_name, sha1_to_hex(commit)); |
| 342 | name = commit_name; |
| 343 | } |
| 344 | |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 345 | /* Root commit? */ |
| 346 | if (show_root_diff && memcmp(buf + 46, "parent ", 7)) { |
| 347 | header = generate_header(name, "root", buf, size); |
| 348 | diff_root_tree(commit, ""); |
| 349 | } |
| 350 | |
| 351 | /* More than one parent? */ |
| 352 | if (ignore_merges) { |
| 353 | if (!memcmp(buf + 46 + 48, "parent ", 7)) |
| 354 | return 0; |
| 355 | } |
| 356 | |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 357 | offset = 46; |
| 358 | while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) { |
| 359 | unsigned char parent[20]; |
| 360 | if (get_sha1_hex(buf + offset + 7, parent)) |
| 361 | return -1; |
| 362 | header = generate_header(name, sha1_to_hex(parent), buf, size); |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 363 | diff_tree_sha1_top(parent, commit, ""); |
Linus Torvalds | d6db010 | 2005-05-21 22:42:53 | [diff] [blame] | 364 | if (!header && verbose_header) { |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 365 | header_prefix = "\ndiff-tree "; |
Linus Torvalds | d6db010 | 2005-05-21 22:42:53 | [diff] [blame] | 366 | /* |
| 367 | * Don't print multiple merge entries if we |
| 368 | * don't print the diffs. |
| 369 | */ |
Linus Torvalds | d6db010 | 2005-05-21 22:42:53 | [diff] [blame] | 370 | } |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 371 | offset += 48; |
| 372 | } |
Junio C Hamano | 5098baf | 2005-09-15 23:13:43 | [diff] [blame] | 373 | free(buf); |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 377 | static int diff_tree_stdin(char *line) |
| 378 | { |
| 379 | int len = strlen(line); |
| 380 | unsigned char commit[20], parent[20]; |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 381 | static char this_header[1000]; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 382 | |
| 383 | if (!len || line[len-1] != '\n') |
| 384 | return -1; |
| 385 | line[len-1] = 0; |
| 386 | if (get_sha1_hex(line, commit)) |
| 387 | return -1; |
| 388 | if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) { |
Linus Torvalds | f4f21ce | 2005-05-06 17:56:35 | [diff] [blame] | 389 | line[40] = 0; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 390 | line[81] = 0; |
Linus Torvalds | f4f21ce | 2005-05-06 17:56:35 | [diff] [blame] | 391 | sprintf(this_header, "%s (from %s)\n", line, line+41); |
| 392 | header = this_header; |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 393 | return diff_tree_sha1_top(parent, commit, ""); |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 394 | } |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 395 | line[40] = 0; |
Linus Torvalds | b11645b | 2005-05-18 20:06:47 | [diff] [blame] | 396 | return diff_tree_commit(commit, line); |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 397 | } |
| 398 | |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 399 | static int count_paths(const char **paths) |
| 400 | { |
| 401 | int i = 0; |
| 402 | while (*paths++) |
| 403 | i++; |
| 404 | return i; |
| 405 | } |
| 406 | |
Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 407 | static const char diff_tree_usage[] = |
Junio C Hamano | dda2d79 | 2005-07-13 19:52:35 | [diff] [blame] | 408 | "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] " |
| 409 | "[<common diff options>] <tree-ish> <tree-ish>" |
| 410 | COMMON_DIFF_OPTIONS_HELP; |
Junio C Hamano | a8db165 | 2005-06-13 00:44:21 | [diff] [blame] | 411 | |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 412 | int main(int argc, char **argv) |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 413 | { |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 414 | int nr_sha1; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 415 | char line[1000]; |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 416 | unsigned char sha1[2][20]; |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 417 | const char *prefix = setup_git_directory(); |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 418 | |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 419 | nr_sha1 = 0; |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 420 | for (;;) { |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 421 | const char *arg; |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 422 | |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 423 | argv++; |
| 424 | argc--; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 425 | arg = *argv; |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 426 | if (!arg) |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 427 | break; |
| 428 | |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 429 | if (*arg != '-') { |
| 430 | if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) { |
| 431 | nr_sha1++; |
| 432 | continue; |
| 433 | } |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | if (!strcmp(arg, "--")) { |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 438 | argv++; |
| 439 | argc--; |
| 440 | break; |
| 441 | } |
Linus Torvalds | bf16c71 | 2005-04-11 15:37:17 | [diff] [blame] | 442 | if (!strcmp(arg, "-r")) { |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 443 | recursive = 1; |
| 444 | continue; |
| 445 | } |
Junio C Hamano | 4cae1a9 | 2005-05-25 06:24:22 | [diff] [blame] | 446 | if (!strcmp(arg, "-t")) { |
| 447 | recursive = show_tree_entry_in_recursive = 1; |
| 448 | continue; |
| 449 | } |
Linus Torvalds | de809db | 2005-05-20 05:39:27 | [diff] [blame] | 450 | if (!strcmp(arg, "-R")) { |
Junio C Hamano | 19feebc | 2005-05-27 22:54:37 | [diff] [blame] | 451 | diff_setup_opt |= DIFF_SETUP_REVERSE; |
Linus Torvalds | de809db | 2005-05-20 05:39:27 | [diff] [blame] | 452 | continue; |
| 453 | } |
Linus Torvalds | acb46f8 | 2005-07-08 17:45:07 | [diff] [blame] | 454 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) { |
Junio C Hamano | 81e50ea | 2005-05-22 02:42:18 | [diff] [blame] | 455 | diff_output_format = DIFF_FORMAT_PATCH; |
| 456 | recursive = 1; |
Junio C Hamano | 3ebfd4a | 2005-04-27 16:21:00 | [diff] [blame] | 457 | continue; |
| 458 | } |
Junio C Hamano | 52e9578 | 2005-05-21 09:40:01 | [diff] [blame] | 459 | if (!strncmp(arg, "-S", 2)) { |
| 460 | pickaxe = arg + 2; |
| 461 | continue; |
| 462 | } |
Junio C Hamano | af5323e | 2005-05-30 07:09:07 | [diff] [blame] | 463 | if (!strncmp(arg, "-O", 2)) { |
| 464 | orderfile = arg + 2; |
| 465 | continue; |
| 466 | } |
Junio C Hamano | f2ce9fd | 2005-06-12 03:57:13 | [diff] [blame] | 467 | if (!strncmp(arg, "--diff-filter=", 14)) { |
| 468 | diff_filter = arg + 14; |
| 469 | continue; |
| 470 | } |
Junio C Hamano | 367cec1 | 2005-05-27 22:55:28 | [diff] [blame] | 471 | if (!strcmp(arg, "--pickaxe-all")) { |
| 472 | pickaxe_opts = DIFF_PICKAXE_ALL; |
| 473 | continue; |
| 474 | } |
Junio C Hamano | 57fe64a | 2005-05-20 02:00:36 | [diff] [blame] | 475 | if (!strncmp(arg, "-M", 2)) { |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 476 | detect_rename = DIFF_DETECT_RENAME; |
Junio C Hamano | 0e3994f | 2005-06-03 08:37:54 | [diff] [blame] | 477 | if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1) |
| 478 | usage(diff_tree_usage); |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 479 | continue; |
| 480 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 09:39:09 | [diff] [blame] | 481 | if (!strncmp(arg, "-C", 2)) { |
Junio C Hamano | 6b14d7f | 2005-05-22 17:04:37 | [diff] [blame] | 482 | detect_rename = DIFF_DETECT_COPY; |
Junio C Hamano | 0e3994f | 2005-06-03 08:37:54 | [diff] [blame] | 483 | if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1) |
| 484 | usage(diff_tree_usage); |
Junio C Hamano | 427dcb4 | 2005-05-21 09:39:09 | [diff] [blame] | 485 | continue; |
| 486 | } |
Junio C Hamano | f345b0a | 2005-05-30 07:08:37 | [diff] [blame] | 487 | if (!strncmp(arg, "-B", 2)) { |
Junio C Hamano | 0e3994f | 2005-06-03 08:37:54 | [diff] [blame] | 488 | if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1) |
| 489 | usage(diff_tree_usage); |
Junio C Hamano | f345b0a | 2005-05-30 07:08:37 | [diff] [blame] | 490 | continue; |
| 491 | } |
Junio C Hamano | a7ca654 | 2005-06-11 01:31:02 | [diff] [blame] | 492 | if (!strcmp(arg, "--find-copies-harder")) { |
| 493 | find_copies_harder = 1; |
| 494 | continue; |
| 495 | } |
Junio C Hamano | 52f2852 | 2005-07-13 19:45:51 | [diff] [blame] | 496 | if (!strcmp(arg, "--name-only")) { |
| 497 | diff_output_format = DIFF_FORMAT_NAME; |
| 498 | continue; |
| 499 | } |
Linus Torvalds | 6cbd72f | 2005-04-15 22:11:57 | [diff] [blame] | 500 | if (!strcmp(arg, "-z")) { |
Linus Torvalds | e68b6f1 | 2005-07-15 00:59:17 | [diff] [blame] | 501 | diff_line_termination = 0; |
Linus Torvalds | 6cbd72f | 2005-04-15 22:11:57 | [diff] [blame] | 502 | continue; |
| 503 | } |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 504 | if (!strcmp(arg, "-m")) { |
| 505 | ignore_merges = 0; |
| 506 | continue; |
| 507 | } |
Linus Torvalds | f4f21ce | 2005-05-06 17:56:35 | [diff] [blame] | 508 | if (!strcmp(arg, "-s")) { |
Junio C Hamano | d030935 | 2005-05-24 09:05:08 | [diff] [blame] | 509 | diff_output_format = DIFF_FORMAT_NO_OUTPUT; |
Linus Torvalds | f4f21ce | 2005-05-06 17:56:35 | [diff] [blame] | 510 | continue; |
| 511 | } |
Linus Torvalds | cee99d2 | 2005-05-06 18:42:47 | [diff] [blame] | 512 | if (!strcmp(arg, "-v")) { |
| 513 | verbose_header = 1; |
| 514 | header_prefix = "diff-tree "; |
| 515 | continue; |
| 516 | } |
Junio C Hamano | a8db165 | 2005-06-13 00:44:21 | [diff] [blame] | 517 | if (!strncmp(arg, "--pretty", 8)) { |
| 518 | verbose_header = 1; |
Linus Torvalds | ba88e54 | 2005-06-13 03:34:09 | [diff] [blame] | 519 | header_prefix = "diff-tree "; |
Junio C Hamano | a8db165 | 2005-06-13 00:44:21 | [diff] [blame] | 520 | commit_format = get_commit_format(arg+8); |
| 521 | continue; |
| 522 | } |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 523 | if (!strcmp(arg, "--stdin")) { |
| 524 | read_stdin = 1; |
| 525 | continue; |
| 526 | } |
Linus Torvalds | dc26bd8 | 2005-05-19 20:44:29 | [diff] [blame] | 527 | if (!strcmp(arg, "--root")) { |
| 528 | show_root_diff = 1; |
| 529 | continue; |
| 530 | } |
Junio C Hamano | c5bac17 | 2005-04-21 02:49:16 | [diff] [blame] | 531 | usage(diff_tree_usage); |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 532 | } |
Junio C Hamano | a7ca654 | 2005-06-11 01:31:02 | [diff] [blame] | 533 | if (find_copies_harder && detect_rename != DIFF_DETECT_COPY) |
| 534 | usage(diff_tree_usage); |
Linus Torvalds | 73134b6 | 2005-04-10 21:03:58 | [diff] [blame] | 535 | |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 536 | paths = get_pathspec(prefix, argv); |
| 537 | if (paths) { |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 538 | int i; |
| 539 | |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 540 | nr_paths = count_paths(paths); |
Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 541 | pathlens = xmalloc(nr_paths * sizeof(int)); |
Linus Torvalds | c5b4238 | 2005-04-24 05:08:00 | [diff] [blame] | 542 | for (i=0; i<nr_paths; i++) |
| 543 | pathlens[i] = strlen(paths[i]); |
| 544 | } |
| 545 | |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 546 | switch (nr_sha1) { |
| 547 | case 0: |
| 548 | if (!read_stdin) |
| 549 | usage(diff_tree_usage); |
| 550 | break; |
| 551 | case 1: |
Linus Torvalds | 7384889 | 2005-05-18 20:43:58 | [diff] [blame] | 552 | diff_tree_commit(sha1[0], NULL); |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 553 | break; |
| 554 | case 2: |
Junio C Hamano | 5c97558 | 2005-05-19 10:32:35 | [diff] [blame] | 555 | diff_tree_sha1_top(sha1[0], sha1[1], ""); |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 559 | if (!read_stdin) |
Linus Torvalds | 0a8365a | 2005-05-18 20:10:17 | [diff] [blame] | 560 | return 0; |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 561 | |
Junio C Hamano | f0c6b2a | 2005-05-27 22:56:38 | [diff] [blame] | 562 | if (detect_rename) |
| 563 | diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE | |
| 564 | DIFF_SETUP_USE_CACHE); |
Linus Torvalds | e0965d8 | 2005-05-06 17:03:17 | [diff] [blame] | 565 | while (fgets(line, sizeof(line), stdin)) |
| 566 | diff_tree_stdin(line); |
| 567 | |
| 568 | return 0; |
Linus Torvalds | 9174026 | 2005-04-09 20:00:54 | [diff] [blame] | 569 | } |