]>
git.ipfire.org Git - thirdparty/git.git/blob - ls-files.c
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
13 static int show_deleted
= 0;
14 static int show_cached
= 0;
15 static int show_others
= 0;
16 static int show_ignored
= 0;
17 static int show_stage
= 0;
18 static int show_unmerged
= 0;
19 static int line_terminator
= '\n';
21 static const char *tag_cached
= "";
22 static const char *tag_unmerged
= "";
23 static const char *tag_removed
= "";
24 static const char *tag_other
= "";
26 static int nr_excludes
;
27 static const char **excludes
;
28 static int excludes_alloc
;
30 static void add_exclude(const char *string
)
32 if (nr_excludes
== excludes_alloc
) {
33 excludes_alloc
= alloc_nr(excludes_alloc
);
34 excludes
= realloc(excludes
, excludes_alloc
*sizeof(char *));
36 excludes
[nr_excludes
++] = string
;
39 static void add_excludes_from_file(const char *fname
)
45 fd
= open(fname
, O_RDONLY
);
48 size
= lseek(fd
, 0, SEEK_END
);
51 lseek(fd
, 0, SEEK_SET
);
57 if (read(fd
, buf
, size
) != size
)
62 for (i
= 0; i
< size
; i
++) {
64 if (entry
!= buf
+ i
) {
77 static int excluded(const char *pathname
)
81 const char *basename
= strrchr(pathname
, '/');
82 basename
= (basename
) ? basename
+1 : pathname
;
83 for (i
= 0; i
< nr_excludes
; i
++)
84 if (fnmatch(excludes
[i
], basename
, 0) == 0)
90 static const char **dir
;
94 static void add_name(const char *pathname
, int len
)
98 if (cache_name_pos(pathname
, len
) >= 0)
101 if (nr_dir
== dir_alloc
) {
102 dir_alloc
= alloc_nr(dir_alloc
);
103 dir
= xrealloc(dir
, dir_alloc
*sizeof(char *));
105 name
= xmalloc(len
+ 1);
106 memcpy(name
, pathname
, len
+ 1);
107 dir
[nr_dir
++] = name
;
111 * Read a directory tree. We currently ignore anything but
112 * directories, regular files and symlinks. That's because git
113 * doesn't handle them at all yet. Maybe that will change some
116 * Also, we currently ignore all names starting with a dot.
117 * That likely will not change.
119 static void read_directory(const char *path
, const char *base
, int baselen
)
121 DIR *dir
= opendir(path
);
125 char fullname
[MAXPATHLEN
+ 1];
126 memcpy(fullname
, base
, baselen
);
128 while ((de
= readdir(dir
)) != NULL
) {
131 if (de
->d_name
[0] == '.')
133 if (excluded(de
->d_name
) != show_ignored
)
135 len
= strlen(de
->d_name
);
136 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
143 if (lstat(fullname
, &st
))
145 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
147 if (!S_ISDIR(st
.st_mode
))
151 memcpy(fullname
+ baselen
+ len
, "/", 2);
152 read_directory(fullname
, fullname
,
159 add_name(fullname
, baselen
+ len
);
165 static int cmp_name(const void *p1
, const void *p2
)
167 const char *n1
= *(const char **)p1
;
168 const char *n2
= *(const char **)p2
;
169 int l1
= strlen(n1
), l2
= strlen(n2
);
171 return cache_name_compare(n1
, l1
, n2
, l2
);
174 static void show_files(void)
178 /* For cached/deleted files we don't need to even do the readdir */
180 read_directory(".", "", 0);
181 qsort(dir
, nr_dir
, sizeof(char *), cmp_name
);
182 for (i
= 0; i
< nr_dir
; i
++)
183 printf("%s%s%c", tag_other
, dir
[i
], line_terminator
);
185 if (show_cached
| show_stage
) {
186 for (i
= 0; i
< active_nr
; i
++) {
187 struct cache_entry
*ce
= active_cache
[i
];
188 if (excluded(ce
->name
) != show_ignored
)
190 if (show_unmerged
&& !ce_stage(ce
))
194 ce_stage(ce
) ? tag_unmerged
:
196 ce
->name
, line_terminator
);
198 printf("%s%06o %s %d %s%c",
199 ce_stage(ce
) ? tag_unmerged
:
202 sha1_to_hex(ce
->sha1
),
204 ce
->name
, line_terminator
);
208 for (i
= 0; i
< active_nr
; i
++) {
209 struct cache_entry
*ce
= active_cache
[i
];
211 if (excluded(ce
->name
) != show_ignored
)
213 if (!lstat(ce
->name
, &st
))
215 printf("%s%s%c", tag_removed
, ce
->name
,
221 static const char *ls_files_usage
=
222 "ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged])* "
223 "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
225 int main(int argc
, char **argv
)
229 for (i
= 1; i
< argc
; i
++) {
232 if (!strcmp(arg
, "-z")) {
234 } else if (!strcmp(arg
, "-t")) {
239 } else if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
241 } else if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
243 } else if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
245 } else if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
247 } else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
249 } else if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
250 /* There's no point in showing unmerged unless
251 * you also show the stage information.
255 } else if (!strcmp(arg
, "-x") && i
+1 < argc
) {
256 add_exclude(argv
[++i
]);
257 } else if (!strncmp(arg
, "--exclude=", 10)) {
259 } else if (!strcmp(arg
, "-X") && i
+1 < argc
) {
260 add_excludes_from_file(argv
[++i
]);
261 } else if (!strncmp(arg
, "--exclude-from=", 15)) {
262 add_excludes_from_file(arg
+15);
264 usage(ls_files_usage
);
267 if (show_ignored
&& !nr_excludes
) {
268 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
273 /* With no flags, we default to showing the cached files */
274 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
))