| 1 | /* |
| 2 | * This merges the file listing in the directory cache index |
| 3 | * with the actual working directory list, and shows different |
| 4 | * combinations of the two. |
| 5 | * |
| 6 | * Copyright (C) Linus Torvalds, 2005 |
| 7 | */ |
| 8 | |
| 9 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 10 | |
| 11 | #include "builtin.h" |
| 12 | #include "config.h" |
| 13 | #include "convert.h" |
| 14 | #include "environment.h" |
| 15 | #include "quote.h" |
| 16 | #include "dir.h" |
| 17 | #include "gettext.h" |
| 18 | #include "object-name.h" |
| 19 | #include "strbuf.h" |
| 20 | #include "parse-options.h" |
| 21 | #include "resolve-undo.h" |
| 22 | #include "string-list.h" |
| 23 | #include "path.h" |
| 24 | #include "pathspec.h" |
| 25 | #include "read-cache.h" |
| 26 | #include "setup.h" |
| 27 | #include "sparse-index.h" |
| 28 | #include "submodule.h" |
| 29 | #include "odb.h" |
| 30 | #include "hex.h" |
| 31 | |
| 32 | |
| 33 | static int abbrev; |
| 34 | static int show_deleted; |
| 35 | static int show_cached; |
| 36 | static int show_others; |
| 37 | static int show_stage; |
| 38 | static int show_unmerged; |
| 39 | static int show_resolve_undo; |
| 40 | static int show_modified; |
| 41 | static int show_killed; |
| 42 | static int show_valid_bit; |
| 43 | static int show_fsmonitor_bit; |
| 44 | static int line_terminator = '\n'; |
| 45 | static int debug_mode; |
| 46 | static int show_eol; |
| 47 | static int recurse_submodules; |
| 48 | static int skipping_duplicates; |
| 49 | static int show_sparse_dirs; |
| 50 | |
| 51 | static const char *prefix; |
| 52 | static int max_prefix_len; |
| 53 | static int prefix_len; |
| 54 | static struct pathspec pathspec; |
| 55 | static int error_unmatch; |
| 56 | static char *ps_matched; |
| 57 | static const char *with_tree; |
| 58 | static int exc_given; |
| 59 | static int exclude_args; |
| 60 | static const char *format; |
| 61 | |
| 62 | static const char *tag_cached = ""; |
| 63 | static const char *tag_unmerged = ""; |
| 64 | static const char *tag_removed = ""; |
| 65 | static const char *tag_other = ""; |
| 66 | static const char *tag_killed = ""; |
| 67 | static const char *tag_modified = ""; |
| 68 | static const char *tag_skip_worktree = ""; |
| 69 | static const char *tag_resolve_undo = ""; |
| 70 | |
| 71 | static void write_eolinfo(struct index_state *istate, |
| 72 | const struct cache_entry *ce, const char *path) |
| 73 | { |
| 74 | if (show_eol) { |
| 75 | struct stat st; |
| 76 | const char *i_txt = ""; |
| 77 | const char *w_txt = ""; |
| 78 | const char *a_txt = get_convert_attr_ascii(istate, path); |
| 79 | if (ce && S_ISREG(ce->ce_mode)) |
| 80 | i_txt = get_cached_convert_stats_ascii(istate, |
| 81 | ce->name); |
| 82 | if (!lstat(path, &st) && S_ISREG(st.st_mode)) |
| 83 | w_txt = get_wt_convert_stats_ascii(path); |
| 84 | printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void write_name(const char *name) |
| 89 | { |
| 90 | /* |
| 91 | * With "--full-name", prefix_len=0; this caller needs to pass |
| 92 | * an empty string in that case (a NULL is good for ""). |
| 93 | */ |
| 94 | write_name_quoted_relative(name, prefix_len ? prefix : NULL, |
| 95 | stdout, line_terminator); |
| 96 | } |
| 97 | |
| 98 | static void write_name_to_buf(struct strbuf *sb, const char *name) |
| 99 | { |
| 100 | struct strbuf buf = STRBUF_INIT; |
| 101 | const char *rel = relative_path(name, prefix_len ? prefix : NULL, &buf); |
| 102 | |
| 103 | if (line_terminator) |
| 104 | quote_c_style(rel, sb, NULL, 0); |
| 105 | else |
| 106 | strbuf_addstr(sb, rel); |
| 107 | |
| 108 | strbuf_release(&buf); |
| 109 | } |
| 110 | |
| 111 | static const char *get_tag(const struct cache_entry *ce, const char *tag) |
| 112 | { |
| 113 | static char alttag[4]; |
| 114 | |
| 115 | if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) || |
| 116 | (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) { |
| 117 | memcpy(alttag, tag, 3); |
| 118 | |
| 119 | if (isalpha(tag[0])) { |
| 120 | alttag[0] = tolower(tag[0]); |
| 121 | } else if (tag[0] == '?') { |
| 122 | alttag[0] = '!'; |
| 123 | } else { |
| 124 | alttag[0] = 'v'; |
| 125 | alttag[1] = tag[0]; |
| 126 | alttag[2] = ' '; |
| 127 | alttag[3] = 0; |
| 128 | } |
| 129 | |
| 130 | tag = alttag; |
| 131 | } |
| 132 | |
| 133 | return tag; |
| 134 | } |
| 135 | |
| 136 | static void print_debug(const struct cache_entry *ce) |
| 137 | { |
| 138 | if (debug_mode) { |
| 139 | const struct stat_data *sd = &ce->ce_stat_data; |
| 140 | |
| 141 | printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec); |
| 142 | printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec); |
| 143 | printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino); |
| 144 | printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid); |
| 145 | printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void show_dir_entry(struct index_state *istate, |
| 150 | const char *tag, struct dir_entry *ent) |
| 151 | { |
| 152 | int len = max_prefix_len; |
| 153 | |
| 154 | if (len > ent->len) |
| 155 | die("git ls-files: internal error - directory entry not superset of prefix"); |
| 156 | |
| 157 | /* If ps_matches is non-NULL, figure out which pathspec(s) match. */ |
| 158 | if (ps_matched) |
| 159 | dir_path_match(istate, ent, &pathspec, len, ps_matched); |
| 160 | |
| 161 | fputs(tag, stdout); |
| 162 | write_eolinfo(istate, NULL, ent->name); |
| 163 | write_name(ent->name); |
| 164 | } |
| 165 | |
| 166 | static void show_other_files(struct index_state *istate, |
| 167 | const struct dir_struct *dir) |
| 168 | { |
| 169 | int i; |
| 170 | |
| 171 | for (i = 0; i < dir->nr; i++) { |
| 172 | struct dir_entry *ent = dir->entries[i]; |
| 173 | if (!index_name_is_other(istate, ent->name, ent->len)) |
| 174 | continue; |
| 175 | show_dir_entry(istate, tag_other, ent); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void show_killed_files(struct index_state *istate, |
| 180 | const struct dir_struct *dir) |
| 181 | { |
| 182 | int i; |
| 183 | for (i = 0; i < dir->nr; i++) { |
| 184 | struct dir_entry *ent = dir->entries[i]; |
| 185 | char *cp, *sp; |
| 186 | int pos, len, killed = 0; |
| 187 | |
| 188 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { |
| 189 | sp = strchr(cp, '/'); |
| 190 | if (!sp) { |
| 191 | /* If ent->name is prefix of an entry in the |
| 192 | * cache, it will be killed. |
| 193 | */ |
| 194 | pos = index_name_pos(istate, ent->name, ent->len); |
| 195 | if (0 <= pos) |
| 196 | BUG("killed-file %.*s not found", |
| 197 | ent->len, ent->name); |
| 198 | pos = -pos - 1; |
| 199 | while (pos < istate->cache_nr && |
| 200 | ce_stage(istate->cache[pos])) |
| 201 | pos++; /* skip unmerged */ |
| 202 | if (istate->cache_nr <= pos) |
| 203 | break; |
| 204 | /* pos points at a name immediately after |
| 205 | * ent->name in the cache. Does it expect |
| 206 | * ent->name to be a directory? |
| 207 | */ |
| 208 | len = ce_namelen(istate->cache[pos]); |
| 209 | if ((ent->len < len) && |
| 210 | !strncmp(istate->cache[pos]->name, |
| 211 | ent->name, ent->len) && |
| 212 | istate->cache[pos]->name[ent->len] == '/') |
| 213 | killed = 1; |
| 214 | break; |
| 215 | } |
| 216 | if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) { |
| 217 | /* If any of the leading directories in |
| 218 | * ent->name is registered in the cache, |
| 219 | * ent->name will be killed. |
| 220 | */ |
| 221 | killed = 1; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | if (killed) |
| 226 | show_dir_entry(istate, tag_killed, dir->entries[i]); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static void show_files(struct repository *repo, struct dir_struct *dir); |
| 231 | |
| 232 | static void show_submodule(struct repository *superproject, |
| 233 | struct dir_struct *dir, const char *path) |
| 234 | { |
| 235 | struct repository subrepo; |
| 236 | |
| 237 | if (repo_submodule_init(&subrepo, superproject, path, |
| 238 | null_oid(superproject->hash_algo))) |
| 239 | return; |
| 240 | |
| 241 | if (repo_read_index(&subrepo) < 0) |
| 242 | die("index file corrupt"); |
| 243 | |
| 244 | show_files(&subrepo, dir); |
| 245 | |
| 246 | repo_clear(&subrepo); |
| 247 | } |
| 248 | |
| 249 | static void expand_objectsize(struct repository *repo, struct strbuf *line, |
| 250 | const struct object_id *oid, |
| 251 | const enum object_type type, unsigned int padded) |
| 252 | { |
| 253 | static const char padding[] = " "; |
| 254 | size_t min_len = padded ? strlen(padding) : 0; |
| 255 | size_t orig_len = line->len; |
| 256 | size_t len; |
| 257 | |
| 258 | if (type == OBJ_BLOB) { |
| 259 | size_t size; |
| 260 | if (odb_read_object_info(repo->objects, oid, &size) < 0) |
| 261 | die(_("could not get object info about '%s'"), |
| 262 | oid_to_hex(oid)); |
| 263 | strbuf_add_uint(line, size); |
| 264 | } else { |
| 265 | strbuf_addstr(line, "-"); |
| 266 | } |
| 267 | len = line->len - orig_len; |
| 268 | if (len < min_len) |
| 269 | strbuf_insert(line, orig_len, padding, min_len - len); |
| 270 | } |
| 271 | |
| 272 | static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce, |
| 273 | const char *format, const char *fullname) { |
| 274 | struct strbuf sb = STRBUF_INIT; |
| 275 | |
| 276 | while (strbuf_expand_step(&sb, &format)) { |
| 277 | size_t len; |
| 278 | struct stat st; |
| 279 | |
| 280 | if (skip_prefix(format, "%", &format)) |
| 281 | strbuf_addch(&sb, '%'); |
| 282 | else if ((len = strbuf_expand_literal(&sb, format))) |
| 283 | format += len; |
| 284 | else if (skip_prefix(format, "(objectmode)", &format)) |
| 285 | strbuf_addf(&sb, "%06o", ce->ce_mode); |
| 286 | else if (skip_prefix(format, "(objectname)", &format)) |
| 287 | strbuf_add_unique_abbrev(&sb, &ce->oid, abbrev); |
| 288 | else if (skip_prefix(format, "(objecttype)", &format)) |
| 289 | strbuf_addstr(&sb, type_name(object_type(ce->ce_mode))); |
| 290 | else if (skip_prefix(format, "(objectsize:padded)", &format)) |
| 291 | expand_objectsize(repo, &sb, &ce->oid, |
| 292 | object_type(ce->ce_mode), 1); |
| 293 | else if (skip_prefix(format, "(objectsize)", &format)) |
| 294 | expand_objectsize(repo, &sb, &ce->oid, |
| 295 | object_type(ce->ce_mode), 0); |
| 296 | else if (skip_prefix(format, "(stage)", &format)) |
| 297 | strbuf_addf(&sb, "%d", ce_stage(ce)); |
| 298 | else if (skip_prefix(format, "(eolinfo:index)", &format)) |
| 299 | strbuf_addstr(&sb, S_ISREG(ce->ce_mode) ? |
| 300 | get_cached_convert_stats_ascii(repo->index, |
| 301 | ce->name) : ""); |
| 302 | else if (skip_prefix(format, "(eolinfo:worktree)", &format)) |
| 303 | strbuf_addstr(&sb, !lstat(fullname, &st) && |
| 304 | S_ISREG(st.st_mode) ? |
| 305 | get_wt_convert_stats_ascii(fullname) : ""); |
| 306 | else if (skip_prefix(format, "(eolattr)", &format)) |
| 307 | strbuf_addstr(&sb, get_convert_attr_ascii(repo->index, |
| 308 | fullname)); |
| 309 | else if (skip_prefix(format, "(path)", &format)) |
| 310 | write_name_to_buf(&sb, fullname); |
| 311 | else |
| 312 | strbuf_expand_bad_format(format, "ls-files"); |
| 313 | } |
| 314 | strbuf_addch(&sb, line_terminator); |
| 315 | fwrite(sb.buf, sb.len, 1, stdout); |
| 316 | strbuf_release(&sb); |
| 317 | } |
| 318 | |
| 319 | static void show_ce(struct repository *repo, struct dir_struct *dir, |
| 320 | const struct cache_entry *ce, const char *fullname, |
| 321 | const char *tag) |
| 322 | { |
| 323 | if (max_prefix_len > strlen(fullname)) |
| 324 | die("git ls-files: internal error - cache entry not superset of prefix"); |
| 325 | |
| 326 | if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && |
| 327 | is_submodule_active(repo, ce->name)) { |
| 328 | show_submodule(repo, dir, ce->name); |
| 329 | } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname), |
| 330 | max_prefix_len, ps_matched, |
| 331 | S_ISDIR(ce->ce_mode) || |
| 332 | S_ISGITLINK(ce->ce_mode))) { |
| 333 | if (format) { |
| 334 | show_ce_fmt(repo, ce, format, fullname); |
| 335 | print_debug(ce); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | tag = get_tag(ce, tag); |
| 340 | |
| 341 | if (!show_stage) { |
| 342 | fputs(tag, stdout); |
| 343 | } else { |
| 344 | printf("%s%06o %s %d\t", |
| 345 | tag, |
| 346 | ce->ce_mode, |
| 347 | repo_find_unique_abbrev(repo, &ce->oid, abbrev), |
| 348 | ce_stage(ce)); |
| 349 | } |
| 350 | write_eolinfo(repo->index, ce, fullname); |
| 351 | write_name(fullname); |
| 352 | print_debug(ce); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void show_ru_info(struct repository *repo, struct index_state *istate) |
| 357 | { |
| 358 | struct string_list_item *item; |
| 359 | |
| 360 | if (!istate->resolve_undo) |
| 361 | return; |
| 362 | |
| 363 | for_each_string_list_item(item, istate->resolve_undo) { |
| 364 | const char *path = item->string; |
| 365 | struct resolve_undo_info *ui = item->util; |
| 366 | int i, len; |
| 367 | |
| 368 | len = strlen(path); |
| 369 | if (len < max_prefix_len) |
| 370 | continue; /* outside of the prefix */ |
| 371 | if (!match_pathspec(istate, &pathspec, path, len, |
| 372 | max_prefix_len, ps_matched, 0)) |
| 373 | continue; /* uninterested */ |
| 374 | for (i = 0; i < 3; i++) { |
| 375 | if (!ui->mode[i]) |
| 376 | continue; |
| 377 | printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], |
| 378 | repo_find_unique_abbrev(repo, &ui->oid[i], abbrev), |
| 379 | i + 1); |
| 380 | write_name(path); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static int ce_excluded(struct dir_struct *dir, struct index_state *istate, |
| 386 | const char *fullname, const struct cache_entry *ce) |
| 387 | { |
| 388 | int dtype = ce_to_dtype(ce); |
| 389 | return is_excluded(dir, istate, fullname, &dtype); |
| 390 | } |
| 391 | |
| 392 | static void construct_fullname(struct strbuf *out, const struct repository *repo, |
| 393 | const struct cache_entry *ce) |
| 394 | { |
| 395 | strbuf_reset(out); |
| 396 | if (repo->submodule_prefix) |
| 397 | strbuf_addstr(out, repo->submodule_prefix); |
| 398 | strbuf_addstr(out, ce->name); |
| 399 | } |
| 400 | |
| 401 | static void show_files(struct repository *repo, struct dir_struct *dir) |
| 402 | { |
| 403 | int i; |
| 404 | struct strbuf fullname = STRBUF_INIT; |
| 405 | |
| 406 | /* For cached/deleted files we don't need to even do the readdir */ |
| 407 | if (show_others || show_killed) { |
| 408 | if (!show_others) |
| 409 | dir->flags |= DIR_COLLECT_KILLED_ONLY; |
| 410 | fill_directory(dir, repo->index, &pathspec); |
| 411 | if (show_others) |
| 412 | show_other_files(repo->index, dir); |
| 413 | if (show_killed) |
| 414 | show_killed_files(repo->index, dir); |
| 415 | } |
| 416 | |
| 417 | if (!(show_cached || show_stage || show_deleted || show_modified)) |
| 418 | return; |
| 419 | |
| 420 | for (i = 0; i < repo->index->cache_nr; i++) { |
| 421 | const struct cache_entry *ce = repo->index->cache[i]; |
| 422 | struct stat st; |
| 423 | int stat_err; |
| 424 | |
| 425 | if (S_ISSPARSEDIR(ce->ce_mode) && !show_sparse_dirs) { |
| 426 | /* |
| 427 | * This is the first time we've hit a sparse dir, |
| 428 | * so expansion will leave the first 'i' entries |
| 429 | * alone. |
| 430 | */ |
| 431 | ensure_full_index(repo->index); |
| 432 | ce = repo->index->cache[i]; |
| 433 | } |
| 434 | |
| 435 | construct_fullname(&fullname, repo, ce); |
| 436 | |
| 437 | if ((dir->flags & DIR_SHOW_IGNORED) && |
| 438 | !ce_excluded(dir, repo->index, fullname.buf, ce)) |
| 439 | continue; |
| 440 | if (ce->ce_flags & CE_UPDATE) |
| 441 | continue; |
| 442 | if ((show_cached || show_stage) && |
| 443 | (!show_unmerged || ce_stage(ce))) { |
| 444 | show_ce(repo, dir, ce, fullname.buf, |
| 445 | ce_stage(ce) ? tag_unmerged : |
| 446 | (ce_skip_worktree(ce) ? tag_skip_worktree : |
| 447 | tag_cached)); |
| 448 | if (skipping_duplicates) |
| 449 | goto skip_to_next_name; |
| 450 | } |
| 451 | |
| 452 | if (!(show_deleted || show_modified)) |
| 453 | continue; |
| 454 | if (ce_skip_worktree(ce)) |
| 455 | continue; |
| 456 | /* |
| 457 | * match_pathspec() is linear in pathspec.nr, so prefilter only |
| 458 | * the single-pathspec case. Only entries shown by show_ce() |
| 459 | * satisfy --error-unmatch. |
| 460 | */ |
| 461 | if (pathspec.nr == 1 && |
| 462 | !match_pathspec(repo->index, &pathspec, fullname.buf, |
| 463 | fullname.len, max_prefix_len, NULL, |
| 464 | S_ISDIR(ce->ce_mode) || |
| 465 | S_ISGITLINK(ce->ce_mode))) |
| 466 | continue; |
| 467 | stat_err = lstat(fullname.buf, &st); |
| 468 | if (stat_err && (errno != ENOENT && errno != ENOTDIR)) |
| 469 | error_errno("cannot lstat '%s'", fullname.buf); |
| 470 | if (stat_err && show_deleted) { |
| 471 | show_ce(repo, dir, ce, fullname.buf, tag_removed); |
| 472 | if (skipping_duplicates) |
| 473 | goto skip_to_next_name; |
| 474 | } |
| 475 | if (show_modified && |
| 476 | (stat_err || ie_modified(repo->index, ce, &st, 0))) { |
| 477 | show_ce(repo, dir, ce, fullname.buf, tag_modified); |
| 478 | if (skipping_duplicates) |
| 479 | goto skip_to_next_name; |
| 480 | } |
| 481 | continue; |
| 482 | |
| 483 | skip_to_next_name: |
| 484 | { |
| 485 | int j; |
| 486 | struct cache_entry **cache = repo->index->cache; |
| 487 | for (j = i + 1; j < repo->index->cache_nr; j++) |
| 488 | if (strcmp(ce->name, cache[j]->name)) |
| 489 | break; |
| 490 | i = j - 1; /* compensate for the for loop */ |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | strbuf_release(&fullname); |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Prune the index to only contain stuff starting with "prefix" |
| 499 | */ |
| 500 | static void prune_index(struct index_state *istate, |
| 501 | const char *prefix, size_t prefixlen) |
| 502 | { |
| 503 | int pos; |
| 504 | unsigned int first, last; |
| 505 | |
| 506 | if (!prefix || !istate->cache_nr) |
| 507 | return; |
| 508 | pos = index_name_pos(istate, prefix, prefixlen); |
| 509 | if (pos < 0) |
| 510 | pos = -pos-1; |
| 511 | first = pos; |
| 512 | last = istate->cache_nr; |
| 513 | while (last > first) { |
| 514 | int next = first + ((last - first) >> 1); |
| 515 | const struct cache_entry *ce = istate->cache[next]; |
| 516 | if (!strncmp(ce->name, prefix, prefixlen)) { |
| 517 | first = next+1; |
| 518 | continue; |
| 519 | } |
| 520 | last = next; |
| 521 | } |
| 522 | MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos); |
| 523 | istate->cache_nr = last - pos; |
| 524 | } |
| 525 | |
| 526 | static int get_common_prefix_len(const char *common_prefix) |
| 527 | { |
| 528 | int common_prefix_len; |
| 529 | |
| 530 | if (!common_prefix) |
| 531 | return 0; |
| 532 | |
| 533 | common_prefix_len = strlen(common_prefix); |
| 534 | |
| 535 | /* |
| 536 | * If the prefix has a trailing slash, strip it so that submodules won't |
| 537 | * be pruned from the index. |
| 538 | */ |
| 539 | if (common_prefix[common_prefix_len - 1] == '/') |
| 540 | common_prefix_len--; |
| 541 | |
| 542 | return common_prefix_len; |
| 543 | } |
| 544 | |
| 545 | static const char * const ls_files_usage[] = { |
| 546 | N_("git ls-files [<options>] [<file>...]"), |
| 547 | NULL |
| 548 | }; |
| 549 | |
| 550 | static int option_parse_exclude(const struct option *opt, |
| 551 | const char *arg, int unset) |
| 552 | { |
| 553 | struct string_list *exclude_list = opt->value; |
| 554 | |
| 555 | BUG_ON_OPT_NEG(unset); |
| 556 | |
| 557 | exc_given = 1; |
| 558 | string_list_append(exclude_list, arg); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int option_parse_exclude_from(const struct option *opt, |
| 564 | const char *arg, int unset) |
| 565 | { |
| 566 | struct dir_struct *dir = opt->value; |
| 567 | |
| 568 | BUG_ON_OPT_NEG(unset); |
| 569 | |
| 570 | exc_given = 1; |
| 571 | add_patterns_from_file(dir, arg); |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int option_parse_exclude_standard(const struct option *opt, |
| 577 | const char *arg, int unset) |
| 578 | { |
| 579 | struct dir_struct *dir = opt->value; |
| 580 | |
| 581 | BUG_ON_OPT_NEG(unset); |
| 582 | BUG_ON_OPT_ARG(arg); |
| 583 | |
| 584 | exc_given = 1; |
| 585 | setup_standard_excludes(dir); |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | int cmd_ls_files(int argc, |
| 591 | const char **argv, |
| 592 | const char *cmd_prefix, |
| 593 | struct repository *repo) |
| 594 | { |
| 595 | int require_work_tree = 0, show_tag = 0, i; |
| 596 | char *max_prefix; |
| 597 | struct dir_struct dir = DIR_INIT; |
| 598 | struct pattern_list *pl; |
| 599 | struct string_list exclude_list = STRING_LIST_INIT_NODUP; |
| 600 | struct option builtin_ls_files_options[] = { |
| 601 | /* Think twice before adding "--nul" synonym to this */ |
| 602 | OPT_SET_INT('z', NULL, &line_terminator, |
| 603 | N_("separate paths with the NUL character"), '\0'), |
| 604 | OPT_BOOL('t', NULL, &show_tag, |
| 605 | N_("identify the file status with tags")), |
| 606 | OPT_BOOL('v', NULL, &show_valid_bit, |
| 607 | N_("use lowercase letters for 'assume unchanged' files")), |
| 608 | OPT_BOOL('f', NULL, &show_fsmonitor_bit, |
| 609 | N_("use lowercase letters for 'fsmonitor clean' files")), |
| 610 | OPT_BOOL('c', "cached", &show_cached, |
| 611 | N_("show cached files in the output (default)")), |
| 612 | OPT_BOOL('d', "deleted", &show_deleted, |
| 613 | N_("show deleted files in the output")), |
| 614 | OPT_BOOL('m', "modified", &show_modified, |
| 615 | N_("show modified files in the output")), |
| 616 | OPT_BOOL('o', "others", &show_others, |
| 617 | N_("show other files in the output")), |
| 618 | OPT_BIT('i', "ignored", &dir.flags, |
| 619 | N_("show ignored files in the output"), |
| 620 | DIR_SHOW_IGNORED), |
| 621 | OPT_BOOL('s', "stage", &show_stage, |
| 622 | N_("show staged contents' object name in the output")), |
| 623 | OPT_BOOL('k', "killed", &show_killed, |
| 624 | N_("show files on the filesystem that need to be removed")), |
| 625 | OPT_BIT(0, "directory", &dir.flags, |
| 626 | N_("show 'other' directories' names only"), |
| 627 | DIR_SHOW_OTHER_DIRECTORIES), |
| 628 | OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")), |
| 629 | OPT_NEGBIT(0, "empty-directory", &dir.flags, |
| 630 | N_("don't show empty directories"), |
| 631 | DIR_HIDE_EMPTY_DIRECTORIES), |
| 632 | OPT_BOOL('u', "unmerged", &show_unmerged, |
| 633 | N_("show unmerged files in the output")), |
| 634 | OPT_BOOL(0, "resolve-undo", &show_resolve_undo, |
| 635 | N_("show resolve-undo information")), |
| 636 | OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"), |
| 637 | N_("skip files matching pattern"), |
| 638 | PARSE_OPT_NONEG, option_parse_exclude), |
| 639 | OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"), |
| 640 | N_("read exclude patterns from <file>"), |
| 641 | PARSE_OPT_NONEG, option_parse_exclude_from), |
| 642 | OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"), |
| 643 | N_("read additional per-directory exclude patterns in <file>")), |
| 644 | OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL, |
| 645 | N_("add the standard git exclusions"), |
| 646 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, |
| 647 | option_parse_exclude_standard), |
| 648 | OPT_SET_INT_F(0, "full-name", &prefix_len, |
| 649 | N_("make the output relative to the project top directory"), |
| 650 | 0, PARSE_OPT_NONEG), |
| 651 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules, |
| 652 | N_("recurse through submodules")), |
| 653 | OPT_BOOL(0, "error-unmatch", &error_unmatch, |
| 654 | N_("if any <file> is not in the index, treat this as an error")), |
| 655 | OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"), |
| 656 | N_("pretend that paths removed since <tree-ish> are still present")), |
| 657 | OPT__ABBREV(&abbrev), |
| 658 | OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")), |
| 659 | OPT_BOOL(0, "deduplicate", &skipping_duplicates, |
| 660 | N_("suppress duplicate entries")), |
| 661 | OPT_BOOL(0, "sparse", &show_sparse_dirs, |
| 662 | N_("show sparse directories in the presence of a sparse index")), |
| 663 | OPT_STRING_F(0, "format", &format, N_("format"), |
| 664 | N_("format to use for the output"), |
| 665 | PARSE_OPT_NONEG), |
| 666 | OPT_END() |
| 667 | }; |
| 668 | int ret = 0; |
| 669 | |
| 670 | show_usage_with_options_if_asked(argc, argv, |
| 671 | ls_files_usage, builtin_ls_files_options); |
| 672 | |
| 673 | prepare_repo_settings(repo); |
| 674 | repo->settings.command_requires_full_index = 0; |
| 675 | |
| 676 | prefix = cmd_prefix; |
| 677 | if (prefix) |
| 678 | prefix_len = strlen(prefix); |
| 679 | repo_config(repo, git_default_config, NULL); |
| 680 | |
| 681 | if (repo_read_index(repo) < 0) |
| 682 | die("index file corrupt"); |
| 683 | |
| 684 | argc = parse_options(argc, argv, prefix, builtin_ls_files_options, |
| 685 | ls_files_usage, 0); |
| 686 | pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option"); |
| 687 | for (i = 0; i < exclude_list.nr; i++) { |
| 688 | add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args); |
| 689 | } |
| 690 | |
| 691 | if (format && (show_stage || show_others || show_killed || |
| 692 | show_resolve_undo || skipping_duplicates || show_eol || show_tag)) |
| 693 | usage_msg_opt(_("--format cannot be used with -s, -o, -k, -t, " |
| 694 | "--resolve-undo, --deduplicate, --eol"), |
| 695 | ls_files_usage, builtin_ls_files_options); |
| 696 | |
| 697 | if (show_tag || show_valid_bit || show_fsmonitor_bit) { |
| 698 | tag_cached = "H "; |
| 699 | tag_unmerged = "M "; |
| 700 | tag_removed = "R "; |
| 701 | tag_modified = "C "; |
| 702 | tag_other = "? "; |
| 703 | tag_killed = "K "; |
| 704 | tag_skip_worktree = "S "; |
| 705 | tag_resolve_undo = "U "; |
| 706 | } |
| 707 | if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed) |
| 708 | require_work_tree = 1; |
| 709 | if (show_unmerged) |
| 710 | /* |
| 711 | * There's no point in showing unmerged unless |
| 712 | * you also show the stage information. |
| 713 | */ |
| 714 | show_stage = 1; |
| 715 | if (show_tag || show_stage) |
| 716 | skipping_duplicates = 0; |
| 717 | if (dir.exclude_per_dir) |
| 718 | exc_given = 1; |
| 719 | |
| 720 | if (require_work_tree && !is_inside_work_tree(repo)) |
| 721 | setup_work_tree(repo); |
| 722 | |
| 723 | if (recurse_submodules && |
| 724 | (show_deleted || show_others || show_unmerged || |
| 725 | show_killed || show_modified || show_resolve_undo || with_tree)) |
| 726 | die("ls-files --recurse-submodules unsupported mode"); |
| 727 | |
| 728 | if (recurse_submodules && error_unmatch) |
| 729 | die("ls-files --recurse-submodules does not support " |
| 730 | "--error-unmatch"); |
| 731 | |
| 732 | parse_pathspec(&pathspec, 0, |
| 733 | PATHSPEC_PREFER_CWD, |
| 734 | prefix, argv); |
| 735 | |
| 736 | /* |
| 737 | * Find common prefix for all pathspec's |
| 738 | * This is used as a performance optimization which unfortunately cannot |
| 739 | * be done when recursing into submodules because when a pathspec is |
| 740 | * given which spans repository boundaries you can't simply remove the |
| 741 | * submodule entry because the pathspec may match something inside the |
| 742 | * submodule. |
| 743 | */ |
| 744 | if (recurse_submodules) |
| 745 | max_prefix = NULL; |
| 746 | else |
| 747 | max_prefix = common_prefix(&pathspec); |
| 748 | max_prefix_len = get_common_prefix_len(max_prefix); |
| 749 | |
| 750 | prune_index(repo->index, max_prefix, max_prefix_len); |
| 751 | |
| 752 | /* Treat unmatching pathspec elements as errors */ |
| 753 | if (pathspec.nr && error_unmatch) |
| 754 | ps_matched = xcalloc(pathspec.nr, 1); |
| 755 | |
| 756 | if ((dir.flags & DIR_SHOW_IGNORED) && !show_others && !show_cached) |
| 757 | die("ls-files -i must be used with either -o or -c"); |
| 758 | |
| 759 | if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given) |
| 760 | die("ls-files --ignored needs some exclude pattern"); |
| 761 | |
| 762 | /* With no flags, we default to showing the cached files */ |
| 763 | if (!(show_stage || show_deleted || show_others || show_unmerged || |
| 764 | show_killed || show_modified || show_resolve_undo)) |
| 765 | show_cached = 1; |
| 766 | |
| 767 | if (with_tree) { |
| 768 | /* |
| 769 | * Basic sanity check; show-stages and show-unmerged |
| 770 | * would not make any sense with this option. |
| 771 | */ |
| 772 | if (show_stage || show_unmerged) |
| 773 | die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u"); |
| 774 | overlay_tree_on_index(repo->index, with_tree, max_prefix); |
| 775 | } |
| 776 | |
| 777 | show_files(repo, &dir); |
| 778 | |
| 779 | if (show_resolve_undo) |
| 780 | show_ru_info(repo, repo->index); |
| 781 | |
| 782 | if (ps_matched && report_path_error(ps_matched, &pathspec)) { |
| 783 | fprintf(stderr, "Did you forget to 'git add'?\n"); |
| 784 | ret = 1; |
| 785 | } |
| 786 | |
| 787 | string_list_clear(&exclude_list, 0); |
| 788 | dir_clear(&dir); |
| 789 | free(max_prefix); |
| 790 | return ret; |
| 791 | } |