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
12 static int show_deleted
= 0;
13 static int show_cached
= 0;
14 static int show_others
= 0;
15 static int show_ignored
= 0;
16 static int show_stage
= 0;
17 static int show_unmerged
= 0;
18 static int line_terminator
= '\n';
20 static const char **dir
;
24 static void add_name(const char *pathname
, int len
)
28 if (cache_name_pos(pathname
, len
) >= 0)
31 if (nr_dir
== dir_alloc
) {
32 dir_alloc
= alloc_nr(dir_alloc
);
33 dir
= realloc(dir
, dir_alloc
*sizeof(char *));
35 name
= malloc(len
+ 1);
36 memcpy(name
, pathname
, len
+ 1);
41 * Read a directory tree. We currently ignore anything but
42 * directories and regular files. That's because git doesn't
43 * handle them at all yet. Maybe that will change some day.
45 * Also, we currently ignore all names starting with a dot.
46 * That likely will not change.
48 static void read_directory(const char *path
, const char *base
, int baselen
)
50 DIR *dir
= opendir(path
);
54 char fullname
[MAXPATHLEN
+ 1];
55 memcpy(fullname
, base
, baselen
);
57 while ((de
= readdir(dir
)) != NULL
) {
60 if (de
->d_name
[0] == '.')
62 len
= strlen(de
->d_name
);
63 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
70 if (lstat(fullname
, &st
))
72 if (S_ISREG(st
.st_mode
))
74 if (!S_ISDIR(st
.st_mode
))
78 memcpy(fullname
+ baselen
+ len
, "/", 2);
79 read_directory(fullname
, fullname
, baselen
+ len
+ 1);
84 add_name(fullname
, baselen
+ len
);
90 static int cmp_name(const void *p1
, const void *p2
)
92 const char *n1
= *(const char **)p1
;
93 const char *n2
= *(const char **)p2
;
94 int l1
= strlen(n1
), l2
= strlen(n2
);
96 return cache_name_compare(n1
, l1
, n2
, l2
);
99 static void show_files(void)
103 /* For cached/deleted files we don't need to even do the readdir */
104 if (show_others
| show_ignored
) {
105 read_directory(".", "", 0);
106 qsort(dir
, nr_dir
, sizeof(char *), cmp_name
);
109 for (i
= 0; i
< nr_dir
; i
++)
110 printf("%s%c", dir
[i
], line_terminator
);
112 if (show_cached
| show_stage
) {
113 for (i
= 0; i
< active_nr
; i
++) {
114 struct cache_entry
*ce
= active_cache
[i
];
115 if (show_unmerged
&& !ce_stage(ce
))
118 printf("%s%c", ce
->name
, line_terminator
);
120 printf(/* "%06o %s %d %10d %s%c", */
123 sha1_to_hex(ce
->sha1
),
125 /* ntohl(ce->ce_size), */
126 ce
->name
, line_terminator
);
130 for (i
= 0; i
< active_nr
; i
++) {
131 struct cache_entry
*ce
= active_cache
[i
];
133 if (!stat(ce
->name
, &st
))
135 printf("%s%c", ce
->name
, line_terminator
);
139 /* We don't have any "ignore" list yet */
143 int main(int argc
, char **argv
)
147 for (i
= 1; i
< argc
; i
++) {
150 if (!strcmp(arg
, "-z")) {
155 if (!strcmp(arg
, "--cached")) {
159 if (!strcmp(arg
, "--deleted")) {
163 if (!strcmp(arg
, "--others")) {
167 if (!strcmp(arg
, "--ignored")) {
171 if (!strcmp(arg
, "--stage")) {
175 if (!strcmp(arg
, "--unmerged")) {
176 // There's no point in showing unmerged unless you also show the stage information
182 usage("show-files [-z] (--[cached|deleted|others|ignored|stage])*");
185 /* With no flags, we default to showing the cached files */
186 if (!(show_stage
| show_deleted
| show_others
| show_ignored
| show_unmerged
))