| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 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> |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 9 | #include <fnmatch.h> |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 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; |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 17 | static int show_stage = 0; |
| Linus Torvalds | eec8c63 | 2005-04-16 19:43:32 | [diff] [blame] | 18 | static int show_unmerged = 0; |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 19 | static int show_killed = 0; |
| Junio C Hamano | b83c834 | 2005-04-15 18:11:01 | [diff] [blame] | 20 | static int line_terminator = '\n'; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 21 | |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 22 | static const char *tag_cached = ""; |
| 23 | static const char *tag_unmerged = ""; |
| 24 | static const char *tag_removed = ""; |
| 25 | static const char *tag_other = ""; |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 26 | static const char *tag_killed = ""; |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 27 | |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 28 | static char *exclude_per_dir = NULL; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 29 | static int nr_excludes; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 30 | static int excludes_alloc; |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 31 | static struct exclude { |
| 32 | const char *pattern; |
| 33 | const char *base; |
| 34 | int baselen; |
| 35 | } **excludes; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 36 | |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 37 | static void add_exclude(const char *string, const char *base, int baselen) |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 38 | { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 39 | struct exclude *x = xmalloc(sizeof (*x)); |
| 40 | |
| 41 | x->pattern = string; |
| 42 | x->base = base; |
| 43 | x->baselen = baselen; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 44 | if (nr_excludes == excludes_alloc) { |
| 45 | excludes_alloc = alloc_nr(excludes_alloc); |
| 46 | excludes = realloc(excludes, excludes_alloc*sizeof(char *)); |
| 47 | } |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 48 | excludes[nr_excludes++] = x; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 49 | } |
| 50 | |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 51 | static int add_excludes_from_file_1(const char *fname, |
| 52 | const char *base, int baselen) |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 53 | { |
| 54 | int fd, i; |
| 55 | long size; |
| 56 | char *buf, *entry; |
| 57 | |
| 58 | fd = open(fname, O_RDONLY); |
| 59 | if (fd < 0) |
| 60 | goto err; |
| 61 | size = lseek(fd, 0, SEEK_END); |
| 62 | if (size < 0) |
| 63 | goto err; |
| 64 | lseek(fd, 0, SEEK_SET); |
| 65 | if (size == 0) { |
| 66 | close(fd); |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 67 | return 0; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 68 | } |
| 69 | buf = xmalloc(size); |
| 70 | if (read(fd, buf, size) != size) |
| 71 | goto err; |
| 72 | close(fd); |
| 73 | |
| 74 | entry = buf; |
| 75 | for (i = 0; i < size; i++) { |
| 76 | if (buf[i] == '\n') { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 77 | if (entry != buf + i && entry[0] != '#') { |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 78 | buf[i] = 0; |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 79 | add_exclude(entry, base, baselen); |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 80 | } |
| 81 | entry = buf + i + 1; |
| 82 | } |
| 83 | } |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 84 | return 0; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 85 | |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 86 | err: |
| 87 | if (0 <= fd) |
| 88 | close(fd); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | static void add_excludes_from_file(const char *fname) |
| 93 | { |
| 94 | if (add_excludes_from_file_1(fname, "", 0) < 0) |
| 95 | die("cannot use %s as an exclude file", fname); |
| 96 | } |
| 97 | |
| 98 | static int push_exclude_per_directory(const char *base, int baselen) |
| 99 | { |
| 100 | char exclude_file[PATH_MAX]; |
| 101 | int current_nr = nr_excludes; |
| 102 | |
| 103 | if (exclude_per_dir) { |
| 104 | memcpy(exclude_file, base, baselen); |
| 105 | strcpy(exclude_file + baselen, exclude_per_dir); |
| 106 | add_excludes_from_file_1(exclude_file, base, baselen); |
| 107 | } |
| 108 | return current_nr; |
| 109 | } |
| 110 | |
| 111 | static void pop_exclude_per_directory(int stk) |
| 112 | { |
| 113 | while (stk < nr_excludes) |
| 114 | free(excludes[--nr_excludes]); |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int excluded(const char *pathname) |
| 118 | { |
| 119 | int i; |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 120 | |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 121 | if (nr_excludes) { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 122 | int pathlen = strlen(pathname); |
| 123 | |
| 124 | for (i = 0; i < nr_excludes; i++) { |
| 125 | struct exclude *x = excludes[i]; |
| 126 | const char *exclude = x->pattern; |
| 127 | int to_exclude = 1; |
| 128 | |
| 129 | if (*exclude == '!') { |
| 130 | to_exclude = 0; |
| 131 | exclude++; |
| 132 | } |
| 133 | |
| 134 | if (!strchr(exclude, '/')) { |
| 135 | /* match basename */ |
| 136 | const char *basename = strrchr(pathname, '/'); |
| 137 | basename = (basename) ? basename+1 : pathname; |
| 138 | if (fnmatch(exclude, basename, 0) == 0) |
| 139 | return to_exclude; |
| 140 | } |
| 141 | else { |
| 142 | /* match with FNM_PATHNAME: |
| 143 | * exclude has base (baselen long) inplicitly |
| 144 | * in front of it. |
| 145 | */ |
| 146 | int baselen = x->baselen; |
| 147 | if (*exclude == '/') |
| 148 | exclude++; |
| 149 | |
| 150 | if (pathlen < baselen || |
| 151 | (baselen && pathname[baselen-1] != '/') || |
| 152 | strncmp(pathname, x->base, baselen)) |
| 153 | continue; |
| 154 | |
| 155 | if (fnmatch(exclude, pathname+baselen, |
| 156 | FNM_PATHNAME) == 0) |
| 157 | return to_exclude; |
| 158 | } |
| 159 | } |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 164 | struct nond_on_fs { |
| 165 | int len; |
| 166 | char name[0]; |
| 167 | }; |
| 168 | |
| 169 | static struct nond_on_fs **dir; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 170 | static int nr_dir; |
| 171 | static int dir_alloc; |
| 172 | |
| 173 | static void add_name(const char *pathname, int len) |
| 174 | { |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 175 | struct nond_on_fs *ent; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 176 | |
| 177 | if (cache_name_pos(pathname, len) >= 0) |
| 178 | return; |
| 179 | |
| 180 | if (nr_dir == dir_alloc) { |
| 181 | dir_alloc = alloc_nr(dir_alloc); |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 182 | dir = xrealloc(dir, dir_alloc*sizeof(ent)); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 183 | } |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 184 | ent = xmalloc(sizeof(*ent) + len + 1); |
| 185 | ent->len = len; |
| 186 | memcpy(ent->name, pathname, len); |
| 187 | dir[nr_dir++] = ent; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Read a directory tree. We currently ignore anything but |
| Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 192 | * directories, regular files and symlinks. That's because git |
| 193 | * doesn't handle them at all yet. Maybe that will change some |
| 194 | * day. |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 195 | * |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 196 | * Also, we ignore the name ".git" (even if it is not a directory). |
| Ingo Molnar | aebb267 | 2005-04-12 18:36:26 | [diff] [blame] | 197 | * That likely will not change. |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 198 | */ |
| 199 | static void read_directory(const char *path, const char *base, int baselen) |
| 200 | { |
| 201 | DIR *dir = opendir(path); |
| 202 | |
| 203 | if (dir) { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 204 | int exclude_stk; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 205 | struct dirent *de; |
| 206 | char fullname[MAXPATHLEN + 1]; |
| 207 | memcpy(fullname, base, baselen); |
| 208 | |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 209 | exclude_stk = push_exclude_per_directory(base, baselen); |
| 210 | |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 211 | while ((de = readdir(dir)) != NULL) { |
| 212 | int len; |
| 213 | |
| Junio C Hamano | c4ee295 | 2005-05-25 01:20:08 | [diff] [blame] | 214 | if ((de->d_name[0] == '.') && |
| 215 | (de->d_name[1] == 0 || |
| 216 | !strcmp(de->d_name + 1, ".") || |
| 217 | !strcmp(de->d_name + 1, "git"))) |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 218 | continue; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 219 | len = strlen(de->d_name); |
| 220 | memcpy(fullname + baselen, de->d_name, len+1); |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 221 | if (excluded(fullname) != show_ignored) |
| 222 | continue; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 223 | |
| Edgar Toernig | b682969 | 2005-04-30 16:51:03 | [diff] [blame] | 224 | switch (DTYPE(de)) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 225 | struct stat st; |
| 226 | default: |
| 227 | continue; |
| 228 | case DT_UNKNOWN: |
| 229 | if (lstat(fullname, &st)) |
| 230 | continue; |
| Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 231 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 232 | break; |
| 233 | if (!S_ISDIR(st.st_mode)) |
| 234 | continue; |
| 235 | /* fallthrough */ |
| 236 | case DT_DIR: |
| 237 | memcpy(fullname + baselen + len, "/", 2); |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 238 | read_directory(fullname, fullname, |
| 239 | baselen + len + 1); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 240 | continue; |
| 241 | case DT_REG: |
| Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 242 | case DT_LNK: |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | add_name(fullname, baselen + len); |
| 246 | } |
| 247 | closedir(dir); |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 248 | |
| 249 | pop_exclude_per_directory(exclude_stk); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | static int cmp_name(const void *p1, const void *p2) |
| 254 | { |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 255 | const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1; |
| 256 | const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2; |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 257 | |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 258 | return cache_name_compare(e1->name, e1->len, |
| 259 | e2->name, e2->len); |
| 260 | } |
| 261 | |
| Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 262 | static void show_killed_files(void) |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 263 | { |
| 264 | int i; |
| 265 | for (i = 0; i < nr_dir; i++) { |
| 266 | struct nond_on_fs *ent = dir[i]; |
| 267 | char *cp, *sp; |
| 268 | int pos, len, killed = 0; |
| 269 | |
| 270 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { |
| 271 | sp = strchr(cp, '/'); |
| 272 | if (!sp) { |
| 273 | /* If ent->name is prefix of an entry in the |
| 274 | * cache, it will be killed. |
| 275 | */ |
| 276 | pos = cache_name_pos(ent->name, ent->len); |
| 277 | if (0 <= pos) |
| 278 | die("bug in show-killed-files"); |
| 279 | pos = -pos - 1; |
| 280 | while (pos < active_nr && |
| 281 | ce_stage(active_cache[pos])) |
| 282 | pos++; /* skip unmerged */ |
| 283 | if (active_nr <= pos) |
| 284 | break; |
| 285 | /* pos points at a name immediately after |
| 286 | * ent->name in the cache. Does it expect |
| 287 | * ent->name to be a directory? |
| 288 | */ |
| 289 | len = ce_namelen(active_cache[pos]); |
| 290 | if ((ent->len < len) && |
| 291 | !strncmp(active_cache[pos]->name, |
| 292 | ent->name, ent->len) && |
| 293 | active_cache[pos]->name[ent->len] == '/') |
| 294 | killed = 1; |
| 295 | break; |
| 296 | } |
| 297 | if (0 <= cache_name_pos(ent->name, sp - ent->name)) { |
| 298 | /* If any of the leading directories in |
| 299 | * ent->name is registered in the cache, |
| 300 | * ent->name will be killed. |
| 301 | */ |
| 302 | killed = 1; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | if (killed) |
| 307 | printf("%s%.*s%c", tag_killed, |
| 308 | dir[i]->len, dir[i]->name, |
| 309 | line_terminator); |
| 310 | } |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void show_files(void) |
| 314 | { |
| 315 | int i; |
| 316 | |
| 317 | /* For cached/deleted files we don't need to even do the readdir */ |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 318 | if (show_others || show_killed) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 319 | read_directory(".", "", 0); |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 320 | qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name); |
| 321 | if (show_others) |
| 322 | for (i = 0; i < nr_dir; i++) |
| 323 | printf("%s%.*s%c", tag_other, |
| 324 | dir[i]->len, dir[i]->name, |
| 325 | line_terminator); |
| 326 | if (show_killed) |
| 327 | show_killed_files(); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 328 | } |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 329 | if (show_cached | show_stage) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 330 | for (i = 0; i < active_nr; i++) { |
| 331 | struct cache_entry *ce = active_cache[i]; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 332 | if (excluded(ce->name) != show_ignored) |
| 333 | continue; |
| Linus Torvalds | eec8c63 | 2005-04-16 19:43:32 | [diff] [blame] | 334 | if (show_unmerged && !ce_stage(ce)) |
| 335 | continue; |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 336 | if (!show_stage) |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 337 | printf("%s%s%c", |
| 338 | ce_stage(ce) ? tag_unmerged : |
| 339 | tag_cached, |
| 340 | ce->name, line_terminator); |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 341 | else |
| Junio C Hamano | 2eab945 | 2005-05-26 21:38:19 | [diff] [blame] | 342 | printf("%s%06o %s %d\t%s%c", |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 343 | ce_stage(ce) ? tag_unmerged : |
| 344 | tag_cached, |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 345 | ntohl(ce->ce_mode), |
| 346 | sha1_to_hex(ce->sha1), |
| 347 | ce_stage(ce), |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 348 | ce->name, line_terminator); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | if (show_deleted) { |
| 352 | for (i = 0; i < active_nr; i++) { |
| 353 | struct cache_entry *ce = active_cache[i]; |
| 354 | struct stat st; |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 355 | if (excluded(ce->name) != show_ignored) |
| 356 | continue; |
| Kay Sievers | 8ae0a8c | 2005-05-05 12:38:25 | [diff] [blame] | 357 | if (!lstat(ce->name, &st)) |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 358 | continue; |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 359 | printf("%s%s%c", tag_removed, ce->name, |
| 360 | line_terminator); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 361 | } |
| 362 | } |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 363 | } |
| 364 | |
| Nicolas Pitre | 1771039 | 2005-04-30 20:59:38 | [diff] [blame] | 365 | static const char *ls_files_usage = |
| Alexey Nezhdanov | 667bb59 | 2005-05-19 11:17:16 | [diff] [blame] | 366 | "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* " |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 367 | "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] " |
| 368 | "[ --exclude-per-directory=<filename> ]"; |
| 369 | ; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 370 | |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 371 | int main(int argc, char **argv) |
| 372 | { |
| 373 | int i; |
| 374 | |
| 375 | for (i = 1; i < argc; i++) { |
| 376 | char *arg = argv[i]; |
| 377 | |
| Junio C Hamano | b83c834 | 2005-04-15 18:11:01 | [diff] [blame] | 378 | if (!strcmp(arg, "-z")) { |
| 379 | line_terminator = 0; |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 380 | } else if (!strcmp(arg, "-t")) { |
| 381 | tag_cached = "H "; |
| 382 | tag_unmerged = "M "; |
| 383 | tag_removed = "R "; |
| 384 | tag_other = "? "; |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 385 | tag_killed = "K "; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 386 | } else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 387 | show_cached = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 388 | } else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 389 | show_deleted = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 390 | } else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 391 | show_others = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 392 | } else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) { |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 393 | show_ignored = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 394 | } else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) { |
| Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 395 | show_stage = 1; |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 396 | } else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) { |
| 397 | show_killed = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 398 | } else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) { |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 399 | /* There's no point in showing unmerged unless |
| 400 | * you also show the stage information. |
| 401 | */ |
| Linus Torvalds | eec8c63 | 2005-04-16 19:43:32 | [diff] [blame] | 402 | show_stage = 1; |
| 403 | show_unmerged = 1; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 404 | } else if (!strcmp(arg, "-x") && i+1 < argc) { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 405 | add_exclude(argv[++i], "", 0); |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 406 | } else if (!strncmp(arg, "--exclude=", 10)) { |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 407 | add_exclude(arg+10, "", 0); |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 408 | } else if (!strcmp(arg, "-X") && i+1 < argc) { |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 409 | add_excludes_from_file(argv[++i]); |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 410 | } else if (!strncmp(arg, "--exclude-from=", 15)) { |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 411 | add_excludes_from_file(arg+15); |
| Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 412 | } else if (!strncmp(arg, "--exclude-per-directory=", 24)) { |
| 413 | exclude_per_dir = arg + 24; |
| Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 414 | } else |
| Nicolas Pitre | 1771039 | 2005-04-30 20:59:38 | [diff] [blame] | 415 | usage(ls_files_usage); |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (show_ignored && !nr_excludes) { |
| Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 419 | fprintf(stderr, "%s: --ignored needs some exclude pattern\n", |
| 420 | argv[0]); |
| Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 421 | exit(1); |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* With no flags, we default to showing the cached files */ |
| Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 425 | if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed)) |
| Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 426 | show_cached = 1; |
| 427 | |
| 428 | read_cache(); |
| 429 | show_files(); |
| 430 | return 0; |
| 431 | } |