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 abbrev
= 0;
15 static int show_deleted
= 0;
16 static int show_cached
= 0;
17 static int show_others
= 0;
18 static int show_stage
= 0;
19 static int show_unmerged
= 0;
20 static int show_modified
= 0;
21 static int show_killed
= 0;
22 static int show_valid_bit
= 0;
23 static int line_terminator
= '\n';
25 static int prefix_len
= 0, prefix_offset
= 0;
26 static const char *prefix
= NULL
;
27 static const char **pathspec
= NULL
;
28 static int error_unmatch
= 0;
29 static char *ps_matched
= NULL
;
31 static const char *tag_cached
= "";
32 static const char *tag_unmerged
= "";
33 static const char *tag_removed
= "";
34 static const char *tag_other
= "";
35 static const char *tag_killed
= "";
36 static const char *tag_modified
= "";
40 * Match a pathspec against a filename. The first "len" characters
41 * are the common prefix
43 static int match(const char **spec
, char *ps_matched
,
44 const char *filename
, int len
)
48 while ((m
= *spec
++) != NULL
) {
49 int matchlen
= strlen(m
+ len
);
53 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
54 if (m
[len
+ matchlen
- 1] == '/')
56 switch (filename
[len
+ matchlen
]) {
61 if (!fnmatch(m
+ len
, filename
+ len
, 0))
74 static void show_dir_entry(const char *tag
, struct dir_entry
*ent
)
77 int offset
= prefix_offset
;
80 die("git-ls-files: internal error - directory entry not superset of prefix");
82 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
86 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
87 putchar(line_terminator
);
90 static void show_other_files(struct dir_struct
*dir
)
93 for (i
= 0; i
< dir
->nr
; i
++) {
94 /* We should not have a matching entry, but we
95 * may have an unmerged entry for this path.
97 struct dir_entry
*ent
= dir
->entries
[i
];
98 int pos
= cache_name_pos(ent
->name
, ent
->len
);
99 struct cache_entry
*ce
;
101 die("bug in show-other-files");
103 if (pos
< active_nr
) {
104 ce
= active_cache
[pos
];
105 if (ce_namelen(ce
) == ent
->len
&&
106 !memcmp(ce
->name
, ent
->name
, ent
->len
))
107 continue; /* Yup, this one exists unmerged */
109 show_dir_entry(tag_other
, ent
);
113 static void show_killed_files(struct dir_struct
*dir
)
116 for (i
= 0; i
< dir
->nr
; i
++) {
117 struct dir_entry
*ent
= dir
->entries
[i
];
119 int pos
, len
, killed
= 0;
121 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
122 sp
= strchr(cp
, '/');
124 /* If ent->name is prefix of an entry in the
125 * cache, it will be killed.
127 pos
= cache_name_pos(ent
->name
, ent
->len
);
129 die("bug in show-killed-files");
131 while (pos
< active_nr
&&
132 ce_stage(active_cache
[pos
]))
133 pos
++; /* skip unmerged */
134 if (active_nr
<= pos
)
136 /* pos points at a name immediately after
137 * ent->name in the cache. Does it expect
138 * ent->name to be a directory?
140 len
= ce_namelen(active_cache
[pos
]);
141 if ((ent
->len
< len
) &&
142 !strncmp(active_cache
[pos
]->name
,
143 ent
->name
, ent
->len
) &&
144 active_cache
[pos
]->name
[ent
->len
] == '/')
148 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
149 /* If any of the leading directories in
150 * ent->name is registered in the cache,
151 * ent->name will be killed.
158 show_dir_entry(tag_killed
, dir
->entries
[i
]);
162 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
164 int len
= prefix_len
;
165 int offset
= prefix_offset
;
167 if (len
>= ce_namelen(ce
))
168 die("git-ls-files: internal error - cache entry not superset of prefix");
170 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
173 if (tag
&& *tag
&& show_valid_bit
&&
174 (ce
->ce_flags
& htons(CE_VALID
))) {
175 static char alttag
[4];
176 memcpy(alttag
, tag
, 3);
178 alttag
[0] = tolower(tag
[0]);
179 else if (tag
[0] == '?')
192 write_name_quoted("", 0, ce
->name
+ offset
,
193 line_terminator
, stdout
);
194 putchar(line_terminator
);
197 printf("%s%06o %s %d\t",
200 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
201 : sha1_to_hex(ce
->sha1
),
203 write_name_quoted("", 0, ce
->name
+ offset
,
204 line_terminator
, stdout
);
205 putchar(line_terminator
);
209 static void show_files(struct dir_struct
*dir
)
213 /* For cached/deleted files we don't need to even do the readdir */
214 if (show_others
|| show_killed
) {
215 const char *path
= ".", *base
= "";
216 int baselen
= prefix_len
;
219 path
= base
= prefix
;
220 read_directory(dir
, path
, base
, baselen
);
222 show_other_files(dir
);
224 show_killed_files(dir
);
226 if (show_cached
| show_stage
) {
227 for (i
= 0; i
< active_nr
; i
++) {
228 struct cache_entry
*ce
= active_cache
[i
];
229 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
231 if (show_unmerged
&& !ce_stage(ce
))
233 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
236 if (show_deleted
| show_modified
) {
237 for (i
= 0; i
< active_nr
; i
++) {
238 struct cache_entry
*ce
= active_cache
[i
];
241 if (excluded(dir
, ce
->name
) != dir
->show_ignored
)
243 err
= lstat(ce
->name
, &st
);
244 if (show_deleted
&& err
)
245 show_ce_entry(tag_removed
, ce
);
246 if (show_modified
&& ce_modified(ce
, &st
, 0))
247 show_ce_entry(tag_modified
, ce
);
253 * Prune the index to only contain stuff starting with "prefix"
255 static void prune_cache(void)
257 int pos
= cache_name_pos(prefix
, prefix_len
);
258 unsigned int first
, last
;
266 while (last
> first
) {
267 int next
= (last
+ first
) >> 1;
268 struct cache_entry
*ce
= active_cache
[next
];
269 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
278 static void verify_pathspec(void)
280 const char **p
, *n
, *prev
;
286 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
288 for (i
= 0; i
< max
; i
++) {
290 if (prev
&& prev
[i
] != c
)
292 if (!c
|| c
== '*' || c
== '?')
305 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
306 die("git-ls-files: cannot generate relative filenames containing '..'");
311 real_prefix
= xmalloc(max
+ 1);
312 memcpy(real_prefix
, prev
, max
);
313 real_prefix
[max
] = 0;
315 prefix
= real_prefix
;
318 static const char ls_files_usage
[] =
319 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
320 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
321 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
324 int main(int argc
, const char **argv
)
328 struct dir_struct dir
;
330 memset(&dir
, 0, sizeof(dir
));
331 prefix
= setup_git_directory();
333 prefix_offset
= strlen(prefix
);
334 git_config(git_default_config
);
336 for (i
= 1; i
< argc
; i
++) {
337 const char *arg
= argv
[i
];
339 if (!strcmp(arg
, "--")) {
343 if (!strcmp(arg
, "-z")) {
347 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
358 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
362 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
366 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
370 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
374 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
375 dir
.show_ignored
= 1;
378 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
382 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
386 if (!strcmp(arg
, "--directory")) {
387 dir
.show_other_directories
= 1;
390 if (!strcmp(arg
, "--no-empty-directory")) {
391 dir
.hide_empty_directories
= 1;
394 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
395 /* There's no point in showing unmerged unless
396 * you also show the stage information.
402 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
404 add_exclude(argv
[++i
], "", 0, &dir
.exclude_list
[EXC_CMDL
]);
407 if (!strncmp(arg
, "--exclude=", 10)) {
409 add_exclude(arg
+10, "", 0, &dir
.exclude_list
[EXC_CMDL
]);
412 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
414 add_excludes_from_file(&dir
, argv
[++i
]);
417 if (!strncmp(arg
, "--exclude-from=", 15)) {
419 add_excludes_from_file(&dir
, arg
+15);
422 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
424 dir
.exclude_per_dir
= arg
+ 24;
427 if (!strcmp(arg
, "--full-name")) {
431 if (!strcmp(arg
, "--error-unmatch")) {
435 if (!strncmp(arg
, "--abbrev=", 9)) {
436 abbrev
= strtoul(arg
+9, NULL
, 10);
437 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
438 abbrev
= MINIMUM_ABBREV
;
439 else if (abbrev
> 40)
443 if (!strcmp(arg
, "--abbrev")) {
444 abbrev
= DEFAULT_ABBREV
;
448 usage(ls_files_usage
);
452 pathspec
= get_pathspec(prefix
, argv
+ i
);
454 /* Verify that the pathspec matches the prefix */
458 /* Treat unmatching pathspec elements as errors */
459 if (pathspec
&& error_unmatch
) {
461 for (num
= 0; pathspec
[num
]; num
++)
463 ps_matched
= xcalloc(1, num
);
466 if (dir
.show_ignored
&& !exc_given
) {
467 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
472 /* With no flags, we default to showing the cached files */
473 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
474 show_killed
| show_modified
))
483 /* We need to make sure all pathspec matched otherwise
487 for (num
= 0; pathspec
[num
]; num
++) {
490 error("pathspec '%s' did not match any.",
491 pathspec
[num
] + prefix_offset
);
494 return errors
? 1 : 0;