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.
6 * Copyright (C) Linus Torvalds, 2005
14 static int show_deleted
;
15 static int show_cached
;
16 static int show_others
;
17 static int show_stage
;
18 static int show_unmerged
;
19 static int show_modified
;
20 static int show_killed
;
21 static int show_valid_bit
;
22 static int line_terminator
= '\n';
24 static int prefix_len
;
25 static int prefix_offset
;
26 static const char **pathspec
;
27 static int error_unmatch
;
28 static char *ps_matched
;
30 static const char *tag_cached
= "";
31 static const char *tag_unmerged
= "";
32 static const char *tag_removed
= "";
33 static const char *tag_other
= "";
34 static const char *tag_killed
= "";
35 static const char *tag_modified
= "";
39 * Match a pathspec against a filename. The first "len" characters
40 * are the common prefix
42 static int match(const char **spec
, char *ps_matched
,
43 const char *filename
, int len
)
47 while ((m
= *spec
++) != NULL
) {
48 int matchlen
= strlen(m
+ len
);
52 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
53 if (m
[len
+ matchlen
- 1] == '/')
55 switch (filename
[len
+ matchlen
]) {
60 if (!fnmatch(m
+ len
, filename
+ len
, 0))
73 static void show_dir_entry(const char *tag
, struct dir_entry
*ent
)
76 int offset
= prefix_offset
;
79 die("git-ls-files: internal error - directory entry not superset of prefix");
81 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
85 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
86 putchar(line_terminator
);
89 static void show_other_files(struct dir_struct
*dir
)
92 for (i
= 0; i
< dir
->nr
; i
++) {
93 /* We should not have a matching entry, but we
94 * may have an unmerged entry for this path.
96 struct dir_entry
*ent
= dir
->entries
[i
];
97 int pos
= cache_name_pos(ent
->name
, ent
->len
);
98 struct cache_entry
*ce
;
100 die("bug in show-other-files");
102 if (pos
< active_nr
) {
103 ce
= active_cache
[pos
];
104 if (ce_namelen(ce
) == ent
->len
&&
105 !memcmp(ce
->name
, ent
->name
, ent
->len
))
106 continue; /* Yup, this one exists unmerged */
108 show_dir_entry(tag_other
, ent
);
112 static void show_killed_files(struct dir_struct
*dir
)
115 for (i
= 0; i
< dir
->nr
; i
++) {
116 struct dir_entry
*ent
= dir
->entries
[i
];
118 int pos
, len
, killed
= 0;
120 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
121 sp
= strchr(cp
, '/');
123 /* If ent->name is prefix of an entry in the
124 * cache, it will be killed.
126 pos
= cache_name_pos(ent
->name
, ent
->len
);
128 die("bug in show-killed-files");
130 while (pos
< active_nr
&&
131 ce_stage(active_cache
[pos
]))
132 pos
++; /* skip unmerged */
133 if (active_nr
<= pos
)
135 /* pos points at a name immediately after
136 * ent->name in the cache. Does it expect
137 * ent->name to be a directory?
139 len
= ce_namelen(active_cache
[pos
]);
140 if ((ent
->len
< len
) &&
141 !strncmp(active_cache
[pos
]->name
,
142 ent
->name
, ent
->len
) &&
143 active_cache
[pos
]->name
[ent
->len
] == '/')
147 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
148 /* If any of the leading directories in
149 * ent->name is registered in the cache,
150 * ent->name will be killed.
157 show_dir_entry(tag_killed
, dir
->entries
[i
]);
161 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
163 int len
= prefix_len
;
164 int offset
= prefix_offset
;
166 if (len
>= ce_namelen(ce
))
167 die("git-ls-files: internal error - cache entry not superset of prefix");
169 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
172 if (tag
&& *tag
&& show_valid_bit
&&
173 (ce
->ce_flags
& htons(CE_VALID
))) {
174 static char alttag
[4];
175 memcpy(alttag
, tag
, 3);
177 alttag
[0] = tolower(tag
[0]);
178 else if (tag
[0] == '?')
191 write_name_quoted("", 0, ce
->name
+ offset
,
192 line_terminator
, stdout
);
193 putchar(line_terminator
);
196 printf("%s%06o %s %d\t",
199 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
200 : sha1_to_hex(ce
->sha1
),
202 write_name_quoted("", 0, ce
->name
+ offset
,
203 line_terminator
, stdout
);
204 putchar(line_terminator
);
208 static void show_files(struct dir_struct
*dir
, const char *prefix
)
212 /* For cached/deleted files we don't need to even do the readdir */
213 if (show_others
|| show_killed
) {
214 const char *path
= ".", *base
= "";
215 int baselen
= prefix_len
;
218 path
= base
= prefix
;
219 read_directory(dir
, path
, base
, baselen
);
221 show_other_files(dir
);
223 show_killed_files(dir
);
225 if (show_cached
| show_stage
) {
226 for (i
= 0; i
< active_nr
; i
++) {
227 struct cache_entry
*ce
= active_cache
[i
];
228 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
230 if (show_unmerged
&& !ce_stage(ce
))
232 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
235 if (show_deleted
| show_modified
) {
236 for (i
= 0; i
< active_nr
; i
++) {
237 struct cache_entry
*ce
= active_cache
[i
];
240 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
242 err
= lstat(ce
->name
, &st
);
243 if (show_deleted
&& err
)
244 show_ce_entry(tag_removed
, ce
);
245 if (show_modified
&& ce_modified(ce
, &st
, 0))
246 show_ce_entry(tag_modified
, ce
);
252 * Prune the index to only contain stuff starting with "prefix"
254 static void prune_cache(const char *prefix
)
256 int pos
= cache_name_pos(prefix
, prefix_len
);
257 unsigned int first
, last
;
265 while (last
> first
) {
266 int next
= (last
+ first
) >> 1;
267 struct cache_entry
*ce
= active_cache
[next
];
268 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
277 static const char *verify_pathspec(const char *prefix
)
279 const char **p
, *n
, *prev
;
285 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
287 for (i
= 0; i
< max
; i
++) {
289 if (prev
&& prev
[i
] != c
)
291 if (!c
|| c
== '*' || c
== '?')
304 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
305 die("git-ls-files: cannot generate relative filenames containing '..'");
310 real_prefix
= xmalloc(max
+ 1);
311 memcpy(real_prefix
, prev
, max
);
312 real_prefix
[max
] = 0;
317 static const char ls_files_usage
[] =
318 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
319 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
320 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
323 int cmd_ls_files(int argc
, const char **argv
, const char *prefix
)
327 struct dir_struct dir
;
329 memset(&dir
, 0, sizeof(dir
));
331 prefix_offset
= strlen(prefix
);
332 git_config(git_default_config
);
334 for (i
= 1; i
< argc
; i
++) {
335 const char *arg
= argv
[i
];
337 if (!strcmp(arg
, "--")) {
341 if (!strcmp(arg
, "-z")) {
345 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
356 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
360 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
364 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
368 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
372 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
373 dir
.show_ignored
= 1;
376 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
380 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
384 if (!strcmp(arg
, "--directory")) {
385 dir
.show_other_directories
= 1;
388 if (!strcmp(arg
, "--no-empty-directory")) {
389 dir
.hide_empty_directories
= 1;
392 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
393 /* There's no point in showing unmerged unless
394 * you also show the stage information.
400 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
402 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
405 if (!strncmp(arg
, "--exclude=", 10)) {
407 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
410 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
412 add_excludes_from_file(&dir
, argv
[++i
]);
415 if (!strncmp(arg
, "--exclude-from=", 15)) {
417 add_excludes_from_file(&dir
, arg
+15);
420 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
422 dir
.exclude_per_dir
= arg
+ 24;
425 if (!strcmp(arg
, "--full-name")) {
429 if (!strcmp(arg
, "--error-unmatch")) {
433 if (!strncmp(arg
, "--abbrev=", 9)) {
434 abbrev
= strtoul(arg
+9, NULL
, 10);
435 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
436 abbrev
= MINIMUM_ABBREV
;
437 else if (abbrev
> 40)
441 if (!strcmp(arg
, "--abbrev")) {
442 abbrev
= DEFAULT_ABBREV
;
446 usage(ls_files_usage
);
450 pathspec
= get_pathspec(prefix
, argv
+ i
);
452 /* Verify that the pathspec matches the prefix */
454 prefix
= verify_pathspec(prefix
);
456 /* Treat unmatching pathspec elements as errors */
457 if (pathspec
&& error_unmatch
) {
459 for (num
= 0; pathspec
[num
]; num
++)
461 ps_matched
= xcalloc(1, num
);
464 if (dir
.show_ignored
&& !exc_given
) {
465 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
470 /* With no flags, we default to showing the cached files */
471 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
472 show_killed
| show_modified
))
478 show_files(&dir
, prefix
);
481 /* We need to make sure all pathspec matched otherwise
485 for (num
= 0; pathspec
[num
]; num
++) {
488 error("pathspec '%s' did not match any file(s) known to git.",
489 pathspec
[num
] + prefix_offset
);
494 fprintf(stderr
, "Did you forget to 'git add'?\n");
496 return errors
? 1 : 0;