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