Remove the annoying "ok" printout from show-diff.
[git.git] / show-files.c
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  */
8 #include <dirent.h>
9 #include <sys/param.h>
10
11 #include "cache.h"
12
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
18 static const char **dir;
19 static int nr_dir;
20 static int dir_alloc;
21
22 static void add_name(const char *pathname, int len)
23 {
24         char *name;
25
26         if (cache_name_pos(pathname, len) >= 0)
27                 return;
28
29         if (nr_dir == dir_alloc) {
30                 dir_alloc = alloc_nr(dir_alloc);
31                 dir = realloc(dir, dir_alloc*sizeof(char *));
32         }
33         name = malloc(len + 1);
34         memcpy(name, pathname, len + 1);
35         dir[nr_dir++] = name;
36 }
37
38 /*
39  * Read a directory tree. We currently ignore anything but
40  * directories and regular files. That's because git doesn't
41  * handle them at all yet. Maybe that will change some day.
42  *
43  * Also, we currently ignore all names starting with a dot.
44  * That likely will not change. 
45  */
46 static void read_directory(const char *path, const char *base, int baselen)
47 {
48         DIR *dir = opendir(path);
49
50         if (dir) {
51                 struct dirent *de;
52                 char fullname[MAXPATHLEN + 1];
53                 memcpy(fullname, base, baselen);
54
55                 while ((de = readdir(dir)) != NULL) {
56                         int len;
57
58                         if (de->d_name[0] == '.')
59                                 continue;
60                         len = strlen(de->d_name);
61                         memcpy(fullname + baselen, de->d_name, len+1);
62
63                         switch (de->d_type) {
64                         struct stat st;
65                         default:
66                                 continue;
67                         case DT_UNKNOWN:
68                                 if (lstat(fullname, &st))
69                                         continue;
70                                 if (S_ISREG(st.st_mode))
71                                         break;
72                                 if (!S_ISDIR(st.st_mode))
73                                         continue;
74                                 /* fallthrough */
75                         case DT_DIR:
76                                 memcpy(fullname + baselen + len, "/", 2);
77                                 read_directory(fullname, fullname, baselen + len + 1);
78                                 continue;
79                         case DT_REG:
80                                 break;
81                         }
82                         add_name(fullname, baselen + len);
83                 }
84                 closedir(dir);
85         }
86 }
87
88 static int cmp_name(const void *p1, const void *p2)
89 {
90         const char *n1 = *(const char **)p1;
91         const char *n2 = *(const char **)p2;
92         int l1 = strlen(n1), l2 = strlen(n2);
93
94         return cache_name_compare(n1, l1, n2, l2);
95 }
96
97 static void show_files(void)
98 {
99         int i;
100
101         /* For cached/deleted files we don't need to even do the readdir */
102         if (show_others | show_ignored) {
103                 read_directory(".", "", 0);
104                 qsort(dir, nr_dir, sizeof(char *), cmp_name);
105         }
106         if (show_others) {
107                 for (i = 0; i < nr_dir; i++)
108                         printf("%s\n", dir[i]);
109         }
110         if (show_cached) {
111                 for (i = 0; i < active_nr; i++) {
112                         struct cache_entry *ce = active_cache[i];
113                         printf("%s\n", ce->name);
114                 }
115         }
116         if (show_deleted) {
117                 for (i = 0; i < active_nr; i++) {
118                         struct cache_entry *ce = active_cache[i];
119                         struct stat st;
120                         if (!stat(ce->name, &st))
121                                 continue;
122                         printf("%s\n", ce->name);
123                 }
124         }
125         if (show_ignored) {
126                 /* We don't have any "ignore" list yet */
127         }
128 }
129
130 int main(int argc, char **argv)
131 {
132         int i;
133
134         for (i = 1; i < argc; i++) {
135                 char *arg = argv[i];
136
137                 if (!strcmp(arg, "--cached")) {
138                         show_cached = 1;
139                         continue;
140                 }
141                 if (!strcmp(arg, "--deleted")) {
142                         show_deleted = 1;
143                         continue;
144                 }
145                 if (!strcmp(arg, "--others")) {
146                         show_others = 1;
147                         continue;
148                 }
149                 if (!strcmp(arg, "--ignored")) {
150                         show_ignored = 1;
151                         continue;
152                 }
153
154                 usage("show-files (--[cached|deleted|others|ignoded])*");
155         }
156
157         /* With no flags, we default to showing the cached files */
158         if (!(show_cached | show_deleted | show_others | show_ignored))
159                 show_cached = 1;
160
161         read_cache();
162         show_files();
163         return 0;
164 }