Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "diff --no-index" support |
| 3 | * Copyright (c) 2007 by Johannes Schindelin |
| 4 | * Copyright (c) 2008 by Junio C Hamano |
| 5 | */ |
| 6 | |
| 7 | #include "cache.h" |
| 8 | #include "color.h" |
| 9 | #include "commit.h" |
| 10 | #include "blob.h" |
| 11 | #include "tag.h" |
| 12 | #include "diff.h" |
| 13 | #include "diffcore.h" |
| 14 | #include "revision.h" |
| 15 | #include "log-tree.h" |
| 16 | #include "builtin.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 17 | #include "string-list.h" |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 18 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 19 | static int read_directory(const char *path, struct string_list *list) |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 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)) |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 29 | string_list_insert(list, e->d_name); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 30 | |
| 31 | closedir(dir); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 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; |
Johannes Schindelin | 36adb4a | 2009-03-07 16:51:33 +0100 | [diff] [blame] | 41 | #ifdef _WIN32 |
| 42 | else if (!strcasecmp(path, "nul")) |
| 43 | *mode = 0; |
| 44 | #endif |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 45 | else if (!strcmp(path, "-")) |
| 46 | *mode = create_ce_mode(0666); |
Johannes Schindelin | 418566b | 2009-01-29 17:30:51 +0100 | [diff] [blame] | 47 | else if (lstat(path, &st)) |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 48 | return error("Could not access '%s'", path); |
| 49 | else |
| 50 | *mode = st.st_mode; |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int queue_diff(struct diff_options *o, |
| 55 | const char *name1, const char *name2) |
| 56 | { |
| 57 | int mode1 = 0, mode2 = 0; |
| 58 | |
| 59 | if (get_mode(name1, &mode1) || get_mode(name2, &mode2)) |
| 60 | return -1; |
| 61 | |
| 62 | if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) |
| 63 | return error("file/directory conflict: %s, %s", name1, name2); |
| 64 | |
| 65 | if (S_ISDIR(mode1) || S_ISDIR(mode2)) { |
| 66 | char buffer1[PATH_MAX], buffer2[PATH_MAX]; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 67 | struct string_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1}; |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 68 | int len1 = 0, len2 = 0, i1, i2, ret = 0; |
| 69 | |
| 70 | if (name1 && read_directory(name1, &p1)) |
| 71 | return -1; |
| 72 | if (name2 && read_directory(name2, &p2)) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 73 | string_list_clear(&p1, 0); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (name1) { |
| 78 | len1 = strlen(name1); |
| 79 | if (len1 > 0 && name1[len1 - 1] == '/') |
| 80 | len1--; |
| 81 | memcpy(buffer1, name1, len1); |
| 82 | buffer1[len1++] = '/'; |
| 83 | } |
| 84 | |
| 85 | if (name2) { |
| 86 | len2 = strlen(name2); |
| 87 | if (len2 > 0 && name2[len2 - 1] == '/') |
| 88 | len2--; |
| 89 | memcpy(buffer2, name2, len2); |
| 90 | buffer2[len2++] = '/'; |
| 91 | } |
| 92 | |
| 93 | for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) { |
| 94 | const char *n1, *n2; |
| 95 | int comp; |
| 96 | |
| 97 | if (i1 == p1.nr) |
| 98 | comp = 1; |
| 99 | else if (i2 == p2.nr) |
| 100 | comp = -1; |
| 101 | else |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 102 | comp = strcmp(p1.items[i1].string, |
| 103 | p2.items[i2].string); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 104 | |
| 105 | if (comp > 0) |
| 106 | n1 = NULL; |
| 107 | else { |
| 108 | n1 = buffer1; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 109 | strncpy(buffer1 + len1, p1.items[i1++].string, |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 110 | PATH_MAX - len1); |
| 111 | } |
| 112 | |
| 113 | if (comp < 0) |
| 114 | n2 = NULL; |
| 115 | else { |
| 116 | n2 = buffer2; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 117 | strncpy(buffer2 + len2, p2.items[i2++].string, |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 118 | PATH_MAX - len2); |
| 119 | } |
| 120 | |
| 121 | ret = queue_diff(o, n1, n2); |
| 122 | } |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 123 | string_list_clear(&p1, 0); |
| 124 | string_list_clear(&p2, 0); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 125 | |
| 126 | return ret; |
| 127 | } else { |
| 128 | struct diff_filespec *d1, *d2; |
| 129 | |
| 130 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { |
| 131 | unsigned tmp; |
| 132 | const char *tmp_c; |
| 133 | tmp = mode1; mode1 = mode2; mode2 = tmp; |
| 134 | tmp_c = name1; name1 = name2; name2 = tmp_c; |
| 135 | } |
| 136 | |
| 137 | if (!name1) |
| 138 | name1 = "/dev/null"; |
| 139 | if (!name2) |
| 140 | name2 = "/dev/null"; |
| 141 | d1 = alloc_filespec(name1); |
| 142 | d2 = alloc_filespec(name2); |
| 143 | fill_filespec(d1, null_sha1, mode1); |
| 144 | fill_filespec(d2, null_sha1, mode2); |
| 145 | |
| 146 | diff_queue(&diff_queued_diff, d1, d2); |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
Junio C Hamano | 0403660 | 2008-05-26 21:54:23 -0700 | [diff] [blame] | 151 | static int path_outside_repo(const char *path) |
| 152 | { |
Junio C Hamano | 0403660 | 2008-05-26 21:54:23 -0700 | [diff] [blame] | 153 | const char *work_tree; |
| 154 | size_t len; |
| 155 | |
| 156 | if (!is_absolute_path(path)) |
| 157 | return 0; |
| 158 | work_tree = get_git_work_tree(); |
Clemens Buchacher | 4e1f879 | 2010-05-22 14:21:27 +0200 | [diff] [blame] | 159 | if (!work_tree) |
| 160 | return 1; |
Junio C Hamano | 0403660 | 2008-05-26 21:54:23 -0700 | [diff] [blame] | 161 | len = strlen(work_tree); |
| 162 | if (strncmp(path, work_tree, len) || |
| 163 | (path[len] != '\0' && path[len] != '/')) |
| 164 | return 1; |
| 165 | return 0; |
| 166 | } |
| 167 | |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 168 | void diff_no_index(struct rev_info *revs, |
| 169 | int argc, const char **argv, |
| 170 | int nongit, const char *prefix) |
| 171 | { |
| 172 | int i; |
| 173 | int no_index = 0; |
| 174 | unsigned options = 0; |
| 175 | |
| 176 | /* Were we asked to do --no-index explicitly? */ |
| 177 | for (i = 1; i < argc; i++) { |
Thomas Rast | e423ffd | 2009-01-06 19:53:32 +0100 | [diff] [blame] | 178 | if (!strcmp(argv[i], "--")) { |
| 179 | i++; |
| 180 | break; |
| 181 | } |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 182 | if (!strcmp(argv[i], "--no-index")) |
| 183 | no_index = 1; |
| 184 | if (argv[i][0] != '-') |
| 185 | break; |
| 186 | } |
| 187 | |
Junio C Hamano | 0403660 | 2008-05-26 21:54:23 -0700 | [diff] [blame] | 188 | if (!no_index && !nongit) { |
| 189 | /* |
| 190 | * Inside a git repository, without --no-index. Only |
| 191 | * when a path outside the repository is given, |
| 192 | * e.g. "git diff /var/tmp/[12]", or "git diff |
| 193 | * Makefile /var/tmp/Makefile", allow it to be used as |
| 194 | * a colourful "diff" replacement. |
| 195 | */ |
| 196 | if ((argc != i + 2) || |
| 197 | (!path_outside_repo(argv[i]) && |
| 198 | !path_outside_repo(argv[i+1]))) |
| 199 | return; |
| 200 | } |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 201 | if (argc != i + 2) |
Jonathan Nieder | d74bb30 | 2009-11-09 09:05:04 -0600 | [diff] [blame] | 202 | usagef("git diff %s <path> <path>", |
| 203 | no_index ? "--no-index" : "[--no-index]"); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 204 | |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 205 | diff_setup(&revs->diffopt); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 206 | for (i = 1; i < argc - 2; ) { |
| 207 | int j; |
| 208 | if (!strcmp(argv[i], "--no-index")) |
| 209 | i++; |
Thomas Rast | a324fc4 | 2009-01-07 12:15:30 +0100 | [diff] [blame] | 210 | else if (!strcmp(argv[i], "-q")) { |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 211 | options |= DIFF_SILENT_ON_REMOVED; |
Thomas Rast | a324fc4 | 2009-01-07 12:15:30 +0100 | [diff] [blame] | 212 | i++; |
| 213 | } |
Thomas Rast | e423ffd | 2009-01-06 19:53:32 +0100 | [diff] [blame] | 214 | else if (!strcmp(argv[i], "--")) |
| 215 | i++; |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 216 | else { |
| 217 | j = diff_opt_parse(&revs->diffopt, argv + i, argc - i); |
| 218 | if (!j) |
| 219 | die("invalid diff option/value: %s", argv[i]); |
| 220 | i += j; |
| 221 | } |
| 222 | } |
| 223 | |
Thomas Rast | c6dbca0 | 2009-01-07 00:56:03 +0100 | [diff] [blame] | 224 | /* |
| 225 | * If the user asked for our exit code then don't start a |
| 226 | * pager or we would end up reporting its exit code instead. |
| 227 | */ |
| 228 | if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS)) |
| 229 | setup_pager(); |
| 230 | |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 231 | if (prefix) { |
| 232 | int len = strlen(prefix); |
| 233 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 234 | revs->diffopt.paths = xcalloc(2, sizeof(char *)); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 235 | for (i = 0; i < 2; i++) { |
| 236 | const char *p = argv[argc - 2 + i]; |
| 237 | /* |
| 238 | * stdin should be spelled as '-'; if you have |
| 239 | * path that is '-', spell it as ./-. |
| 240 | */ |
| 241 | p = (strcmp(p, "-") |
| 242 | ? xstrdup(prefix_filename(prefix, len, p)) |
| 243 | : p); |
| 244 | revs->diffopt.paths[i] = p; |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | revs->diffopt.paths = argv + argc - 2; |
| 249 | revs->diffopt.nr_paths = 2; |
Michael Spang | d61027b | 2009-02-18 01:48:06 -0500 | [diff] [blame] | 250 | revs->diffopt.skip_stat_unmatch = 1; |
Johannes Sixt | 5d83f9c | 2009-03-25 18:19:46 +0100 | [diff] [blame] | 251 | if (!revs->diffopt.output_format) |
| 252 | revs->diffopt.output_format = DIFF_FORMAT_PATCH; |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 253 | |
| 254 | DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS); |
| 255 | DIFF_OPT_SET(&revs->diffopt, NO_INDEX); |
| 256 | |
| 257 | revs->max_count = -2; |
| 258 | if (diff_setup_done(&revs->diffopt) < 0) |
| 259 | die("diff_setup_done failed"); |
| 260 | |
| 261 | if (queue_diff(&revs->diffopt, revs->diffopt.paths[0], |
| 262 | revs->diffopt.paths[1])) |
| 263 | exit(1); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 264 | diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/"); |
Junio C Hamano | 0569e9b | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 265 | diffcore_std(&revs->diffopt); |
| 266 | diff_flush(&revs->diffopt); |
| 267 | |
| 268 | /* |
| 269 | * The return code for --no-index imitates diff(1): |
| 270 | * 0 = no changes, 1 = changes, else error |
| 271 | */ |
| 272 | exit(revs->diffopt.found_changes); |
| 273 | } |