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