Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 8 | #include "cache.h" |
Junio C Hamano | 22ddf71 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 9 | #include "quote.h" |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 10 | #include "dir.h" |
Peter Eriksen | 0864f26 | 2006-05-23 14:15:29 +0200 | [diff] [blame] | 11 | #include "builtin.h" |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 12 | #include "tree.h" |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 13 | #include "parse-options.h" |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 14 | #include "resolve-undo.h" |
| 15 | #include "string-list.h" |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 16 | #include "pathspec.h" |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 17 | #include "run-command.h" |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 18 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 19 | static int abbrev; |
| 20 | static int show_deleted; |
| 21 | static int show_cached; |
| 22 | static int show_others; |
| 23 | static int show_stage; |
| 24 | static int show_unmerged; |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 25 | static int show_resolve_undo; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 26 | static int show_modified; |
| 27 | static int show_killed; |
| 28 | static int show_valid_bit; |
Junio C Hamano | b83c834 | 2005-04-15 11:11:01 -0700 | [diff] [blame] | 29 | static int line_terminator = '\n'; |
Thomas Rast | 8497421 | 2010-07-31 00:35:59 +0200 | [diff] [blame] | 30 | static int debug_mode; |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 31 | static int show_eol; |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 32 | static int recurse_submodules; |
Brandon Williams | 07c01b9 | 2016-10-07 11:18:50 -0700 | [diff] [blame] | 33 | static struct argv_array submodules_options = ARGV_ARRAY_INIT; |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 34 | |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 35 | static const char *prefix; |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 36 | static const char *super_prefix; |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 37 | static int max_prefix_len; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 38 | static int prefix_len; |
Nguyễn Thái Ngọc Duy | 9e06d6e | 2013-07-14 15:35:43 +0700 | [diff] [blame] | 39 | static struct pathspec pathspec; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 40 | static int error_unmatch; |
| 41 | static char *ps_matched; |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 42 | static const char *with_tree; |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 43 | static int exc_given; |
Adam Spiers | c04318e | 2013-01-06 16:58:04 +0000 | [diff] [blame] | 44 | static int exclude_args; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 45 | |
Petr Baudis | 20d37ef | 2005-04-21 19:47:08 -0700 | [diff] [blame] | 46 | static const char *tag_cached = ""; |
| 47 | static const char *tag_unmerged = ""; |
| 48 | static const char *tag_removed = ""; |
| 49 | static const char *tag_other = ""; |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 50 | static const char *tag_killed = ""; |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 51 | static const char *tag_modified = ""; |
Nguyễn Thái Ngọc Duy | 44a3691 | 2009-08-20 20:46:57 +0700 | [diff] [blame] | 52 | static const char *tag_skip_worktree = ""; |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 53 | static const char *tag_resolve_undo = ""; |
Petr Baudis | 20d37ef | 2005-04-21 19:47:08 -0700 | [diff] [blame] | 54 | |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 55 | static void write_eolinfo(const struct cache_entry *ce, const char *path) |
| 56 | { |
| 57 | if (!show_eol) |
| 58 | return; |
| 59 | else { |
| 60 | struct stat st; |
| 61 | const char *i_txt = ""; |
| 62 | const char *w_txt = ""; |
| 63 | const char *a_txt = get_convert_attr_ascii(path); |
| 64 | if (ce && S_ISREG(ce->ce_mode)) |
| 65 | i_txt = get_cached_convert_stats_ascii(ce->name); |
| 66 | if (!lstat(path, &st) && S_ISREG(st.st_mode)) |
| 67 | w_txt = get_wt_convert_stats_ascii(path); |
| 68 | printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt); |
| 69 | } |
| 70 | } |
| 71 | |
Jiang Xin | e9a820c | 2013-06-25 23:53:46 +0800 | [diff] [blame] | 72 | static void write_name(const char *name) |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 73 | { |
Jiang Xin | ad66df2 | 2013-06-25 23:53:44 +0800 | [diff] [blame] | 74 | /* |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 75 | * Prepend the super_prefix to name to construct the full_name to be |
| 76 | * written. |
| 77 | */ |
| 78 | struct strbuf full_name = STRBUF_INIT; |
| 79 | if (super_prefix) { |
| 80 | strbuf_addstr(&full_name, super_prefix); |
| 81 | strbuf_addstr(&full_name, name); |
| 82 | name = full_name.buf; |
| 83 | } |
| 84 | |
| 85 | /* |
Jiang Xin | e9a820c | 2013-06-25 23:53:46 +0800 | [diff] [blame] | 86 | * With "--full-name", prefix_len=0; this caller needs to pass |
| 87 | * an empty string in that case (a NULL is good for ""). |
Jiang Xin | ad66df2 | 2013-06-25 23:53:44 +0800 | [diff] [blame] | 88 | */ |
Jiang Xin | e9a820c | 2013-06-25 23:53:46 +0800 | [diff] [blame] | 89 | write_name_quoted_relative(name, prefix_len ? prefix : NULL, |
| 90 | stdout, line_terminator); |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 91 | |
| 92 | strbuf_release(&full_name); |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 95 | static void show_dir_entry(const char *tag, struct dir_entry *ent) |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 96 | { |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 97 | int len = max_prefix_len; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 98 | |
| 99 | if (len >= ent->len) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 100 | die("git ls-files: internal error - directory entry not superset of prefix"); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 101 | |
Nguyễn Thái Ngọc Duy | ebb3289 | 2014-01-24 20:40:29 +0700 | [diff] [blame] | 102 | if (!dir_path_match(ent, &pathspec, len, ps_matched)) |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 103 | return; |
| 104 | |
Junio C Hamano | 22ddf71 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 105 | fputs(tag, stdout); |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 106 | write_eolinfo(NULL, ent->name); |
Jiang Xin | e9a820c | 2013-06-25 23:53:46 +0800 | [diff] [blame] | 107 | write_name(ent->name); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 110 | static void show_other_files(struct dir_struct *dir) |
Junio C Hamano | fcbc308 | 2005-11-06 17:26:31 -0800 | [diff] [blame] | 111 | { |
| 112 | int i; |
Linus Torvalds | 5698454 | 2007-04-14 16:22:08 -0700 | [diff] [blame] | 113 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 114 | for (i = 0; i < dir->nr; i++) { |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 115 | struct dir_entry *ent = dir->entries[i]; |
Jeff King | 98fa473 | 2008-10-16 11:07:26 -0400 | [diff] [blame] | 116 | if (!cache_name_is_other(ent->name, ent->len)) |
| 117 | continue; |
Junio C Hamano | fcbc308 | 2005-11-06 17:26:31 -0800 | [diff] [blame] | 118 | show_dir_entry(tag_other, ent); |
| 119 | } |
| 120 | } |
| 121 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 122 | static void show_killed_files(struct dir_struct *dir) |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 123 | { |
| 124 | int i; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 125 | for (i = 0; i < dir->nr; i++) { |
| 126 | struct dir_entry *ent = dir->entries[i]; |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 127 | char *cp, *sp; |
| 128 | int pos, len, killed = 0; |
| 129 | |
| 130 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { |
| 131 | sp = strchr(cp, '/'); |
| 132 | if (!sp) { |
| 133 | /* If ent->name is prefix of an entry in the |
| 134 | * cache, it will be killed. |
| 135 | */ |
| 136 | pos = cache_name_pos(ent->name, ent->len); |
| 137 | if (0 <= pos) |
Johannes Schindelin | ef1177d1 | 2016-07-26 18:05:50 +0200 | [diff] [blame] | 138 | die("BUG: killed-file %.*s not found", |
| 139 | ent->len, ent->name); |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 140 | pos = -pos - 1; |
| 141 | while (pos < active_nr && |
| 142 | ce_stage(active_cache[pos])) |
| 143 | pos++; /* skip unmerged */ |
| 144 | if (active_nr <= pos) |
| 145 | break; |
| 146 | /* pos points at a name immediately after |
| 147 | * ent->name in the cache. Does it expect |
| 148 | * ent->name to be a directory? |
| 149 | */ |
| 150 | len = ce_namelen(active_cache[pos]); |
| 151 | if ((ent->len < len) && |
| 152 | !strncmp(active_cache[pos]->name, |
| 153 | ent->name, ent->len) && |
| 154 | active_cache[pos]->name[ent->len] == '/') |
| 155 | killed = 1; |
| 156 | break; |
| 157 | } |
| 158 | if (0 <= cache_name_pos(ent->name, sp - ent->name)) { |
| 159 | /* If any of the leading directories in |
| 160 | * ent->name is registered in the cache, |
| 161 | * ent->name will be killed. |
| 162 | */ |
| 163 | killed = 1; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (killed) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 168 | show_dir_entry(tag_killed, dir->entries[i]); |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Brandon Williams | 07c01b9 | 2016-10-07 11:18:50 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Compile an argv_array with all of the options supported by --recurse_submodules |
| 174 | */ |
| 175 | static void compile_submodule_options(const struct dir_struct *dir, int show_tag) |
| 176 | { |
| 177 | if (line_terminator == '\0') |
| 178 | argv_array_push(&submodules_options, "-z"); |
| 179 | if (show_tag) |
| 180 | argv_array_push(&submodules_options, "-t"); |
| 181 | if (show_valid_bit) |
| 182 | argv_array_push(&submodules_options, "-v"); |
| 183 | if (show_cached) |
| 184 | argv_array_push(&submodules_options, "--cached"); |
| 185 | if (show_eol) |
| 186 | argv_array_push(&submodules_options, "--eol"); |
| 187 | if (debug_mode) |
| 188 | argv_array_push(&submodules_options, "--debug"); |
| 189 | } |
| 190 | |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Recursively call ls-files on a submodule |
| 193 | */ |
| 194 | static void show_gitlink(const struct cache_entry *ce) |
| 195 | { |
| 196 | struct child_process cp = CHILD_PROCESS_INIT; |
| 197 | int status; |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 198 | int i; |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 199 | |
| 200 | argv_array_pushf(&cp.args, "--super-prefix=%s%s/", |
| 201 | super_prefix ? super_prefix : "", |
| 202 | ce->name); |
| 203 | argv_array_push(&cp.args, "ls-files"); |
| 204 | argv_array_push(&cp.args, "--recurse-submodules"); |
| 205 | |
Brandon Williams | 07c01b9 | 2016-10-07 11:18:50 -0700 | [diff] [blame] | 206 | /* add supported options */ |
| 207 | argv_array_pushv(&cp.args, submodules_options.argv); |
| 208 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Pass in the original pathspec args. The submodule will be |
| 211 | * responsible for prepending the 'submodule_prefix' prior to comparing |
| 212 | * against the pathspec for matches. |
| 213 | */ |
| 214 | argv_array_push(&cp.args, "--"); |
| 215 | for (i = 0; i < pathspec.nr; i++) |
| 216 | argv_array_push(&cp.args, pathspec.items[i].original); |
| 217 | |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 218 | cp.git_cmd = 1; |
| 219 | cp.dir = ce->name; |
| 220 | status = run_command(&cp); |
| 221 | if (status) |
| 222 | exit(status); |
| 223 | } |
| 224 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 225 | static void show_ce_entry(const char *tag, const struct cache_entry *ce) |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 226 | { |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 227 | struct strbuf name = STRBUF_INIT; |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 228 | int len = max_prefix_len; |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 229 | if (super_prefix) |
| 230 | strbuf_addstr(&name, super_prefix); |
| 231 | strbuf_addstr(&name, ce->name); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 232 | |
| 233 | if (len >= ce_namelen(ce)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 234 | die("git ls-files: internal error - cache entry not superset of prefix"); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 235 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 236 | if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && |
| 237 | submodule_path_match(&pathspec, name.buf, ps_matched)) { |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 238 | show_gitlink(ce); |
| 239 | } else if (match_pathspec(&pathspec, name.buf, name.len, |
| 240 | len, ps_matched, |
| 241 | S_ISDIR(ce->ce_mode) || |
| 242 | S_ISGITLINK(ce->ce_mode))) { |
| 243 | if (tag && *tag && show_valid_bit && |
| 244 | (ce->ce_flags & CE_VALID)) { |
| 245 | static char alttag[4]; |
| 246 | memcpy(alttag, tag, 3); |
| 247 | if (isalpha(tag[0])) |
| 248 | alttag[0] = tolower(tag[0]); |
| 249 | else if (tag[0] == '?') |
| 250 | alttag[0] = '!'; |
| 251 | else { |
| 252 | alttag[0] = 'v'; |
| 253 | alttag[1] = tag[0]; |
| 254 | alttag[2] = ' '; |
| 255 | alttag[3] = 0; |
| 256 | } |
| 257 | tag = alttag; |
Junio C Hamano | 2bcab24 | 2006-02-08 21:50:18 -0800 | [diff] [blame] | 258 | } |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 259 | |
| 260 | if (!show_stage) { |
| 261 | fputs(tag, stdout); |
| 262 | } else { |
| 263 | printf("%s%06o %s %d\t", |
| 264 | tag, |
| 265 | ce->ce_mode, |
Junio C Hamano | 1c2b1f7 | 2016-10-26 13:14:44 -0700 | [diff] [blame] | 266 | find_unique_abbrev(ce->oid.hash, abbrev), |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 267 | ce_stage(ce)); |
| 268 | } |
| 269 | write_eolinfo(ce, ce->name); |
| 270 | write_name(ce->name); |
| 271 | if (debug_mode) { |
| 272 | const struct stat_data *sd = &ce->ce_stat_data; |
| 273 | |
| 274 | printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec); |
| 275 | printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec); |
| 276 | printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino); |
| 277 | printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid); |
| 278 | printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags); |
| 279 | } |
Junio C Hamano | 2bcab24 | 2006-02-08 21:50:18 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 282 | strbuf_release(&name); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 285 | static void show_ru_info(void) |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 286 | { |
Alex Riesen | 8a57c6e | 2010-07-03 14:41:54 +0200 | [diff] [blame] | 287 | struct string_list_item *item; |
| 288 | |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 289 | if (!the_index.resolve_undo) |
| 290 | return; |
Alex Riesen | 8a57c6e | 2010-07-03 14:41:54 +0200 | [diff] [blame] | 291 | |
| 292 | for_each_string_list_item(item, the_index.resolve_undo) { |
| 293 | const char *path = item->string; |
| 294 | struct resolve_undo_info *ui = item->util; |
| 295 | int i, len; |
| 296 | |
| 297 | len = strlen(path); |
| 298 | if (len < max_prefix_len) |
| 299 | continue; /* outside of the prefix */ |
Nguyễn Thái Ngọc Duy | 854b095 | 2014-01-24 20:40:30 +0700 | [diff] [blame] | 300 | if (!match_pathspec(&pathspec, path, len, |
Nguyễn Thái Ngọc Duy | ae8d082 | 2014-01-24 20:40:33 +0700 | [diff] [blame] | 301 | max_prefix_len, ps_matched, 0)) |
Alex Riesen | 8a57c6e | 2010-07-03 14:41:54 +0200 | [diff] [blame] | 302 | continue; /* uninterested */ |
| 303 | for (i = 0; i < 3; i++) { |
| 304 | if (!ui->mode[i]) |
| 305 | continue; |
| 306 | printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], |
| 307 | find_unique_abbrev(ui->sha1[i], abbrev), |
| 308 | i + 1); |
Jiang Xin | e9a820c | 2013-06-25 23:53:46 +0800 | [diff] [blame] | 309 | write_name(path); |
Alex Riesen | 8a57c6e | 2010-07-03 14:41:54 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 314 | static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce) |
Junio C Hamano | 782cd4c | 2012-06-05 21:17:52 -0700 | [diff] [blame] | 315 | { |
| 316 | int dtype = ce_to_dtype(ce); |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 317 | return is_excluded(dir, ce->name, &dtype); |
Junio C Hamano | 782cd4c | 2012-06-05 21:17:52 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 320 | static void show_files(struct dir_struct *dir) |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 321 | { |
| 322 | int i; |
| 323 | |
| 324 | /* For cached/deleted files we don't need to even do the readdir */ |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 325 | if (show_others || show_killed) { |
Junio C Hamano | 2eac2a4 | 2013-08-15 12:13:46 -0700 | [diff] [blame] | 326 | if (!show_others) |
| 327 | dir->flags |= DIR_COLLECT_KILLED_ONLY; |
Nguyễn Thái Ngọc Duy | 7327d3d | 2013-07-14 15:35:55 +0700 | [diff] [blame] | 328 | fill_directory(dir, &pathspec); |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 329 | if (show_others) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 330 | show_other_files(dir); |
Junio C Hamano | 6ca4594 | 2005-05-12 17:17:54 -0700 | [diff] [blame] | 331 | if (show_killed) |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 332 | show_killed_files(dir); |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 333 | } |
René Scharfe | 0b437a1 | 2013-06-13 20:19:44 +0200 | [diff] [blame] | 334 | if (show_cached || show_stage) { |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 335 | for (i = 0; i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 336 | const struct cache_entry *ce = active_cache[i]; |
Junio C Hamano | eb41775 | 2012-06-01 11:28:00 -0700 | [diff] [blame] | 337 | if ((dir->flags & DIR_SHOW_IGNORED) && |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 338 | !ce_excluded(dir, ce)) |
Jeff King | 500348a | 2009-10-30 15:05:52 -0400 | [diff] [blame] | 339 | continue; |
Linus Torvalds | eec8c63 | 2005-04-16 12:43:32 -0700 | [diff] [blame] | 340 | if (show_unmerged && !ce_stage(ce)) |
| 341 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 342 | if (ce->ce_flags & CE_UPDATE) |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 343 | continue; |
Nguyễn Thái Ngọc Duy | 44a3691 | 2009-08-20 20:46:57 +0700 | [diff] [blame] | 344 | show_ce_entry(ce_stage(ce) ? tag_unmerged : |
| 345 | (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce); |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
René Scharfe | 0b437a1 | 2013-06-13 20:19:44 +0200 | [diff] [blame] | 348 | if (show_deleted || show_modified) { |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 349 | for (i = 0; i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 350 | const struct cache_entry *ce = active_cache[i]; |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 351 | struct stat st; |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 352 | int err; |
Junio C Hamano | eb41775 | 2012-06-01 11:28:00 -0700 | [diff] [blame] | 353 | if ((dir->flags & DIR_SHOW_IGNORED) && |
Karsten Blees | b07bc8c | 2013-04-15 21:12:57 +0200 | [diff] [blame] | 354 | !ce_excluded(dir, ce)) |
Jeff King | 500348a | 2009-10-30 15:05:52 -0400 | [diff] [blame] | 355 | continue; |
Junio C Hamano | 4b4e26d | 2008-11-16 00:10:25 -0800 | [diff] [blame] | 356 | if (ce->ce_flags & CE_UPDATE) |
| 357 | continue; |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 358 | if (ce_skip_worktree(ce)) |
| 359 | continue; |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 360 | err = lstat(ce->name, &st); |
| 361 | if (show_deleted && err) |
| 362 | show_ce_entry(tag_removed, ce); |
Junio C Hamano | 2bcab24 | 2006-02-08 21:50:18 -0800 | [diff] [blame] | 363 | if (show_modified && ce_modified(ce, &st, 0)) |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 364 | show_ce_entry(tag_modified, ce); |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Prune the index to only contain stuff starting with "prefix" |
| 371 | */ |
René Scharfe | 7b4158a | 2017-02-10 20:42:28 +0100 | [diff] [blame] | 372 | static void prune_cache(const char *prefix, size_t prefixlen) |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 373 | { |
René Scharfe | 7b4158a | 2017-02-10 20:42:28 +0100 | [diff] [blame] | 374 | int pos; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 375 | unsigned int first, last; |
| 376 | |
René Scharfe | 7b4158a | 2017-02-10 20:42:28 +0100 | [diff] [blame] | 377 | if (!prefix) |
| 378 | return; |
| 379 | pos = cache_name_pos(prefix, prefixlen); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 380 | if (pos < 0) |
| 381 | pos = -pos-1; |
René Scharfe | 96f6d3f | 2017-02-10 21:03:30 +0100 | [diff] [blame] | 382 | first = pos; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 383 | last = active_nr; |
| 384 | while (last > first) { |
| 385 | int next = (last + first) >> 1; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 386 | const struct cache_entry *ce = active_cache[next]; |
René Scharfe | 7b4158a | 2017-02-10 20:42:28 +0100 | [diff] [blame] | 387 | if (!strncmp(ce->name, prefix, prefixlen)) { |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 388 | first = next+1; |
| 389 | continue; |
| 390 | } |
| 391 | last = next; |
| 392 | } |
René Scharfe | 96f6d3f | 2017-02-10 21:03:30 +0100 | [diff] [blame] | 393 | memmove(active_cache, active_cache + pos, |
| 394 | (last - pos) * sizeof(struct cache_entry *)); |
| 395 | active_nr = last - pos; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 398 | /* |
| 399 | * Read the tree specified with --with-tree option |
| 400 | * (typically, HEAD) into stage #1 and then |
| 401 | * squash them down to stage #0. This is used for |
| 402 | * --error-unmatch to list and check the path patterns |
| 403 | * that were given from the command line. We are not |
| 404 | * going to write this index out. |
| 405 | */ |
Junio C Hamano | ee425e4 | 2007-11-18 01:13:32 -0800 | [diff] [blame] | 406 | void overlay_tree_on_cache(const char *tree_name, const char *prefix) |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 407 | { |
| 408 | struct tree *tree; |
| 409 | unsigned char sha1[20]; |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 410 | struct pathspec pathspec; |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 411 | struct cache_entry *last_stage0 = NULL; |
| 412 | int i; |
| 413 | |
| 414 | if (get_sha1(tree_name, sha1)) |
| 415 | die("tree-ish %s not found.", tree_name); |
| 416 | tree = parse_tree_indirect(sha1); |
| 417 | if (!tree) |
| 418 | die("bad tree-ish %s", tree_name); |
| 419 | |
| 420 | /* Hoist the unmerged entries up to stage #3 to make room */ |
| 421 | for (i = 0; i < active_nr; i++) { |
| 422 | struct cache_entry *ce = active_cache[i]; |
| 423 | if (!ce_stage(ce)) |
| 424 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 425 | ce->ce_flags |= CE_STAGEMASK; |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (prefix) { |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 429 | static const char *(matchbuf[1]); |
| 430 | matchbuf[0] = NULL; |
| 431 | parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC, |
| 432 | PATHSPEC_PREFER_CWD, prefix, matchbuf); |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 433 | } else |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 434 | memset(&pathspec, 0, sizeof(pathspec)); |
Nguyễn Thái Ngọc Duy | f0096c0 | 2011-03-25 16:34:19 +0700 | [diff] [blame] | 435 | if (read_tree(tree, 1, &pathspec)) |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 436 | die("unable to read tree entries %s", tree_name); |
| 437 | |
| 438 | for (i = 0; i < active_nr; i++) { |
| 439 | struct cache_entry *ce = active_cache[i]; |
| 440 | switch (ce_stage(ce)) { |
| 441 | case 0: |
| 442 | last_stage0 = ce; |
| 443 | /* fallthru */ |
| 444 | default: |
| 445 | continue; |
| 446 | case 1: |
| 447 | /* |
| 448 | * If there is stage #0 entry for this, we do not |
| 449 | * need to show it. We use CE_UPDATE bit to mark |
| 450 | * such an entry. |
| 451 | */ |
| 452 | if (last_stage0 && |
| 453 | !strcmp(last_stage0->name, ce->name)) |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 454 | ce->ce_flags |= CE_UPDATE; |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 459 | static const char * const ls_files_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 460 | N_("git ls-files [<options>] [<file>...]"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 461 | NULL |
| 462 | }; |
| 463 | |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 464 | static int option_parse_exclude(const struct option *opt, |
| 465 | const char *arg, int unset) |
| 466 | { |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 467 | struct string_list *exclude_list = opt->value; |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 468 | |
| 469 | exc_given = 1; |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 470 | string_list_append(exclude_list, arg); |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int option_parse_exclude_from(const struct option *opt, |
| 476 | const char *arg, int unset) |
| 477 | { |
| 478 | struct dir_struct *dir = opt->value; |
| 479 | |
| 480 | exc_given = 1; |
| 481 | add_excludes_from_file(dir, arg); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int option_parse_exclude_standard(const struct option *opt, |
| 487 | const char *arg, int unset) |
| 488 | { |
| 489 | struct dir_struct *dir = opt->value; |
| 490 | |
| 491 | exc_given = 1; |
| 492 | setup_standard_excludes(dir); |
| 493 | |
| 494 | return 0; |
| 495 | } |
Nicolas Pitre | cf9a113 | 2005-04-28 15:06:25 -0700 | [diff] [blame] | 496 | |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 497 | int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 498 | { |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 499 | int require_work_tree = 0, show_tag = 0, i; |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 500 | const char *max_prefix; |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 501 | struct dir_struct dir; |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 502 | struct exclude_list *el; |
| 503 | struct string_list exclude_list = STRING_LIST_INIT_NODUP; |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 504 | struct option builtin_ls_files_options[] = { |
Jeff King | 1f3c79a | 2016-01-31 06:35:46 -0500 | [diff] [blame] | 505 | /* Think twice before adding "--nul" synonym to this */ |
| 506 | OPT_SET_INT('z', NULL, &line_terminator, |
| 507 | N_("paths are separated with NUL character"), '\0'), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 508 | OPT_BOOL('t', NULL, &show_tag, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 509 | N_("identify the file status with tags")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 510 | OPT_BOOL('v', NULL, &show_valid_bit, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 511 | N_("use lowercase letters for 'assume unchanged' files")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 512 | OPT_BOOL('c', "cached", &show_cached, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 513 | N_("show cached files in the output (default)")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 514 | OPT_BOOL('d', "deleted", &show_deleted, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 515 | N_("show deleted files in the output")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 516 | OPT_BOOL('m', "modified", &show_modified, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 517 | N_("show modified files in the output")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 518 | OPT_BOOL('o', "others", &show_others, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 519 | N_("show other files in the output")), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 520 | OPT_BIT('i', "ignored", &dir.flags, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 521 | N_("show ignored files in the output"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 522 | DIR_SHOW_IGNORED), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 523 | OPT_BOOL('s', "stage", &show_stage, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 524 | N_("show staged contents' object name in the output")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 525 | OPT_BOOL('k', "killed", &show_killed, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 526 | N_("show files on the filesystem that need to be removed")), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 527 | OPT_BIT(0, "directory", &dir.flags, |
Alex Henrie | ad5fe37 | 2014-08-30 13:56:01 -0600 | [diff] [blame] | 528 | N_("show 'other' directories' names only"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 529 | DIR_SHOW_OTHER_DIRECTORIES), |
Torsten Bögershausen | a7630bd | 2016-01-16 07:50:02 +0100 | [diff] [blame] | 530 | OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")), |
Jeff King | e9008b9 | 2009-05-08 01:01:17 -0400 | [diff] [blame] | 531 | OPT_NEGBIT(0, "empty-directory", &dir.flags, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 532 | N_("don't show empty directories"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 533 | DIR_HIDE_EMPTY_DIRECTORIES), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 534 | OPT_BOOL('u', "unmerged", &show_unmerged, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 535 | N_("show unmerged files in the output")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 536 | OPT_BOOL(0, "resolve-undo", &show_resolve_undo, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 537 | N_("show resolve-undo information")), |
Junio C Hamano | a39b15b | 2013-01-23 21:19:10 -0800 | [diff] [blame] | 538 | { OPTION_CALLBACK, 'x', "exclude", &exclude_list, N_("pattern"), |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 539 | N_("skip files matching pattern"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 540 | 0, option_parse_exclude }, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 541 | { OPTION_CALLBACK, 'X', "exclude-from", &dir, N_("file"), |
| 542 | N_("exclude patterns are read from <file>"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 543 | 0, option_parse_exclude_from }, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 544 | OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"), |
| 545 | N_("read additional per-directory exclude patterns in <file>")), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 546 | { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 547 | N_("add the standard git exclusions"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 548 | PARSE_OPT_NOARG, option_parse_exclude_standard }, |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 549 | { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 550 | N_("make the output relative to the project top directory"), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 551 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL }, |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 552 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules, |
| 553 | N_("recurse through submodules")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 554 | OPT_BOOL(0, "error-unmatch", &error_unmatch, |
Nguyễn Thái Ngọc Duy | 377adc3 | 2012-08-20 19:32:20 +0700 | [diff] [blame] | 555 | N_("if any <file> is not in the index, treat this as an error")), |
| 556 | OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"), |
| 557 | N_("pretend that paths removed since <tree-ish> are still present")), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 558 | OPT__ABBREV(&abbrev), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 559 | OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")), |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 560 | OPT_END() |
| 561 | }; |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 562 | |
Nguyễn Thái Ngọc Duy | cbb3167 | 2010-10-22 01:48:14 -0500 | [diff] [blame] | 563 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 564 | usage_with_options(ls_files_usage, builtin_ls_files_options); |
| 565 | |
Linus Torvalds | 453ec4b | 2006-05-16 19:02:14 -0700 | [diff] [blame] | 566 | memset(&dir, 0, sizeof(dir)); |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 567 | prefix = cmd_prefix; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 568 | if (prefix) |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 569 | prefix_len = strlen(prefix); |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 570 | super_prefix = get_super_prefix(); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 571 | git_config(git_default_config, NULL); |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 572 | |
Nguyễn Thái Ngọc Duy | c28b3d6 | 2009-08-20 20:47:01 +0700 | [diff] [blame] | 573 | if (read_cache() < 0) |
| 574 | die("index file corrupt"); |
| 575 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 576 | argc = parse_options(argc, argv, prefix, builtin_ls_files_options, |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 577 | ls_files_usage, 0); |
Adam Spiers | 72aeb18 | 2013-01-16 13:25:58 +0000 | [diff] [blame] | 578 | el = add_exclude_list(&dir, EXC_CMDL, "--exclude option"); |
| 579 | for (i = 0; i < exclude_list.nr; i++) { |
| 580 | add_exclude(exclude_list.items[i].string, "", 0, el, --exclude_args); |
| 581 | } |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 582 | if (show_tag || show_valid_bit) { |
| 583 | tag_cached = "H "; |
| 584 | tag_unmerged = "M "; |
| 585 | tag_removed = "R "; |
| 586 | tag_modified = "C "; |
| 587 | tag_other = "? "; |
| 588 | tag_killed = "K "; |
Nguyễn Thái Ngọc Duy | 44a3691 | 2009-08-20 20:46:57 +0700 | [diff] [blame] | 589 | tag_skip_worktree = "S "; |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 590 | tag_resolve_undo = "U "; |
Nicolas Pitre | 9ff768e | 2005-04-28 11:44:04 -0700 | [diff] [blame] | 591 | } |
Junio C Hamano | de2e3b0 | 2009-03-20 14:30:51 -0700 | [diff] [blame] | 592 | if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed) |
Miklos Vajna | ce8e880 | 2009-02-17 15:27:11 +0100 | [diff] [blame] | 593 | require_work_tree = 1; |
| 594 | if (show_unmerged) |
| 595 | /* |
| 596 | * There's no point in showing unmerged unless |
| 597 | * you also show the stage information. |
| 598 | */ |
| 599 | show_stage = 1; |
| 600 | if (dir.exclude_per_dir) |
| 601 | exc_given = 1; |
Nicolas Pitre | 9ff768e | 2005-04-28 11:44:04 -0700 | [diff] [blame] | 602 | |
Mike Hommey | 7d8ae93 | 2007-11-03 12:23:12 +0100 | [diff] [blame] | 603 | if (require_work_tree && !is_inside_work_tree()) |
| 604 | setup_work_tree(); |
Johannes Schindelin | 6d9ba67 | 2007-01-23 13:30:20 +0100 | [diff] [blame] | 605 | |
Brandon Williams | 07c01b9 | 2016-10-07 11:18:50 -0700 | [diff] [blame] | 606 | if (recurse_submodules) |
| 607 | compile_submodule_options(&dir, show_tag); |
| 608 | |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 609 | if (recurse_submodules && |
| 610 | (show_stage || show_deleted || show_others || show_unmerged || |
Brandon Williams | 07c01b9 | 2016-10-07 11:18:50 -0700 | [diff] [blame] | 611 | show_killed || show_modified || show_resolve_undo || with_tree)) |
Brandon Williams | e77aa33 | 2016-10-07 11:18:49 -0700 | [diff] [blame] | 612 | die("ls-files --recurse-submodules unsupported mode"); |
| 613 | |
| 614 | if (recurse_submodules && error_unmatch) |
| 615 | die("ls-files --recurse-submodules does not support " |
| 616 | "--error-unmatch"); |
| 617 | |
Nguyễn Thái Ngọc Duy | 9e06d6e | 2013-07-14 15:35:43 +0700 | [diff] [blame] | 618 | parse_pathspec(&pathspec, 0, |
| 619 | PATHSPEC_PREFER_CWD | |
| 620 | PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP, |
| 621 | prefix, argv); |
Johannes Schindelin | f3670a5 | 2009-02-07 14:43:03 +0100 | [diff] [blame] | 622 | |
Brandon Williams | 75a6315 | 2016-10-07 11:18:51 -0700 | [diff] [blame] | 623 | /* |
| 624 | * Find common prefix for all pathspec's |
| 625 | * This is used as a performance optimization which unfortunately cannot |
| 626 | * be done when recursing into submodules |
| 627 | */ |
| 628 | if (recurse_submodules) |
| 629 | max_prefix = NULL; |
| 630 | else |
| 631 | max_prefix = common_prefix(&pathspec); |
Clemens Buchacher | 8894d53 | 2011-07-30 19:13:47 +0200 | [diff] [blame] | 632 | max_prefix_len = max_prefix ? strlen(max_prefix) : 0; |
Linus Torvalds | 5be4efb | 2005-08-21 12:55:33 -0700 | [diff] [blame] | 633 | |
Junio C Hamano | bba319b | 2006-02-14 12:40:20 -0800 | [diff] [blame] | 634 | /* Treat unmatching pathspec elements as errors */ |
Nguyễn Thái Ngọc Duy | 9e06d6e | 2013-07-14 15:35:43 +0700 | [diff] [blame] | 635 | if (pathspec.nr && error_unmatch) |
Junio C Hamano | 8b54c23 | 2015-08-20 09:57:32 -0700 | [diff] [blame] | 636 | ps_matched = xcalloc(pathspec.nr, 1); |
Junio C Hamano | bba319b | 2006-02-14 12:40:20 -0800 | [diff] [blame] | 637 | |
Ben Walton | ac78b00 | 2009-10-08 21:53:35 -0400 | [diff] [blame] | 638 | if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given) |
| 639 | die("ls-files --ignored needs some exclude pattern"); |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 640 | |
| 641 | /* With no flags, we default to showing the cached files */ |
René Scharfe | 0b437a1 | 2013-06-13 20:19:44 +0200 | [diff] [blame] | 642 | if (!(show_stage || show_deleted || show_others || show_unmerged || |
| 643 | show_killed || show_modified || show_resolve_undo)) |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 644 | show_cached = 1; |
| 645 | |
René Scharfe | 7b4158a | 2017-02-10 20:42:28 +0100 | [diff] [blame] | 646 | prune_cache(max_prefix, max_prefix_len); |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 647 | if (with_tree) { |
| 648 | /* |
| 649 | * Basic sanity check; show-stages and show-unmerged |
| 650 | * would not make any sense with this option. |
| 651 | */ |
| 652 | if (show_stage || show_unmerged) |
| 653 | die("ls-files --with-tree is incompatible with -s or -u"); |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 654 | overlay_tree_on_cache(with_tree, max_prefix); |
Junio C Hamano | 64586e7 | 2007-09-12 16:04:22 -0700 | [diff] [blame] | 655 | } |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 656 | show_files(&dir); |
Junio C Hamano | 9d9a2f4 | 2009-12-25 10:08:04 -0800 | [diff] [blame] | 657 | if (show_resolve_undo) |
Clemens Buchacher | efad1a5 | 2010-06-03 15:39:18 +0200 | [diff] [blame] | 658 | show_ru_info(); |
Junio C Hamano | bba319b | 2006-02-14 12:40:20 -0800 | [diff] [blame] | 659 | |
| 660 | if (ps_matched) { |
Junio C Hamano | ee425e4 | 2007-11-18 01:13:32 -0800 | [diff] [blame] | 661 | int bad; |
Nguyễn Thái Ngọc Duy | 17ddc66 | 2013-07-14 15:35:53 +0700 | [diff] [blame] | 662 | bad = report_path_error(ps_matched, &pathspec, prefix); |
Junio C Hamano | ee425e4 | 2007-11-18 01:13:32 -0800 | [diff] [blame] | 663 | if (bad) |
Andreas Ericsson | ced7b82 | 2006-11-30 12:28:28 +0100 | [diff] [blame] | 664 | fprintf(stderr, "Did you forget to 'git add'?\n"); |
| 665 | |
Junio C Hamano | ee425e4 | 2007-11-18 01:13:32 -0800 | [diff] [blame] | 666 | return bad ? 1 : 0; |
Junio C Hamano | bba319b | 2006-02-14 12:40:20 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Linus Torvalds | 8695c8b | 2005-04-11 18:55:38 -0700 | [diff] [blame] | 669 | return 0; |
| 670 | } |