Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/diff-no-index.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / diff-no-index.c` 446 lines ยท 11439 bytes Raw Blame History 1 /* 2 * "diff --no-index" support 3 * Copyright (c) 2007 by Johannes Schindelin 4 * Copyright (c) 2008 by Junio C Hamano 5 */ 6 7 #define DISABLE_SIGN_COMPARE_WARNINGS 8 9 #include "git-compat-util.h" 10 #include "abspath.h" 11 #include "color.h" 12 #include "commit.h" 13 #include "diff.h" 14 #include "diffcore.h" 15 #include "gettext.h" 16 #include "revision.h" 17 #include "parse-options.h" 18 #include "pathspec.h" 19 #include "string-list.h" 20 #include "dir.h" 21 22 static int read_directory_contents(const char *path, struct string_list *list, 23 const struct pathspec *pathspec, 24 struct strbuf *match) 25 { 26 int len = match->len; 27 DIR *dir; 28 struct dirent *e; 29 30 if (!(dir = opendir(path))) 31 return error("Could not open directory %s", path); 32 33 while ((e = readdir_skip_dot_and_dotdot(dir))) { 34 if (pathspec) { 35 int is_dir = 0; 36 37 strbuf_setlen(match, len); 38 strbuf_addstr(match, e->d_name); 39 if (NOT_CONSTANT(DTYPE(e)) != DT_UNKNOWN) { 40 is_dir = (DTYPE(e) == DT_DIR); 41 } else { 42 struct strbuf pathbuf = STRBUF_INIT; 43 44 strbuf_addstr(&pathbuf, path); 45 strbuf_complete(&pathbuf, '/'); 46 is_dir = get_dtype(e, &pathbuf, 0) == DT_DIR; 47 strbuf_release(&pathbuf); 48 } 49 50 if (!match_leading_pathspec(NULL, pathspec, 51 match->buf, match->len, 52 0, NULL, is_dir)) 53 continue; 54 } 55 56 string_list_insert(list, e->d_name); 57 } 58 59 strbuf_setlen(match, len); 60 closedir(dir); 61 return 0; 62 } 63 64 /* 65 * This should be "(standard input)" or something, but it will 66 * probably expose many more breakages in the way no-index code 67 * is bolted onto the diff callchain. 68 */ 69 static const char file_from_standard_input[] = "-"; 70 71 /* 72 * For paths given on the command-line we treat "-" as stdin and named 73 * pipes and symbolic links to named pipes specially. 74 */ 75 enum special { 76 SPECIAL_NONE, 77 SPECIAL_STDIN, 78 SPECIAL_PIPE, 79 }; 80 81 static int get_mode(const char *path, int *mode, enum special *special) 82 { 83 struct stat st; 84 85 if (!path || !strcmp(path, "/dev/null")) { 86 *mode = 0; 87 #ifdef GIT_WINDOWS_NATIVE 88 } else if (!strcasecmp(path, "nul")) { 89 *mode = 0; 90 #endif 91 } else if (path == file_from_standard_input) { 92 *mode = create_ce_mode(0666); 93 *special = SPECIAL_STDIN; 94 } else if (lstat(path, &st)) { 95 return error("Could not access '%s'", path); 96 } else { 97 *mode = st.st_mode; 98 } 99 /* 100 * For paths on the command-line treat named pipes and symbolic 101 * links that resolve to a named pipe specially. 102 */ 103 if (special && 104 (S_ISFIFO(*mode) || 105 (S_ISLNK(*mode) && !stat(path, &st) && S_ISFIFO(st.st_mode)))) { 106 *mode = create_ce_mode(0666); 107 *special = SPECIAL_PIPE; 108 } 109 110 return 0; 111 } 112 113 static void populate_common(struct diff_filespec *s, struct strbuf *buf) 114 { 115 size_t size = 0; 116 117 s->should_munmap = 0; 118 s->data = strbuf_detach(buf, &size); 119 s->size = size; 120 s->should_free = 1; 121 s->is_stdin = 1; 122 } 123 124 static void populate_from_pipe(struct diff_filespec *s) 125 { 126 struct strbuf buf = STRBUF_INIT; 127 int fd = xopen(s->path, O_RDONLY); 128 129 if (strbuf_read(&buf, fd, 0) < 0) 130 die_errno("error while reading from '%s'", s->path); 131 close(fd); 132 populate_common(s, &buf); 133 } 134 135 static void populate_from_stdin(struct diff_filespec *s) 136 { 137 struct strbuf buf = STRBUF_INIT; 138 139 if (strbuf_read(&buf, 0, 0) < 0) 140 die_errno("error while reading from stdin"); 141 populate_common(s, &buf); 142 } 143 144 static struct diff_filespec *noindex_filespec(const struct git_hash_algo *algop, 145 const char *name, int mode, 146 enum special special) 147 { 148 struct diff_filespec *s; 149 150 if (!name) 151 name = "/dev/null"; 152 s = alloc_filespec(name); 153 fill_filespec(s, null_oid(algop), 0, mode); 154 if (special == SPECIAL_STDIN) 155 populate_from_stdin(s); 156 else if (special == SPECIAL_PIPE) 157 populate_from_pipe(s); 158 return s; 159 } 160 161 static int queue_diff(struct diff_options *o, const struct git_hash_algo *algop, 162 const char *name1, const char *name2, int recursing, 163 const struct pathspec *ps, 164 struct strbuf *ps_match1, struct strbuf *ps_match2) 165 { 166 int mode1 = 0, mode2 = 0; 167 enum special special1 = SPECIAL_NONE, special2 = SPECIAL_NONE; 168 169 /* Paths can only be special if we're not recursing. */ 170 if (get_mode(name1, &mode1, recursing ? NULL : &special1) || 171 get_mode(name2, &mode2, recursing ? NULL : &special2)) 172 return -1; 173 174 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) { 175 struct diff_filespec *d1, *d2; 176 177 if (S_ISDIR(mode1)) { 178 /* 2 is file that is created */ 179 d1 = noindex_filespec(algop, NULL, 0, SPECIAL_NONE); 180 d2 = noindex_filespec(algop, name2, mode2, special2); 181 name2 = NULL; 182 mode2 = 0; 183 } else { 184 /* 1 is file that is deleted */ 185 d1 = noindex_filespec(algop, name1, mode1, special1); 186 d2 = noindex_filespec(algop, NULL, 0, SPECIAL_NONE); 187 name1 = NULL; 188 mode1 = 0; 189 } 190 /* emit that file */ 191 if (o->flags.reverse_diff) 192 SWAP(d1, d2); 193 diff_queue(&diff_queued_diff, d1, d2); 194 195 /* and then let the entire directory be created or deleted */ 196 } 197 198 if (S_ISDIR(mode1) || S_ISDIR(mode2)) { 199 struct strbuf buffer1 = STRBUF_INIT; 200 struct strbuf buffer2 = STRBUF_INIT; 201 struct string_list p1 = STRING_LIST_INIT_DUP; 202 struct string_list p2 = STRING_LIST_INIT_DUP; 203 int i1, i2, ret = 0; 204 size_t len1 = 0, len2 = 0; 205 size_t match1_len = ps_match1->len; 206 size_t match2_len = ps_match2->len; 207 208 if (name1 && read_directory_contents(name1, &p1, ps, ps_match1)) 209 return -1; 210 if (name2 && read_directory_contents(name2, &p2, ps, ps_match2)) { 211 string_list_clear(&p1, 0); 212 return -1; 213 } 214 215 if (name1) { 216 strbuf_addstr(&buffer1, name1); 217 strbuf_complete(&buffer1, '/'); 218 len1 = buffer1.len; 219 } 220 221 if (name2) { 222 strbuf_addstr(&buffer2, name2); 223 strbuf_complete(&buffer2, '/'); 224 len2 = buffer2.len; 225 } 226 227 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) { 228 const char *n1, *n2; 229 int comp; 230 231 strbuf_setlen(&buffer1, len1); 232 strbuf_setlen(&buffer2, len2); 233 234 if (ps) { 235 strbuf_setlen(ps_match1, match1_len); 236 strbuf_setlen(ps_match2, match2_len); 237 } 238 239 if (i1 == p1.nr) 240 comp = 1; 241 else if (i2 == p2.nr) 242 comp = -1; 243 else 244 comp = strcmp(p1.items[i1].string, p2.items[i2].string); 245 246 if (comp > 0) 247 n1 = NULL; 248 else { 249 strbuf_addstr(&buffer1, p1.items[i1].string); 250 if (ps) { 251 strbuf_addstr(ps_match1, p1.items[i1].string); 252 strbuf_complete(ps_match1, '/'); 253 } 254 n1 = buffer1.buf; 255 i1++; 256 } 257 258 if (comp < 0) 259 n2 = NULL; 260 else { 261 strbuf_addstr(&buffer2, p2.items[i2].string); 262 if (ps) { 263 strbuf_addstr(ps_match2, p2.items[i2].string); 264 strbuf_complete(ps_match2, '/'); 265 } 266 n2 = buffer2.buf; 267 i2++; 268 } 269 270 ret = queue_diff(o, algop, n1, n2, 1, ps, ps_match1, ps_match2); 271 } 272 string_list_clear(&p1, 0); 273 string_list_clear(&p2, 0); 274 strbuf_release(&buffer1); 275 strbuf_release(&buffer2); 276 277 return ret; 278 } else { 279 struct diff_filespec *d1, *d2; 280 281 if (o->flags.reverse_diff) { 282 SWAP(mode1, mode2); 283 SWAP(name1, name2); 284 SWAP(special1, special2); 285 } 286 287 d1 = noindex_filespec(algop, name1, mode1, special1); 288 d2 = noindex_filespec(algop, name2, mode2, special2); 289 diff_queue(&diff_queued_diff, d1, d2); 290 return 0; 291 } 292 } 293 294 /* append basename of F to D */ 295 static void append_basename(struct strbuf *path, const char *dir, const char *file) 296 { 297 const char *tail = strrchr(file, '/'); 298 299 strbuf_addstr(path, dir); 300 while (path->len && path->buf[path->len - 1] == '/') 301 path->len--; 302 strbuf_addch(path, '/'); 303 strbuf_addstr(path, tail ? tail + 1 : file); 304 } 305 306 /* 307 * DWIM "diff D F" into "diff D/F F" and "diff F D" into "diff F D/F" 308 * Note that we append the basename of F to D/, so "diff a/b/file D" 309 * becomes "diff a/b/file D/file", not "diff a/b/file D/a/b/file". 310 * 311 * Return 1 if both paths are directories, 0 otherwise. 312 */ 313 static int fixup_paths(const char **path, struct strbuf *replacement) 314 { 315 struct stat st; 316 unsigned int isdir0 = 0, isdir1 = 0; 317 unsigned int ispipe0 = 0, ispipe1 = 0; 318 319 if (path[0] != file_from_standard_input && !stat(path[0], &st)) { 320 isdir0 = S_ISDIR(st.st_mode); 321 ispipe0 = S_ISFIFO(st.st_mode); 322 } 323 324 if (path[1] != file_from_standard_input && !stat(path[1], &st)) { 325 isdir1 = S_ISDIR(st.st_mode); 326 ispipe1 = S_ISFIFO(st.st_mode); 327 } 328 329 if ((path[0] == file_from_standard_input && isdir1) || 330 (isdir0 && path[1] == file_from_standard_input)) 331 die(_("cannot compare stdin to a directory")); 332 333 if ((isdir0 && ispipe1) || (ispipe0 && isdir1)) 334 die(_("cannot compare a named pipe to a directory")); 335 336 /* if both paths are directories, we will enable pathspecs */ 337 if (isdir0 && isdir1) 338 return 1; 339 340 if (isdir0) { 341 append_basename(replacement, path[0], path[1]); 342 path[0] = replacement->buf; 343 } else if (isdir1) { 344 append_basename(replacement, path[1], path[0]); 345 path[1] = replacement->buf; 346 } 347 348 return 0; 349 } 350 351 static const char * const diff_no_index_usage[] = { 352 N_("git diff --no-index [] [...]"), 353 NULL 354 }; 355 356 int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop, 357 int implicit_no_index, int argc, const char **argv) 358 { 359 struct pathspec pathspec, *ps = NULL; 360 struct strbuf ps_match1 = STRBUF_INIT, ps_match2 = STRBUF_INIT; 361 int i, no_index; 362 int ret = 1; 363 const char *paths[2]; 364 char *to_free[ARRAY_SIZE(paths)] = { 0 }; 365 struct strbuf replacement = STRBUF_INIT; 366 const char *prefix = revs->prefix; 367 struct option no_index_options[] = { 368 OPT_BOOL_F(0, "no-index", &no_index, "", 369 PARSE_OPT_NONEG | PARSE_OPT_HIDDEN), 370 OPT_END(), 371 }; 372 struct option *options; 373 374 options = add_diff_options(no_index_options, &revs->diffopt); 375 argc = parse_options(argc, argv, revs->prefix, options, 376 diff_no_index_usage, 0); 377 if (argc < 2) { 378 if (implicit_no_index) 379 warning(_("Not a git repository. Use --no-index to " 380 "compare two paths outside a working tree")); 381 usage_with_options(diff_no_index_usage, options); 382 } 383 for (i = 0; i < 2; i++) { 384 const char *p = argv[i]; 385 if (!strcmp(p, "-")) 386 /* 387 * stdin should be spelled as "-"; if you have 388 * path that is "-", spell it as "./-". 389 */ 390 p = file_from_standard_input; 391 else if (prefix) 392 p = to_free[i] = prefix_filename(prefix, p); 393 paths[i] = p; 394 } 395 396 if (fixup_paths(paths, &replacement)) { 397 parse_pathspec(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_ATTR, 398 PATHSPEC_PREFER_FULL | PATHSPEC_NO_REPOSITORY, 399 NULL, &argv[2]); 400 if (pathspec.nr) 401 ps = &pathspec; 402 } else if (argc > 2) { 403 warning(_("Limiting comparison with pathspecs is only " 404 "supported if both paths are directories.")); 405 usage_with_options(diff_no_index_usage, options); 406 } 407 FREE_AND_NULL(options); 408 409 revs->diffopt.skip_stat_unmatch = 1; 410 if (!revs->diffopt.output_format) 411 revs->diffopt.output_format = DIFF_FORMAT_PATCH; 412 413 revs->diffopt.flags.no_index = 1; 414 415 revs->diffopt.flags.relative_name = 1; 416 revs->diffopt.prefix = prefix; 417 418 revs->max_count = -2; 419 diff_setup_done(&revs->diffopt); 420 421 setup_diff_pager(&revs->diffopt); 422 revs->diffopt.flags.exit_with_status = 1; 423 424 if (queue_diff(&revs->diffopt, algop, paths[0], paths[1], 0, ps, 425 &ps_match1, &ps_match2)) 426 goto out; 427 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/"); 428 diffcore_std(&revs->diffopt); 429 diff_flush(&revs->diffopt); 430 431 /* 432 * The return code for --no-index imitates diff(1): 433 * 0 = no changes, 1 = changes, else error 434 */ 435 ret = diff_result_code(revs); 436 437 out: 438 for (i = 0; i < ARRAY_SIZE(to_free); i++) 439 free(to_free[i]); 440 strbuf_release(&replacement); 441 strbuf_release(&ps_match1); 442 strbuf_release(&ps_match2); 443 if (ps) 444 clear_pathspec(ps); 445 return ret; 446 }