Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 4 | #include "cache.h" |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 5 | #include "quote.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6 | #include "commit.h" |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 7 | #include "diff.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 8 | #include "diffcore.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 9 | #include "revision.h" |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 10 | #include "cache-tree.h" |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 11 | #include "path-list.h" |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 12 | #include "unpack-trees.h" |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 13 | #include "refs.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 15 | /* |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 16 | * diff-files |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 17 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 18 | |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 19 | static int read_directory(const char *path, struct path_list *list) |
| 20 | { |
| 21 | DIR *dir; |
| 22 | struct dirent *e; |
| 23 | |
| 24 | if (!(dir = opendir(path))) |
| 25 | return error("Could not open directory %s", path); |
| 26 | |
| 27 | while ((e = readdir(dir))) |
| 28 | if (strcmp(".", e->d_name) && strcmp("..", e->d_name)) |
René Scharfe | 4d3f4b8 | 2007-07-07 20:19:08 +0200 | [diff] [blame] | 29 | path_list_insert(e->d_name, list); |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 30 | |
| 31 | closedir(dir); |
| 32 | return 0; |
| 33 | } |
| 34 | |
Johannes Schindelin | 0c725f1 | 2007-02-25 23:36:31 +0100 | [diff] [blame] | 35 | static int get_mode(const char *path, int *mode) |
| 36 | { |
| 37 | struct stat st; |
| 38 | |
| 39 | if (!path || !strcmp(path, "/dev/null")) |
| 40 | *mode = 0; |
| 41 | else if (!strcmp(path, "-")) |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 42 | *mode = create_ce_mode(0666); |
Johannes Schindelin | 0c725f1 | 2007-02-25 23:36:31 +0100 | [diff] [blame] | 43 | else if (stat(path, &st)) |
| 44 | return error("Could not access '%s'", path); |
| 45 | else |
| 46 | *mode = st.st_mode; |
| 47 | return 0; |
| 48 | } |
| 49 | |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 50 | static int queue_diff(struct diff_options *o, |
| 51 | const char *name1, const char *name2) |
| 52 | { |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 53 | int mode1 = 0, mode2 = 0; |
| 54 | |
Johannes Schindelin | 0c725f1 | 2007-02-25 23:36:31 +0100 | [diff] [blame] | 55 | if (get_mode(name1, &mode1) || get_mode(name2, &mode2)) |
| 56 | return -1; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 57 | |
| 58 | if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) |
| 59 | return error("file/directory conflict: %s, %s", name1, name2); |
| 60 | |
| 61 | if (S_ISDIR(mode1) || S_ISDIR(mode2)) { |
| 62 | char buffer1[PATH_MAX], buffer2[PATH_MAX]; |
| 63 | struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1}; |
| 64 | int len1 = 0, len2 = 0, i1, i2, ret = 0; |
| 65 | |
| 66 | if (name1 && read_directory(name1, &p1)) |
| 67 | return -1; |
| 68 | if (name2 && read_directory(name2, &p2)) { |
| 69 | path_list_clear(&p1, 0); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | if (name1) { |
| 74 | len1 = strlen(name1); |
| 75 | if (len1 > 0 && name1[len1 - 1] == '/') |
| 76 | len1--; |
| 77 | memcpy(buffer1, name1, len1); |
| 78 | buffer1[len1++] = '/'; |
| 79 | } |
| 80 | |
| 81 | if (name2) { |
| 82 | len2 = strlen(name2); |
| 83 | if (len2 > 0 && name2[len2 - 1] == '/') |
| 84 | len2--; |
| 85 | memcpy(buffer2, name2, len2); |
| 86 | buffer2[len2++] = '/'; |
| 87 | } |
| 88 | |
| 89 | for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) { |
| 90 | const char *n1, *n2; |
| 91 | int comp; |
| 92 | |
| 93 | if (i1 == p1.nr) |
| 94 | comp = 1; |
| 95 | else if (i2 == p2.nr) |
| 96 | comp = -1; |
| 97 | else |
| 98 | comp = strcmp(p1.items[i1].path, |
| 99 | p2.items[i2].path); |
| 100 | |
| 101 | if (comp > 0) |
| 102 | n1 = NULL; |
| 103 | else { |
| 104 | n1 = buffer1; |
| 105 | strncpy(buffer1 + len1, p1.items[i1++].path, |
| 106 | PATH_MAX - len1); |
| 107 | } |
| 108 | |
| 109 | if (comp < 0) |
| 110 | n2 = NULL; |
| 111 | else { |
| 112 | n2 = buffer2; |
| 113 | strncpy(buffer2 + len2, p2.items[i2++].path, |
| 114 | PATH_MAX - len2); |
| 115 | } |
| 116 | |
| 117 | ret = queue_diff(o, n1, n2); |
| 118 | } |
| 119 | path_list_clear(&p1, 0); |
| 120 | path_list_clear(&p2, 0); |
| 121 | |
| 122 | return ret; |
| 123 | } else { |
| 124 | struct diff_filespec *d1, *d2; |
| 125 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 126 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 127 | unsigned tmp; |
| 128 | const char *tmp_c; |
| 129 | tmp = mode1; mode1 = mode2; mode2 = tmp; |
| 130 | tmp_c = name1; name1 = name2; name2 = tmp_c; |
| 131 | } |
| 132 | |
| 133 | if (!name1) |
| 134 | name1 = "/dev/null"; |
| 135 | if (!name2) |
| 136 | name2 = "/dev/null"; |
| 137 | d1 = alloc_filespec(name1); |
| 138 | d2 = alloc_filespec(name2); |
| 139 | fill_filespec(d1, null_sha1, mode1); |
| 140 | fill_filespec(d2, null_sha1, mode2); |
| 141 | |
| 142 | diff_queue(&diff_queued_diff, d1, d2); |
| 143 | return 0; |
| 144 | } |
| 145 | } |
| 146 | |
Junio C Hamano | 1ad029b | 2007-04-13 03:23:20 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Does the path name a blob in the working tree, or a directory |
| 149 | * in the working tree? |
| 150 | */ |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 151 | static int is_in_index(const char *path) |
| 152 | { |
Junio C Hamano | 1ad029b | 2007-04-13 03:23:20 -0700 | [diff] [blame] | 153 | int len, pos; |
| 154 | struct cache_entry *ce; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 155 | |
Junio C Hamano | 1ad029b | 2007-04-13 03:23:20 -0700 | [diff] [blame] | 156 | len = strlen(path); |
| 157 | while (path[len-1] == '/') |
| 158 | len--; |
| 159 | if (!len) |
| 160 | return 1; /* "." */ |
| 161 | pos = cache_name_pos(path, len); |
| 162 | if (0 <= pos) |
| 163 | return 1; |
| 164 | pos = -1 - pos; |
| 165 | while (pos < active_nr) { |
| 166 | ce = active_cache[pos++]; |
| 167 | if (ce_namelen(ce) <= len || |
| 168 | strncmp(ce->name, path, len) || |
| 169 | (ce->name[len] > '/')) |
| 170 | break; /* path cannot be a prefix */ |
| 171 | if (ce->name[len] == '/') |
| 172 | return 1; |
| 173 | } |
| 174 | return 0; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int handle_diff_files_args(struct rev_info *revs, |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 178 | int argc, const char **argv, |
| 179 | unsigned int *options) |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 180 | { |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 181 | *options = 0; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 182 | |
| 183 | /* revs->max_count == -2 means --no-index */ |
| 184 | while (1 < argc && argv[1][0] == '-') { |
| 185 | if (!strcmp(argv[1], "--base")) |
| 186 | revs->max_count = 1; |
| 187 | else if (!strcmp(argv[1], "--ours")) |
| 188 | revs->max_count = 2; |
| 189 | else if (!strcmp(argv[1], "--theirs")) |
| 190 | revs->max_count = 3; |
| 191 | else if (!strcmp(argv[1], "-n") || |
Alex Riesen | 41bbf9d | 2007-03-14 01:17:04 +0100 | [diff] [blame] | 192 | !strcmp(argv[1], "--no-index")) { |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 193 | revs->max_count = -2; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 194 | DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS); |
| 195 | DIFF_OPT_SET(&revs->diffopt, NO_INDEX); |
Alex Riesen | 41bbf9d | 2007-03-14 01:17:04 +0100 | [diff] [blame] | 196 | } |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 197 | else if (!strcmp(argv[1], "-q")) |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 198 | *options |= DIFF_SILENT_ON_REMOVED; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 199 | else |
| 200 | return error("invalid option: %s", argv[1]); |
| 201 | argv++; argc--; |
| 202 | } |
| 203 | |
| 204 | if (revs->max_count == -1 && revs->diffopt.nr_paths == 2) { |
| 205 | /* |
| 206 | * If two files are specified, and at least one is untracked, |
| 207 | * default to no-index. |
| 208 | */ |
| 209 | read_cache(); |
| 210 | if (!is_in_index(revs->diffopt.paths[0]) || |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 211 | !is_in_index(revs->diffopt.paths[1])) { |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 212 | revs->max_count = -2; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 213 | DIFF_OPT_SET(&revs->diffopt, NO_INDEX); |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 214 | } |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Make sure there are NO revision (i.e. pending object) parameter, |
| 219 | * rev.max_count is reasonable (0 <= n <= 3), |
| 220 | * there is no other revision filtering parameters. |
| 221 | */ |
| 222 | if (revs->pending.nr || revs->max_count > 3 || |
| 223 | revs->min_age != -1 || revs->max_age != -1) |
| 224 | return error("no revision allowed with diff-files"); |
| 225 | |
| 226 | if (revs->max_count == -1 && |
| 227 | (revs->diffopt.output_format & DIFF_FORMAT_PATCH)) |
| 228 | revs->combine_merges = revs->dense_combined_merges = 1; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 233 | static int is_outside_repo(const char *path, int nongit, const char *prefix) |
| 234 | { |
| 235 | int i; |
Steffen Prohaska | ecf4831 | 2007-11-25 23:29:03 +0100 | [diff] [blame] | 236 | if (nongit || !strcmp(path, "-") || is_absolute_path(path)) |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 237 | return 1; |
| 238 | if (prefixcmp(path, "../")) |
| 239 | return 0; |
| 240 | if (!prefix) |
| 241 | return 1; |
| 242 | for (i = strlen(prefix); !prefixcmp(path, "../"); ) { |
| 243 | while (i > 0 && prefix[i - 1] != '/') |
| 244 | i--; |
| 245 | if (--i < 0) |
| 246 | return 1; |
| 247 | path += 3; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int setup_diff_no_index(struct rev_info *revs, |
| 253 | int argc, const char ** argv, int nongit, const char *prefix) |
| 254 | { |
| 255 | int i; |
| 256 | for (i = 1; i < argc; i++) |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 257 | if (argv[i][0] != '-' || argv[i][1] == '\0') |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 258 | break; |
| 259 | else if (!strcmp(argv[i], "--")) { |
| 260 | i++; |
| 261 | break; |
| 262 | } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) { |
| 263 | i = argc - 3; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 264 | DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS); |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 265 | break; |
| 266 | } |
Matthieu Moy | 59b0c24 | 2008-04-24 20:06:36 +0200 | [diff] [blame] | 267 | if (nongit && argc != i + 2) |
| 268 | die("git diff [--no-index] takes two paths"); |
| 269 | |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 270 | if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) && |
| 271 | !is_outside_repo(argv[i], nongit, prefix))) |
| 272 | return -1; |
| 273 | |
| 274 | diff_setup(&revs->diffopt); |
| 275 | for (i = 1; i < argc - 2; ) |
| 276 | if (!strcmp(argv[i], "--no-index")) |
| 277 | i++; |
| 278 | else { |
| 279 | int j = diff_opt_parse(&revs->diffopt, |
| 280 | argv + i, argc - i); |
| 281 | if (!j) |
| 282 | die("invalid diff option/value: %s", argv[i]); |
| 283 | i += j; |
| 284 | } |
Junio C Hamano | ae792aa | 2007-03-03 23:45:14 -0800 | [diff] [blame] | 285 | |
| 286 | if (prefix) { |
| 287 | int len = strlen(prefix); |
| 288 | |
| 289 | revs->diffopt.paths = xcalloc(2, sizeof(char*)); |
| 290 | for (i = 0; i < 2; i++) { |
Junio C Hamano | 3afaa72 | 2007-03-04 00:17:27 -0800 | [diff] [blame] | 291 | const char *p = argv[argc - 2 + i]; |
| 292 | /* |
| 293 | * stdin should be spelled as '-'; if you have |
| 294 | * path that is '-', spell it as ./-. |
| 295 | */ |
| 296 | p = (strcmp(p, "-") |
| 297 | ? xstrdup(prefix_filename(prefix, len, p)) |
| 298 | : p); |
| 299 | revs->diffopt.paths[i] = p; |
Junio C Hamano | ae792aa | 2007-03-03 23:45:14 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | else |
| 303 | revs->diffopt.paths = argv + argc - 2; |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 304 | revs->diffopt.nr_paths = 2; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 305 | DIFF_OPT_SET(&revs->diffopt, NO_INDEX); |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 306 | revs->max_count = -2; |
Junio C Hamano | b78281f | 2007-09-14 12:12:32 -0700 | [diff] [blame] | 307 | if (diff_setup_done(&revs->diffopt) < 0) |
| 308 | die("diff_setup_done failed"); |
Johannes Schindelin | fcfa33e | 2007-02-25 23:35:27 +0100 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 312 | int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv) |
| 313 | { |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 314 | unsigned int options; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 315 | |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 316 | if (handle_diff_files_args(revs, argc, argv, &options)) |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 317 | return -1; |
| 318 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 319 | if (DIFF_OPT_TST(&revs->diffopt, NO_INDEX)) { |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 320 | if (revs->diffopt.nr_paths != 2) |
| 321 | return error("need two files/directories with --no-index"); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 322 | if (queue_diff(&revs->diffopt, revs->diffopt.paths[0], |
| 323 | revs->diffopt.paths[1])) |
| 324 | return -1; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 325 | diffcore_std(&revs->diffopt); |
| 326 | diff_flush(&revs->diffopt); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 327 | /* |
| 328 | * The return code for --no-index imitates diff(1): |
| 329 | * 0 = no changes, 1 = changes, else error |
| 330 | */ |
| 331 | return revs->diffopt.found_changes; |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Junio C Hamano | efdfd6c | 2007-02-24 02:20:13 -0800 | [diff] [blame] | 334 | if (read_cache() < 0) { |
| 335 | perror("read_cache"); |
| 336 | return -1; |
| 337 | } |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 338 | return run_diff_files(revs, options); |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 339 | } |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 340 | /* |
| 341 | * See if work tree has an entity that can be staged. Return 0 if so, |
| 342 | * return 1 if not and return -1 if error. |
| 343 | */ |
| 344 | static int check_work_tree_entity(const struct cache_entry *ce, struct stat *st, char *symcache) |
| 345 | { |
| 346 | if (lstat(ce->name, st) < 0) { |
| 347 | if (errno != ENOENT && errno != ENOTDIR) |
| 348 | return -1; |
| 349 | return 1; |
| 350 | } |
| 351 | if (has_symlink_leading_path(ce->name, symcache)) |
| 352 | return 1; |
| 353 | if (S_ISDIR(st->st_mode)) { |
| 354 | unsigned char sub[20]; |
| 355 | if (resolve_gitlink_ref(ce->name, "HEAD", sub)) |
| 356 | return 1; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 360 | |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 361 | int run_diff_files(struct rev_info *revs, unsigned int option) |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 362 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 363 | int entries, i; |
| 364 | int diff_unmerged_stage = revs->max_count; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 365 | int silent_on_removed = option & DIFF_SILENT_ON_REMOVED; |
Junio C Hamano | fb63d7f | 2007-11-09 18:22:52 -0800 | [diff] [blame] | 366 | unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED) |
| 367 | ? CE_MATCH_RACY_IS_DIRTY : 0); |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 368 | char symcache[PATH_MAX]; |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 369 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 370 | if (diff_unmerged_stage < 0) |
| 371 | diff_unmerged_stage = 2; |
Junio C Hamano | b4e1e4a | 2007-02-09 18:51:40 -0800 | [diff] [blame] | 372 | entries = active_nr; |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 373 | symcache[0] = '\0'; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 374 | for (i = 0; i < entries; i++) { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 375 | struct stat st; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 376 | unsigned int oldmode, newmode; |
| 377 | struct cache_entry *ce = active_cache[i]; |
| 378 | int changed; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 379 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 380 | if (DIFF_OPT_TST(&revs->diffopt, QUIET) && |
| 381 | DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES)) |
Junio C Hamano | 822cac0 | 2007-03-14 11:12:51 -0700 | [diff] [blame] | 382 | break; |
| 383 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 384 | if (!ce_path_match(ce, revs->prune_data)) |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 385 | continue; |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 386 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 387 | if (ce_stage(ce)) { |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 388 | struct combine_diff_path *dpath; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 389 | int num_compare_stages = 0; |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 390 | size_t path_len; |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 391 | |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 392 | path_len = ce_namelen(ce); |
| 393 | |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 394 | dpath = xmalloc(combine_diff_path_size(5, path_len)); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 395 | dpath->path = (char *) &(dpath->parent[5]); |
| 396 | |
| 397 | dpath->next = NULL; |
| 398 | dpath->len = path_len; |
| 399 | memcpy(dpath->path, ce->name, path_len); |
| 400 | dpath->path[path_len] = '\0'; |
Junio C Hamano | a8e0d16 | 2006-08-23 13:57:23 -0700 | [diff] [blame] | 401 | hashclr(dpath->sha1); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 402 | memset(&(dpath->parent[0]), 0, |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 403 | sizeof(struct combine_diff_parent)*5); |
| 404 | |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 405 | changed = check_work_tree_entity(ce, &st, symcache); |
| 406 | if (!changed) |
| 407 | dpath->mode = ce_mode_from_stat(ce, st.st_mode); |
| 408 | else { |
| 409 | if (changed < 0) { |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 410 | perror(ce->name); |
| 411 | continue; |
| 412 | } |
| 413 | if (silent_on_removed) |
| 414 | continue; |
| 415 | } |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 416 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 417 | while (i < entries) { |
| 418 | struct cache_entry *nce = active_cache[i]; |
| 419 | int stage; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 420 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 421 | if (strcmp(ce->name, nce->name)) |
| 422 | break; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 423 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 424 | /* Stage #2 (ours) is the first parent, |
| 425 | * stage #3 (theirs) is the second. |
| 426 | */ |
| 427 | stage = ce_stage(nce); |
| 428 | if (2 <= stage) { |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 429 | int mode = nce->ce_mode; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 430 | num_compare_stages++; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 431 | hashcpy(dpath->parent[stage-2].sha1, nce->sha1); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 432 | dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 433 | dpath->parent[stage-2].status = |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 434 | DIFF_STATUS_MODIFIED; |
| 435 | } |
Junio C Hamano | cebff98 | 2006-03-25 23:12:17 -0800 | [diff] [blame] | 436 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 437 | /* diff against the proper unmerged stage */ |
| 438 | if (stage == diff_unmerged_stage) |
| 439 | ce = nce; |
| 440 | i++; |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 441 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 442 | /* |
| 443 | * Compensate for loop update |
| 444 | */ |
| 445 | i--; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 446 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 447 | if (revs->combine_merges && num_compare_stages == 2) { |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 448 | show_combined_diff(dpath, 2, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 449 | revs->dense_combined_merges, |
| 450 | revs); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 451 | free(dpath); |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 452 | continue; |
| 453 | } |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 454 | free(dpath); |
| 455 | dpath = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * Show the diff for the 'ce' if we found the one |
| 459 | * from the desired stage. |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 460 | */ |
Junio C Hamano | e9c8409 | 2007-01-05 01:25:18 -0800 | [diff] [blame] | 461 | diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 462 | if (ce_stage(ce) != diff_unmerged_stage) |
| 463 | continue; |
| 464 | } |
| 465 | |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 466 | if (ce_uptodate(ce)) |
| 467 | continue; |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 468 | |
| 469 | changed = check_work_tree_entity(ce, &st, symcache); |
| 470 | if (changed) { |
| 471 | if (changed < 0) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 472 | perror(ce->name); |
| 473 | continue; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 474 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 475 | if (silent_on_removed) |
| 476 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 477 | diff_addremove(&revs->diffopt, '-', ce->ce_mode, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 478 | ce->sha1, ce->name, NULL); |
| 479 | continue; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 480 | } |
Junio C Hamano | fb63d7f | 2007-11-09 18:22:52 -0800 | [diff] [blame] | 481 | changed = ce_match_stat(ce, &st, ce_option); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 482 | if (!changed && !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 483 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 484 | oldmode = ce->ce_mode; |
| 485 | newmode = ce_mode_from_stat(ce, st.st_mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 486 | diff_change(&revs->diffopt, oldmode, newmode, |
| 487 | ce->sha1, (changed ? null_sha1 : ce->sha1), |
| 488 | ce->name, NULL); |
| 489 | |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 490 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 491 | diffcore_std(&revs->diffopt); |
| 492 | diff_flush(&revs->diffopt); |
| 493 | return 0; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 496 | /* |
| 497 | * diff-index |
| 498 | */ |
| 499 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 500 | struct oneway_unpack_data { |
| 501 | struct rev_info *revs; |
| 502 | char symcache[PATH_MAX]; |
| 503 | }; |
| 504 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 505 | /* A file entry went away or appeared */ |
| 506 | static void diff_index_show_file(struct rev_info *revs, |
| 507 | const char *prefix, |
| 508 | struct cache_entry *ce, |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 509 | const unsigned char *sha1, unsigned int mode) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 510 | { |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 511 | diff_addremove(&revs->diffopt, prefix[0], mode, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 512 | sha1, ce->name, NULL); |
| 513 | } |
| 514 | |
| 515 | static int get_stat_data(struct cache_entry *ce, |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 516 | const unsigned char **sha1p, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 517 | unsigned int *modep, |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 518 | int cached, int match_missing, |
| 519 | struct oneway_unpack_data *cbdata) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 520 | { |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 521 | const unsigned char *sha1 = ce->sha1; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 522 | unsigned int mode = ce->ce_mode; |
| 523 | |
| 524 | if (!cached) { |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 525 | int changed; |
| 526 | struct stat st; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 527 | changed = check_work_tree_entity(ce, &st, cbdata->symcache); |
| 528 | if (changed < 0) |
| 529 | return -1; |
| 530 | else if (changed) { |
| 531 | if (match_missing) { |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 532 | *sha1p = sha1; |
| 533 | *modep = mode; |
| 534 | return 0; |
| 535 | } |
| 536 | return -1; |
| 537 | } |
| 538 | changed = ce_match_stat(ce, &st, 0); |
| 539 | if (changed) { |
Junio C Hamano | 185c975 | 2007-02-16 22:43:48 -0800 | [diff] [blame] | 540 | mode = ce_mode_from_stat(ce, st.st_mode); |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 541 | sha1 = null_sha1; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | *sha1p = sha1; |
| 546 | *modep = mode; |
| 547 | return 0; |
| 548 | } |
| 549 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 550 | static void show_new_file(struct oneway_unpack_data *cbdata, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 551 | struct cache_entry *new, |
| 552 | int cached, int match_missing) |
| 553 | { |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 554 | const unsigned char *sha1; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 555 | unsigned int mode; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 556 | struct rev_info *revs = cbdata->revs; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 557 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 558 | /* |
| 559 | * New file in the index: it might actually be different in |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 560 | * the working copy. |
| 561 | */ |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 562 | if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 563 | return; |
| 564 | |
| 565 | diff_index_show_file(revs, "+", new, sha1, mode); |
| 566 | } |
| 567 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 568 | static int show_modified(struct oneway_unpack_data *cbdata, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 569 | struct cache_entry *old, |
| 570 | struct cache_entry *new, |
| 571 | int report_missing, |
| 572 | int cached, int match_missing) |
| 573 | { |
| 574 | unsigned int mode, oldmode; |
Junio C Hamano | c8c16f2 | 2008-03-02 00:57:26 -0800 | [diff] [blame] | 575 | const unsigned char *sha1; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 576 | struct rev_info *revs = cbdata->revs; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 577 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 578 | if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0) { |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 579 | if (report_missing) |
| 580 | diff_index_show_file(revs, "-", old, |
| 581 | old->sha1, old->ce_mode); |
| 582 | return -1; |
| 583 | } |
| 584 | |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 585 | if (revs->combine_merges && !cached && |
| 586 | (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) { |
| 587 | struct combine_diff_path *p; |
| 588 | int pathlen = ce_namelen(new); |
| 589 | |
| 590 | p = xmalloc(combine_diff_path_size(2, pathlen)); |
| 591 | p->path = (char *) &p->parent[2]; |
| 592 | p->next = NULL; |
| 593 | p->len = pathlen; |
| 594 | memcpy(p->path, new->name, pathlen); |
| 595 | p->path[pathlen] = 0; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 596 | p->mode = mode; |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 597 | hashclr(p->sha1); |
| 598 | memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent)); |
| 599 | p->parent[0].status = DIFF_STATUS_MODIFIED; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 600 | p->parent[0].mode = new->ce_mode; |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 601 | hashcpy(p->parent[0].sha1, new->sha1); |
| 602 | p->parent[1].status = DIFF_STATUS_MODIFIED; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 603 | p->parent[1].mode = old->ce_mode; |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 604 | hashcpy(p->parent[1].sha1, old->sha1); |
| 605 | show_combined_diff(p, 2, revs->dense_combined_merges, revs); |
| 606 | free(p); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 610 | oldmode = old->ce_mode; |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 611 | if (mode == oldmode && !hashcmp(sha1, old->sha1) && |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 612 | !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER)) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 613 | return 0; |
| 614 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 615 | diff_change(&revs->diffopt, oldmode, mode, |
| 616 | old->sha1, sha1, old->name, NULL); |
| 617 | return 0; |
| 618 | } |
| 619 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 620 | /* |
| 621 | * This turns all merge entries into "stage 3". That guarantees that |
| 622 | * when we read in the new tree (into "stage 1"), we won't lose sight |
| 623 | * of the fact that we had unmerged entries. |
| 624 | */ |
| 625 | static void mark_merge_entries(void) |
| 626 | { |
| 627 | int i; |
| 628 | for (i = 0; i < active_nr; i++) { |
| 629 | struct cache_entry *ce = active_cache[i]; |
| 630 | if (!ce_stage(ce)) |
| 631 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 632 | ce->ce_flags |= CE_STAGEMASK; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 636 | /* |
| 637 | * This gets a mix of an existing index and a tree, one pathname entry |
| 638 | * at a time. The index entry may be a single stage-0 one, but it could |
| 639 | * also be multiple unmerged entries (in which case idx_pos/idx_nr will |
| 640 | * give you the position and number of entries in the index). |
| 641 | */ |
| 642 | static void do_oneway_diff(struct unpack_trees_options *o, |
| 643 | struct cache_entry *idx, |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 644 | struct cache_entry *tree) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 645 | { |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 646 | struct oneway_unpack_data *cbdata = o->unpack_data; |
| 647 | struct rev_info *revs = cbdata->revs; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 648 | int match_missing, cached; |
Junio C Hamano | 5c21ac0 | 2006-04-22 03:58:04 -0700 | [diff] [blame] | 649 | |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 650 | /* |
Junio C Hamano | 5c21ac0 | 2006-04-22 03:58:04 -0700 | [diff] [blame] | 651 | * Backward compatibility wart - "diff-index -m" does |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 652 | * not mean "do not ignore merges", but "match_missing". |
| 653 | * |
| 654 | * But with the revision flag parsing, that's found in |
| 655 | * "!revs->ignore_merges". |
Junio C Hamano | 5c21ac0 | 2006-04-22 03:58:04 -0700 | [diff] [blame] | 656 | */ |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 657 | cached = o->index_only; |
| 658 | match_missing = !revs->ignore_merges; |
| 659 | |
| 660 | if (cached && idx && ce_stage(idx)) { |
| 661 | if (tree) |
| 662 | diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode, idx->sha1); |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Something added to the tree? |
| 668 | */ |
| 669 | if (!tree) { |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 670 | show_new_file(cbdata, idx, cached, match_missing); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Something removed from the tree? |
| 676 | */ |
| 677 | if (!idx) { |
| 678 | diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | /* Show difference between old and new */ |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 683 | show_modified(cbdata, tree, idx, 1, cached, match_missing); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Linus Torvalds | 20a16eb | 2008-03-10 23:51:13 -0700 | [diff] [blame] | 686 | static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_options *o) |
| 687 | { |
| 688 | int len = ce_namelen(ce); |
| 689 | const struct index_state *index = o->src_index; |
| 690 | |
| 691 | while (o->pos < index->cache_nr) { |
| 692 | struct cache_entry *next = index->cache[o->pos]; |
| 693 | if (len != ce_namelen(next)) |
| 694 | break; |
| 695 | if (memcmp(ce->name, next->name, len)) |
| 696 | break; |
| 697 | o->pos++; |
| 698 | } |
| 699 | } |
| 700 | |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 701 | /* |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 702 | * The unpack_trees() interface is designed for merging, so |
| 703 | * the different source entries are designed primarily for |
| 704 | * the source trees, with the old index being really mainly |
| 705 | * used for being replaced by the result. |
| 706 | * |
| 707 | * For diffing, the index is more important, and we only have a |
| 708 | * single tree. |
| 709 | * |
| 710 | * We're supposed to return how many index entries we want to skip. |
| 711 | * |
| 712 | * This wrapper makes it all more readable, and takes care of all |
| 713 | * the fairly complex unpack_trees() semantic requirements, including |
| 714 | * the skipping, the path matching, the type conflict cases etc. |
| 715 | */ |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 716 | static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o) |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 717 | { |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 718 | struct cache_entry *idx = src[0]; |
| 719 | struct cache_entry *tree = src[1]; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 720 | struct oneway_unpack_data *cbdata = o->unpack_data; |
| 721 | struct rev_info *revs = cbdata->revs; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 722 | |
Linus Torvalds | 20a16eb | 2008-03-10 23:51:13 -0700 | [diff] [blame] | 723 | if (idx && ce_stage(idx)) |
| 724 | skip_same_name(idx, o); |
| 725 | |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 726 | /* |
| 727 | * Unpack-trees generates a DF/conflict entry if |
| 728 | * there was a directory in the index and a tree |
| 729 | * in the tree. From a diff standpoint, that's a |
| 730 | * delete of the tree and a create of the file. |
| 731 | */ |
| 732 | if (tree == o->df_conflict_entry) |
| 733 | tree = NULL; |
| 734 | |
| 735 | if (ce_path_match(idx ? idx : tree, revs->prune_data)) |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 736 | do_oneway_diff(o, idx, tree); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 737 | |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 738 | return 0; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | int run_diff_index(struct rev_info *revs, int cached) |
| 742 | { |
| 743 | struct object *ent; |
| 744 | struct tree *tree; |
| 745 | const char *tree_name; |
| 746 | struct unpack_trees_options opts; |
| 747 | struct tree_desc t; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 748 | struct oneway_unpack_data unpack_cb; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 749 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 750 | mark_merge_entries(); |
| 751 | |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 752 | ent = revs->pending.objects[0].item; |
| 753 | tree_name = revs->pending.objects[0].name; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 754 | tree = parse_tree_indirect(ent->sha1); |
| 755 | if (!tree) |
| 756 | return error("bad tree object %s", tree_name); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 757 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 758 | unpack_cb.revs = revs; |
| 759 | unpack_cb.symcache[0] = '\0'; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 760 | memset(&opts, 0, sizeof(opts)); |
| 761 | opts.head_idx = 1; |
| 762 | opts.index_only = cached; |
| 763 | opts.merge = 1; |
| 764 | opts.fn = oneway_diff; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 765 | opts.unpack_data = &unpack_cb; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 766 | opts.src_index = &the_index; |
| 767 | opts.dst_index = NULL; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 768 | |
| 769 | init_tree_desc(&t, tree->buffer, tree->size); |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 770 | if (unpack_trees(1, &t, &opts)) |
| 771 | exit(128); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 772 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 773 | diffcore_std(&revs->diffopt); |
| 774 | diff_flush(&revs->diffopt); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 775 | return 0; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 776 | } |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 777 | |
| 778 | int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt) |
| 779 | { |
| 780 | struct tree *tree; |
| 781 | struct rev_info revs; |
| 782 | int i; |
| 783 | struct cache_entry **dst; |
| 784 | struct cache_entry *last = NULL; |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 785 | struct unpack_trees_options opts; |
| 786 | struct tree_desc t; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 787 | struct oneway_unpack_data unpack_cb; |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * This is used by git-blame to run diff-cache internally; |
| 791 | * it potentially needs to repeatedly run this, so we will |
| 792 | * start by removing the higher order entries the last round |
| 793 | * left behind. |
| 794 | */ |
| 795 | dst = active_cache; |
| 796 | for (i = 0; i < active_nr; i++) { |
| 797 | struct cache_entry *ce = active_cache[i]; |
| 798 | if (ce_stage(ce)) { |
| 799 | if (last && !strcmp(ce->name, last->name)) |
| 800 | continue; |
| 801 | cache_tree_invalidate_path(active_cache_tree, |
| 802 | ce->name); |
| 803 | last = ce; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 804 | ce->ce_flags |= CE_REMOVE; |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 805 | } |
| 806 | *dst++ = ce; |
| 807 | } |
| 808 | active_nr = dst - active_cache; |
| 809 | |
| 810 | init_revisions(&revs, NULL); |
| 811 | revs.prune_data = opt->paths; |
| 812 | tree = parse_tree_indirect(tree_sha1); |
| 813 | if (!tree) |
| 814 | die("bad tree object %s", sha1_to_hex(tree_sha1)); |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 815 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 816 | unpack_cb.revs = &revs; |
| 817 | unpack_cb.symcache[0] = '\0'; |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 818 | memset(&opts, 0, sizeof(opts)); |
| 819 | opts.head_idx = 1; |
| 820 | opts.index_only = 1; |
| 821 | opts.merge = 1; |
| 822 | opts.fn = oneway_diff; |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 823 | opts.unpack_data = &unpack_cb; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 824 | opts.src_index = &the_index; |
| 825 | opts.dst_index = &the_index; |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 826 | |
| 827 | init_tree_desc(&t, tree->buffer, tree->size); |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 828 | if (unpack_trees(1, &t, &opts)) |
| 829 | exit(128); |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 830 | return 0; |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 831 | } |