]>
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 | struct show_index_data { |
268 | const char *pathname; | |
269 | struct index_state *istate; | |
270 | const struct cache_entry *ce; | |
271 | }; | |
272 | ||
273 | static size_t expand_show_index(struct strbuf *sb, const char *start, | |
274 | void *context) | |
275 | { | |
276 | struct show_index_data *data = context; | |
277 | const char *end; | |
278 | const char *p; | |
279 | size_t len = strbuf_expand_literal_cb(sb, start, NULL); | |
280 | struct stat st; | |
281 | ||
282 | if (len) | |
283 | return len; | |
284 | if (*start != '(') | |
285 | die(_("bad ls-files format: element '%s' " | |
286 | "does not start with '('"), start); | |
287 | ||
288 | end = strchr(start + 1, ')'); | |
289 | if (!end) | |
746aae3d | 290 | die(_("bad ls-files format: element '%s' " |
ce74de93 ZH |
291 | "does not end in ')'"), start); |
292 | ||
293 | len = end - start + 1; | |
294 | if (skip_prefix(start, "(objectmode)", &p)) | |
295 | strbuf_addf(sb, "%06o", data->ce->ce_mode); | |
296 | else if (skip_prefix(start, "(objectname)", &p)) | |
297 | strbuf_add_unique_abbrev(sb, &data->ce->oid, abbrev); | |
4d28c4f7 ZH |
298 | else if (skip_prefix(start, "(objecttype)", &p)) |
299 | strbuf_addstr(sb, type_name(object_type(data->ce->ce_mode))); | |
300 | else if (skip_prefix(start, "(objectsize:padded)", &p)) | |
301 | expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 1); | |
302 | else if (skip_prefix(start, "(objectsize)", &p)) | |
303 | expand_objectsize(sb, &data->ce->oid, object_type(data->ce->ce_mode), 0); | |
ce74de93 ZH |
304 | else if (skip_prefix(start, "(stage)", &p)) |
305 | strbuf_addf(sb, "%d", ce_stage(data->ce)); | |
306 | else if (skip_prefix(start, "(eolinfo:index)", &p)) | |
307 | strbuf_addstr(sb, S_ISREG(data->ce->ce_mode) ? | |
308 | get_cached_convert_stats_ascii(data->istate, | |
309 | data->ce->name) : ""); | |
310 | else if (skip_prefix(start, "(eolinfo:worktree)", &p)) | |
311 | strbuf_addstr(sb, !lstat(data->pathname, &st) && | |
312 | S_ISREG(st.st_mode) ? | |
313 | get_wt_convert_stats_ascii(data->pathname) : ""); | |
314 | else if (skip_prefix(start, "(eolattr)", &p)) | |
315 | strbuf_addstr(sb, get_convert_attr_ascii(data->istate, | |
316 | data->pathname)); | |
317 | else if (skip_prefix(start, "(path)", &p)) | |
318 | write_name_to_buf(sb, data->pathname); | |
319 | else | |
320 | die(_("bad ls-files format: %%%.*s"), (int)len, start); | |
321 | ||
322 | return len; | |
323 | } | |
324 | ||
325 | static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce, | |
326 | const char *format, const char *fullname) { | |
327 | struct show_index_data data = { | |
328 | .pathname = fullname, | |
329 | .istate = repo->index, | |
330 | .ce = ce, | |
331 | }; | |
332 | struct strbuf sb = STRBUF_INIT; | |
333 | ||
334 | strbuf_expand(&sb, format, expand_show_index, &data); | |
335 | strbuf_addch(&sb, line_terminator); | |
336 | fwrite(sb.buf, sb.len, 1, stdout); | |
337 | strbuf_release(&sb); | |
338 | } | |
339 | ||
188dce13 BW |
340 | static void show_ce(struct repository *repo, struct dir_struct *dir, |
341 | const struct cache_entry *ce, const char *fullname, | |
342 | const char *tag) | |
5be4efbe | 343 | { |
188dce13 | 344 | if (max_prefix_len > strlen(fullname)) |
7e44c935 | 345 | die("git ls-files: internal error - cache entry not superset of prefix"); |
5be4efbe | 346 | |
75a6315f | 347 | if (recurse_submodules && S_ISGITLINK(ce->ce_mode) && |
188dce13 BW |
348 | is_submodule_active(repo, ce->name)) { |
349 | show_submodule(repo, dir, ce->name); | |
a52b321d | 350 | } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname), |
188dce13 | 351 | max_prefix_len, ps_matched, |
e77aa336 BW |
352 | S_ISDIR(ce->ce_mode) || |
353 | S_ISGITLINK(ce->ce_mode))) { | |
ce74de93 ZH |
354 | if (format) { |
355 | show_ce_fmt(repo, ce, format, fullname); | |
356 | print_debug(ce); | |
357 | return; | |
358 | } | |
359 | ||
a84f3e59 | 360 | tag = get_tag(ce, tag); |
5be4efbe | 361 | |
e77aa336 BW |
362 | if (!show_stage) { |
363 | fputs(tag, stdout); | |
364 | } else { | |
365 | printf("%s%06o %s %d\t", | |
366 | tag, | |
367 | ce->ce_mode, | |
290eada0 | 368 | repo_find_unique_abbrev(repo, &ce->oid, abbrev), |
e77aa336 BW |
369 | ce_stage(ce)); |
370 | } | |
188dce13 BW |
371 | write_eolinfo(repo->index, ce, fullname); |
372 | write_name(fullname); | |
5306ccf9 | 373 | print_debug(ce); |
2bcab240 | 374 | } |
5be4efbe LT |
375 | } |
376 | ||
847a9e5d | 377 | static void show_ru_info(struct index_state *istate) |
9d9a2f4a | 378 | { |
8a57c6e9 AR |
379 | struct string_list_item *item; |
380 | ||
2d407e2d | 381 | if (!istate->resolve_undo) |
9d9a2f4a | 382 | return; |
8a57c6e9 | 383 | |
2d407e2d | 384 | for_each_string_list_item(item, istate->resolve_undo) { |
8a57c6e9 AR |
385 | const char *path = item->string; |
386 | struct resolve_undo_info *ui = item->util; | |
387 | int i, len; | |
388 | ||
389 | len = strlen(path); | |
390 | if (len < max_prefix_len) | |
391 | continue; /* outside of the prefix */ | |
a52b321d | 392 | if (!match_pathspec(istate, &pathspec, path, len, |
ae8d0824 | 393 | max_prefix_len, ps_matched, 0)) |
8a57c6e9 AR |
394 | continue; /* uninterested */ |
395 | for (i = 0; i < 3; i++) { | |
396 | if (!ui->mode[i]) | |
397 | continue; | |
398 | printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], | |
d850b7a5 | 399 | repo_find_unique_abbrev(the_repository, &ui->oid[i], abbrev), |
8a57c6e9 | 400 | i + 1); |
e9a820ce | 401 | write_name(path); |
8a57c6e9 AR |
402 | } |
403 | } | |
9d9a2f4a JH |
404 | } |
405 | ||
1d35e3bf | 406 | static int ce_excluded(struct dir_struct *dir, struct index_state *istate, |
188dce13 | 407 | const char *fullname, const struct cache_entry *ce) |
782cd4c0 JH |
408 | { |
409 | int dtype = ce_to_dtype(ce); | |
188dce13 BW |
410 | return is_excluded(dir, istate, fullname, &dtype); |
411 | } | |
412 | ||
413 | static void construct_fullname(struct strbuf *out, const struct repository *repo, | |
414 | const struct cache_entry *ce) | |
415 | { | |
416 | strbuf_reset(out); | |
417 | if (repo->submodule_prefix) | |
418 | strbuf_addstr(out, repo->submodule_prefix); | |
419 | strbuf_addstr(out, ce->name); | |
782cd4c0 JH |
420 | } |
421 | ||
188dce13 | 422 | static void show_files(struct repository *repo, struct dir_struct *dir) |
8695c8bf LT |
423 | { |
424 | int i; | |
188dce13 | 425 | struct strbuf fullname = STRBUF_INIT; |
8695c8bf LT |
426 | |
427 | /* For cached/deleted files we don't need to even do the readdir */ | |
6ca45943 | 428 | if (show_others || show_killed) { |
2eac2a4c JH |
429 | if (!show_others) |
430 | dir->flags |= DIR_COLLECT_KILLED_ONLY; | |
188dce13 | 431 | fill_directory(dir, repo->index, &pathspec); |
6ca45943 | 432 | if (show_others) |
188dce13 | 433 | show_other_files(repo->index, dir); |
6ca45943 | 434 | if (show_killed) |
188dce13 | 435 | show_killed_files(repo->index, dir); |
8695c8bf | 436 | } |
188dce13 | 437 | |
ed644d16 ZH |
438 | if (!(show_cached || show_stage || show_deleted || show_modified)) |
439 | return; | |
78087097 DS |
440 | |
441 | if (!show_sparse_dirs) | |
442 | ensure_full_index(repo->index); | |
443 | ||
ed644d16 ZH |
444 | for (i = 0; i < repo->index->cache_nr; i++) { |
445 | const struct cache_entry *ce = repo->index->cache[i]; | |
446 | struct stat st; | |
447 | int stat_err; | |
188dce13 | 448 | |
ed644d16 ZH |
449 | construct_fullname(&fullname, repo, ce); |
450 | ||
451 | if ((dir->flags & DIR_SHOW_IGNORED) && | |
452 | !ce_excluded(dir, repo->index, fullname.buf, ce)) | |
453 | continue; | |
454 | if (ce->ce_flags & CE_UPDATE) | |
455 | continue; | |
456 | if ((show_cached || show_stage) && | |
93a7d983 | 457 | (!show_unmerged || ce_stage(ce))) { |
188dce13 BW |
458 | show_ce(repo, dir, ce, fullname.buf, |
459 | ce_stage(ce) ? tag_unmerged : | |
460 | (ce_skip_worktree(ce) ? tag_skip_worktree : | |
461 | tag_cached)); | |
93a7d983 ZH |
462 | if (skipping_duplicates) |
463 | goto skip_to_next_name; | |
464 | } | |
188dce13 | 465 | |
ed644d16 ZH |
466 | if (!(show_deleted || show_modified)) |
467 | continue; | |
468 | if (ce_skip_worktree(ce)) | |
469 | continue; | |
470 | stat_err = lstat(fullname.buf, &st); | |
471 | if (stat_err && (errno != ENOENT && errno != ENOTDIR)) | |
472 | error_errno("cannot lstat '%s'", fullname.buf); | |
93a7d983 | 473 | if (stat_err && show_deleted) { |
ed644d16 | 474 | show_ce(repo, dir, ce, fullname.buf, tag_removed); |
93a7d983 ZH |
475 | if (skipping_duplicates) |
476 | goto skip_to_next_name; | |
477 | } | |
ed644d16 | 478 | if (show_modified && |
93a7d983 | 479 | (stat_err || ie_modified(repo->index, ce, &st, 0))) { |
ed644d16 | 480 | show_ce(repo, dir, ce, fullname.buf, tag_modified); |
93a7d983 ZH |
481 | if (skipping_duplicates) |
482 | goto skip_to_next_name; | |
483 | } | |
484 | continue; | |
485 | ||
486 | skip_to_next_name: | |
487 | { | |
488 | int j; | |
489 | struct cache_entry **cache = repo->index->cache; | |
490 | for (j = i + 1; j < repo->index->cache_nr; j++) | |
491 | if (strcmp(ce->name, cache[j]->name)) | |
492 | break; | |
493 | i = j - 1; /* compensate for the for loop */ | |
494 | } | |
5be4efbe | 495 | } |
188dce13 BW |
496 | |
497 | strbuf_release(&fullname); | |
5be4efbe LT |
498 | } |
499 | ||
500 | /* | |
501 | * Prune the index to only contain stuff starting with "prefix" | |
502 | */ | |
6510ae17 BW |
503 | static void prune_index(struct index_state *istate, |
504 | const char *prefix, size_t prefixlen) | |
5be4efbe | 505 | { |
7b4158a8 | 506 | int pos; |
5be4efbe LT |
507 | unsigned int first, last; |
508 | ||
168e6355 | 509 | if (!prefix || !istate->cache_nr) |
7b4158a8 | 510 | return; |
6510ae17 | 511 | pos = index_name_pos(istate, prefix, prefixlen); |
5be4efbe LT |
512 | if (pos < 0) |
513 | pos = -pos-1; | |
96f6d3f6 | 514 | first = pos; |
6510ae17 | 515 | last = istate->cache_nr; |
5be4efbe | 516 | while (last > first) { |
568a05c5 | 517 | int next = first + ((last - first) >> 1); |
6510ae17 | 518 | const struct cache_entry *ce = istate->cache[next]; |
7b4158a8 | 519 | if (!strncmp(ce->name, prefix, prefixlen)) { |
5be4efbe LT |
520 | first = next+1; |
521 | continue; | |
8695c8bf | 522 | } |
5be4efbe LT |
523 | last = next; |
524 | } | |
f331ab9d | 525 | MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos); |
6510ae17 | 526 | istate->cache_nr = last - pos; |
5be4efbe LT |
527 | } |
528 | ||
cbca060e BW |
529 | static int get_common_prefix_len(const char *common_prefix) |
530 | { | |
531 | int common_prefix_len; | |
532 | ||
533 | if (!common_prefix) | |
534 | return 0; | |
535 | ||
536 | common_prefix_len = strlen(common_prefix); | |
537 | ||
538 | /* | |
539 | * If the prefix has a trailing slash, strip it so that submodules wont | |
540 | * be pruned from the index. | |
541 | */ | |
542 | if (common_prefix[common_prefix_len - 1] == '/') | |
543 | common_prefix_len--; | |
544 | ||
545 | return common_prefix_len; | |
546 | } | |
547 | ||
ce8e8804 | 548 | static const char * const ls_files_usage[] = { |
9c9b4f2f | 549 | N_("git ls-files [<options>] [<file>...]"), |
ce8e8804 MV |
550 | NULL |
551 | }; | |
552 | ||
ce8e8804 MV |
553 | static int option_parse_exclude(const struct option *opt, |
554 | const char *arg, int unset) | |
555 | { | |
72aeb187 | 556 | struct string_list *exclude_list = opt->value; |
ce8e8804 | 557 | |
517fe807 JK |
558 | BUG_ON_OPT_NEG(unset); |
559 | ||
ce8e8804 | 560 | exc_given = 1; |
72aeb187 | 561 | string_list_append(exclude_list, arg); |
ce8e8804 MV |
562 | |
563 | return 0; | |
564 | } | |
565 | ||
566 | static int option_parse_exclude_from(const struct option *opt, | |
567 | const char *arg, int unset) | |
568 | { | |
569 | struct dir_struct *dir = opt->value; | |
570 | ||
517fe807 JK |
571 | BUG_ON_OPT_NEG(unset); |
572 | ||
ce8e8804 | 573 | exc_given = 1; |
65edd96a | 574 | add_patterns_from_file(dir, arg); |
ce8e8804 MV |
575 | |
576 | return 0; | |
577 | } | |
578 | ||
579 | static int option_parse_exclude_standard(const struct option *opt, | |
580 | const char *arg, int unset) | |
581 | { | |
582 | struct dir_struct *dir = opt->value; | |
583 | ||
517fe807 JK |
584 | BUG_ON_OPT_NEG(unset); |
585 | BUG_ON_OPT_ARG(arg); | |
586 | ||
ce8e8804 MV |
587 | exc_given = 1; |
588 | setup_standard_excludes(dir); | |
589 | ||
590 | return 0; | |
591 | } | |
cf9a113d | 592 | |
efad1a56 | 593 | int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) |
8695c8bf | 594 | { |
72aeb187 | 595 | int require_work_tree = 0, show_tag = 0, i; |
4c217a4c | 596 | char *max_prefix; |
ce93a4c6 | 597 | struct dir_struct dir = DIR_INIT; |
caa3d554 | 598 | struct pattern_list *pl; |
72aeb187 | 599 | struct string_list exclude_list = STRING_LIST_INIT_NODUP; |
ce8e8804 | 600 | struct option builtin_ls_files_options[] = { |
1f3c79a9 JK |
601 | /* Think twice before adding "--nul" synonym to this */ |
602 | OPT_SET_INT('z', NULL, &line_terminator, | |
54b4d125 | 603 | N_("separate paths with the NUL character"), '\0'), |
d5d09d47 | 604 | OPT_BOOL('t', NULL, &show_tag, |
377adc3a | 605 | N_("identify the file status with tags")), |
d5d09d47 | 606 | OPT_BOOL('v', NULL, &show_valid_bit, |
377adc3a | 607 | N_("use lowercase letters for 'assume unchanged' files")), |
d8c71db8 BP |
608 | OPT_BOOL('f', NULL, &show_fsmonitor_bit, |
609 | N_("use lowercase letters for 'fsmonitor clean' files")), | |
d5d09d47 | 610 | OPT_BOOL('c', "cached", &show_cached, |
377adc3a | 611 | N_("show cached files in the output (default)")), |
d5d09d47 | 612 | OPT_BOOL('d', "deleted", &show_deleted, |
377adc3a | 613 | N_("show deleted files in the output")), |
d5d09d47 | 614 | OPT_BOOL('m', "modified", &show_modified, |
377adc3a | 615 | N_("show modified files in the output")), |
d5d09d47 | 616 | OPT_BOOL('o', "others", &show_others, |
377adc3a | 617 | N_("show other files in the output")), |
ce8e8804 | 618 | OPT_BIT('i', "ignored", &dir.flags, |
377adc3a | 619 | N_("show ignored files in the output"), |
ce8e8804 | 620 | DIR_SHOW_IGNORED), |
d5d09d47 | 621 | OPT_BOOL('s', "stage", &show_stage, |
377adc3a | 622 | N_("show staged contents' object name in the output")), |
d5d09d47 | 623 | OPT_BOOL('k', "killed", &show_killed, |
377adc3a | 624 | N_("show files on the filesystem that need to be removed")), |
ce8e8804 | 625 | OPT_BIT(0, "directory", &dir.flags, |
ad5fe377 | 626 | N_("show 'other' directories' names only"), |
ce8e8804 | 627 | DIR_SHOW_OTHER_DIRECTORIES), |
a7630bd4 | 628 | OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")), |
e9008b9a | 629 | OPT_NEGBIT(0, "empty-directory", &dir.flags, |
377adc3a | 630 | N_("don't show empty directories"), |
ce8e8804 | 631 | DIR_HIDE_EMPTY_DIRECTORIES), |
d5d09d47 | 632 | OPT_BOOL('u', "unmerged", &show_unmerged, |
377adc3a | 633 | N_("show unmerged files in the output")), |
d5d09d47 | 634 | OPT_BOOL(0, "resolve-undo", &show_resolve_undo, |
377adc3a | 635 | N_("show resolve-undo information")), |
203c8533 | 636 | OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"), |
377adc3a | 637 | N_("skip files matching pattern"), |
203c8533 DL |
638 | PARSE_OPT_NONEG, option_parse_exclude), |
639 | OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"), | |
54b4d125 | 640 | N_("read exclude patterns from <file>"), |
203c8533 | 641 | PARSE_OPT_NONEG, option_parse_exclude_from), |
377adc3a NTND |
642 | OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"), |
643 | N_("read additional per-directory exclude patterns in <file>")), | |
203c8533 | 644 | OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL, |
377adc3a | 645 | N_("add the standard git exclusions"), |
ccf659e8 | 646 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, |
203c8533 | 647 | option_parse_exclude_standard), |
3e4a67b4 NTND |
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), | |
e77aa336 BW |
651 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules, |
652 | N_("recurse through submodules")), | |
d5d09d47 | 653 | OPT_BOOL(0, "error-unmatch", &error_unmatch, |
377adc3a NTND |
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")), | |
ce8e8804 | 657 | OPT__ABBREV(&abbrev), |
d5d09d47 | 658 | OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")), |
93a7d983 ZH |
659 | OPT_BOOL(0, "deduplicate", &skipping_duplicates, |
660 | N_("suppress duplicate entries")), | |
78087097 DS |
661 | OPT_BOOL(0, "sparse", &show_sparse_dirs, |
662 | N_("show sparse directories in the presence of a sparse index")), | |
ce74de93 ZH |
663 | OPT_STRING_F(0, "format", &format, N_("format"), |
664 | N_("format to use for the output"), | |
665 | PARSE_OPT_NONEG), | |
ce8e8804 MV |
666 | OPT_END() |
667 | }; | |
eab4ac6a | 668 | int ret = 0; |
8695c8bf | 669 | |
cbb3167e NTND |
670 | if (argc == 2 && !strcmp(argv[1], "-h")) |
671 | usage_with_options(ls_files_usage, builtin_ls_files_options); | |
672 | ||
78087097 DS |
673 | prepare_repo_settings(the_repository); |
674 | the_repository->settings.command_requires_full_index = 0; | |
675 | ||
efad1a56 | 676 | prefix = cmd_prefix; |
5be4efbe | 677 | if (prefix) |
efad1a56 | 678 | prefix_len = strlen(prefix); |
ef90d6d4 | 679 | git_config(git_default_config, NULL); |
5be4efbe | 680 | |
188dce13 | 681 | if (repo_read_index(the_repository) < 0) |
c28b3d6e NTND |
682 | die("index file corrupt"); |
683 | ||
37782920 | 684 | argc = parse_options(argc, argv, prefix, builtin_ls_files_options, |
ce8e8804 | 685 | ls_files_usage, 0); |
65edd96a | 686 | pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option"); |
72aeb187 | 687 | for (i = 0; i < exclude_list.nr; i++) { |
65edd96a | 688 | add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args); |
72aeb187 | 689 | } |
ce74de93 ZH |
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 | ||
d8c71db8 | 697 | if (show_tag || show_valid_bit || show_fsmonitor_bit) { |
ce8e8804 MV |
698 | tag_cached = "H "; |
699 | tag_unmerged = "M "; | |
700 | tag_removed = "R "; | |
701 | tag_modified = "C "; | |
702 | tag_other = "? "; | |
703 | tag_killed = "K "; | |
44a36913 | 704 | tag_skip_worktree = "S "; |
9d9a2f4a | 705 | tag_resolve_undo = "U "; |
9ff768e9 | 706 | } |
de2e3b04 | 707 | if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed) |
ce8e8804 MV |
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; | |
93a7d983 ZH |
715 | if (show_tag || show_stage) |
716 | skipping_duplicates = 0; | |
ce8e8804 MV |
717 | if (dir.exclude_per_dir) |
718 | exc_given = 1; | |
9ff768e9 | 719 | |
7d8ae932 MH |
720 | if (require_work_tree && !is_inside_work_tree()) |
721 | setup_work_tree(); | |
6d9ba67b | 722 | |
e77aa336 | 723 | if (recurse_submodules && |
290eada0 | 724 | (show_deleted || show_others || show_unmerged || |
07c01b9f | 725 | show_killed || show_modified || show_resolve_undo || with_tree)) |
e77aa336 BW |
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 | ||
9e06d6ed | 732 | parse_pathspec(&pathspec, 0, |
cbca060e | 733 | PATHSPEC_PREFER_CWD, |
9e06d6ed | 734 | prefix, argv); |
f3670a57 | 735 | |
75a6315f BW |
736 | /* |
737 | * Find common prefix for all pathspec's | |
738 | * This is used as a performance optimization which unfortunately cannot | |
188dce13 BW |
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. | |
75a6315f BW |
743 | */ |
744 | if (recurse_submodules) | |
745 | max_prefix = NULL; | |
746 | else | |
747 | max_prefix = common_prefix(&pathspec); | |
cbca060e BW |
748 | max_prefix_len = get_common_prefix_len(max_prefix); |
749 | ||
188dce13 | 750 | prune_index(the_repository->index, max_prefix, max_prefix_len); |
5be4efbe | 751 | |
bba319b5 | 752 | /* Treat unmatching pathspec elements as errors */ |
9e06d6ed | 753 | if (pathspec.nr && error_unmatch) |
8b54c234 | 754 | ps_matched = xcalloc(pathspec.nr, 1); |
bba319b5 | 755 | |
b338e9f6 EN |
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 | ||
ac78b009 BW |
759 | if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given) |
760 | die("ls-files --ignored needs some exclude pattern"); | |
8695c8bf LT |
761 | |
762 | /* With no flags, we default to showing the cached files */ | |
0b437a18 RS |
763 | if (!(show_stage || show_deleted || show_others || show_unmerged || |
764 | show_killed || show_modified || show_resolve_undo)) | |
8695c8bf LT |
765 | show_cached = 1; |
766 | ||
64586e75 JH |
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) | |
12909b6b | 773 | die(_("options '%s' and '%s' cannot be used together"), "ls-files --with-tree", "-s/-u"); |
188dce13 | 774 | overlay_tree_on_index(the_repository->index, with_tree, max_prefix); |
64586e75 | 775 | } |
188dce13 BW |
776 | |
777 | show_files(the_repository, &dir); | |
778 | ||
9d9a2f4a | 779 | if (show_resolve_undo) |
188dce13 | 780 | show_ru_info(the_repository->index); |
bba319b5 | 781 | |
eab4ac6a ÆAB |
782 | if (ps_matched && report_path_error(ps_matched, &pathspec)) { |
783 | fprintf(stderr, "Did you forget to 'git add'?\n"); | |
784 | ret = 1; | |
bba319b5 JH |
785 | } |
786 | ||
272f0a57 | 787 | string_list_clear(&exclude_list, 0); |
eceba532 | 788 | dir_clear(&dir); |
4c217a4c | 789 | free(max_prefix); |
eab4ac6a | 790 | return ret; |
8695c8bf | 791 | } |