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 | |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 22 | static int prefix_len = 0, prefix_offset = 0; |
| 23 | static const char *prefix = NULL; |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 24 | static const char **pathspec = NULL; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 25 | |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 26 | static const char *tag_cached = ""; |
| 27 | static const char *tag_unmerged = ""; |
| 28 | static const char *tag_removed = ""; |
| 29 | static const char *tag_other = ""; |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 30 | static const char *tag_killed = ""; |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 31 | |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 32 | static char *exclude_per_dir = NULL; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 33 | |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 34 | /* We maintain three exclude pattern lists: |
| 35 | * EXC_CMDL lists patterns explicitly given on the command line. |
| 36 | * EXC_DIRS lists patterns obtained from per-directory ignore files. |
| 37 | * EXC_FILE lists patterns from fallback ignore files. |
| 38 | */ |
| 39 | #define EXC_CMDL 0 |
| 40 | #define EXC_DIRS 1 |
| 41 | #define EXC_FILE 2 |
| 42 | static struct exclude_list { |
| 43 | int nr; |
| 44 | int alloc; |
| 45 | struct exclude { |
| 46 | const char *pattern; |
| 47 | const char *base; |
| 48 | int baselen; |
| 49 | } **excludes; |
| 50 | } exclude_list[3]; |
| 51 | |
| 52 | static void add_exclude(const char *string, const char *base, |
| 53 | int baselen, struct exclude_list *which) |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 54 | { |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 55 | struct exclude *x = xmalloc(sizeof (*x)); |
| 56 | |
| 57 | x->pattern = string; |
| 58 | x->base = base; |
| 59 | x->baselen = baselen; |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 60 | if (which->nr == which->alloc) { |
| 61 | which->alloc = alloc_nr(which->alloc); |
| 62 | which->excludes = realloc(which->excludes, |
| 63 | which->alloc * sizeof(x)); |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 64 | } |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 65 | which->excludes[which->nr++] = x; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 66 | } |
| 67 | |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 68 | static int add_excludes_from_file_1(const char *fname, |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 69 | const char *base, |
| 70 | int baselen, |
| 71 | struct exclude_list *which) |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 72 | { |
| 73 | int fd, i; |
| 74 | long size; |
| 75 | char *buf, *entry; |
| 76 | |
| 77 | fd = open(fname, O_RDONLY); |
| 78 | if (fd < 0) |
| 79 | goto err; |
| 80 | size = lseek(fd, 0, SEEK_END); |
| 81 | if (size < 0) |
| 82 | goto err; |
| 83 | lseek(fd, 0, SEEK_SET); |
| 84 | if (size == 0) { |
| 85 | close(fd); |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 86 | return 0; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 87 | } |
| 88 | buf = xmalloc(size); |
| 89 | if (read(fd, buf, size) != size) |
| 90 | goto err; |
| 91 | close(fd); |
| 92 | |
| 93 | entry = buf; |
| 94 | for (i = 0; i < size; i++) { |
| 95 | if (buf[i] == '\n') { |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 96 | if (entry != buf + i && entry[0] != '#') { |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 97 | buf[i] = 0; |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 98 | add_exclude(entry, base, baselen, which); |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 99 | } |
| 100 | entry = buf + i + 1; |
| 101 | } |
| 102 | } |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 103 | return 0; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 104 | |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 105 | err: |
| 106 | if (0 <= fd) |
| 107 | close(fd); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | static void add_excludes_from_file(const char *fname) |
| 112 | { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 113 | if (add_excludes_from_file_1(fname, "", 0, |
| 114 | &exclude_list[EXC_FILE]) < 0) |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 115 | die("cannot use %s as an exclude file", fname); |
| 116 | } |
| 117 | |
| 118 | static int push_exclude_per_directory(const char *base, int baselen) |
| 119 | { |
| 120 | char exclude_file[PATH_MAX]; |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 121 | struct exclude_list *el = &exclude_list[EXC_DIRS]; |
| 122 | int current_nr = el->nr; |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 123 | |
| 124 | if (exclude_per_dir) { |
| 125 | memcpy(exclude_file, base, baselen); |
| 126 | strcpy(exclude_file + baselen, exclude_per_dir); |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 127 | add_excludes_from_file_1(exclude_file, base, baselen, el); |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 128 | } |
| 129 | return current_nr; |
| 130 | } |
| 131 | |
| 132 | static void pop_exclude_per_directory(int stk) |
| 133 | { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 134 | struct exclude_list *el = &exclude_list[EXC_DIRS]; |
| 135 | |
| 136 | while (stk < el->nr) |
| 137 | free(el->excludes[--el->nr]); |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 138 | } |
| 139 | |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 140 | /* Scan the list and let the last match determines the fate. |
| 141 | * Return 1 for exclude, 0 for include and -1 for undecided. |
| 142 | */ |
| 143 | static int excluded_1(const char *pathname, |
| 144 | int pathlen, |
| 145 | struct exclude_list *el) |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 146 | { |
| 147 | int i; |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 148 | |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 149 | if (el->nr) { |
| 150 | for (i = el->nr - 1; 0 <= i; i--) { |
| 151 | struct exclude *x = el->excludes[i]; |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 152 | const char *exclude = x->pattern; |
| 153 | int to_exclude = 1; |
| 154 | |
| 155 | if (*exclude == '!') { |
| 156 | to_exclude = 0; |
| 157 | exclude++; |
| 158 | } |
| 159 | |
| 160 | if (!strchr(exclude, '/')) { |
| 161 | /* match basename */ |
| 162 | const char *basename = strrchr(pathname, '/'); |
| 163 | basename = (basename) ? basename+1 : pathname; |
| 164 | if (fnmatch(exclude, basename, 0) == 0) |
| 165 | return to_exclude; |
| 166 | } |
| 167 | else { |
| 168 | /* match with FNM_PATHNAME: |
| 169 | * exclude has base (baselen long) inplicitly |
| 170 | * in front of it. |
| 171 | */ |
| 172 | int baselen = x->baselen; |
| 173 | if (*exclude == '/') |
| 174 | exclude++; |
| 175 | |
| 176 | if (pathlen < baselen || |
| 177 | (baselen && pathname[baselen-1] != '/') || |
| 178 | strncmp(pathname, x->base, baselen)) |
| 179 | continue; |
| 180 | |
| 181 | if (fnmatch(exclude, pathname+baselen, |
| 182 | FNM_PATHNAME) == 0) |
| 183 | return to_exclude; |
| 184 | } |
| 185 | } |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 186 | } |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 187 | return -1; /* undecided */ |
| 188 | } |
| 189 | |
| 190 | static int excluded(const char *pathname) |
| 191 | { |
| 192 | int pathlen = strlen(pathname); |
| 193 | int st; |
| 194 | |
| 195 | for (st = EXC_CMDL; st <= EXC_FILE; st++) { |
| 196 | switch (excluded_1(pathname, pathlen, &exclude_list[st])) { |
| 197 | case 0: |
| 198 | return 0; |
| 199 | case 1: |
| 200 | return 1; |
| 201 | } |
| 202 | } |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 206 | struct nond_on_fs { |
| 207 | int len; |
Junio C Hamano | 2c04662 | 2005-08-29 19:41:03 | [diff] [blame] | 208 | char name[0]; |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | static struct nond_on_fs **dir; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 212 | static int nr_dir; |
| 213 | static int dir_alloc; |
| 214 | |
| 215 | static void add_name(const char *pathname, int len) |
| 216 | { |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 217 | struct nond_on_fs *ent; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 218 | |
| 219 | if (cache_name_pos(pathname, len) >= 0) |
| 220 | return; |
| 221 | |
| 222 | if (nr_dir == dir_alloc) { |
| 223 | dir_alloc = alloc_nr(dir_alloc); |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 224 | dir = xrealloc(dir, dir_alloc*sizeof(ent)); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 225 | } |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 226 | ent = xmalloc(sizeof(*ent) + len + 1); |
| 227 | ent->len = len; |
| 228 | memcpy(ent->name, pathname, len); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 229 | ent->name[len] = 0; |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 230 | dir[nr_dir++] = ent; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Read a directory tree. We currently ignore anything but |
Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 235 | * directories, regular files and symlinks. That's because git |
| 236 | * doesn't handle them at all yet. Maybe that will change some |
| 237 | * day. |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 238 | * |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 239 | * Also, we ignore the name ".git" (even if it is not a directory). |
Ingo Molnar | aebb267 | 2005-04-12 18:36:26 | [diff] [blame] | 240 | * That likely will not change. |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 241 | */ |
| 242 | static void read_directory(const char *path, const char *base, int baselen) |
| 243 | { |
| 244 | DIR *dir = opendir(path); |
| 245 | |
| 246 | if (dir) { |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 247 | int exclude_stk; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 248 | struct dirent *de; |
| 249 | char fullname[MAXPATHLEN + 1]; |
| 250 | memcpy(fullname, base, baselen); |
| 251 | |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 252 | exclude_stk = push_exclude_per_directory(base, baselen); |
| 253 | |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 254 | while ((de = readdir(dir)) != NULL) { |
| 255 | int len; |
| 256 | |
Junio C Hamano | c4ee295 | 2005-05-25 01:20:08 | [diff] [blame] | 257 | if ((de->d_name[0] == '.') && |
| 258 | (de->d_name[1] == 0 || |
| 259 | !strcmp(de->d_name + 1, ".") || |
| 260 | !strcmp(de->d_name + 1, "git"))) |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 261 | continue; |
| 262 | len = strlen(de->d_name); |
| 263 | memcpy(fullname + baselen, de->d_name, len+1); |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 264 | if (excluded(fullname) != show_ignored) |
| 265 | continue; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 266 | |
Edgar Toernig | b682969 | 2005-04-30 16:51:03 | [diff] [blame] | 267 | switch (DTYPE(de)) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 268 | struct stat st; |
| 269 | default: |
| 270 | continue; |
| 271 | case DT_UNKNOWN: |
| 272 | if (lstat(fullname, &st)) |
| 273 | continue; |
Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 274 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 275 | break; |
| 276 | if (!S_ISDIR(st.st_mode)) |
| 277 | continue; |
| 278 | /* fallthrough */ |
| 279 | case DT_DIR: |
| 280 | memcpy(fullname + baselen + len, "/", 2); |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 281 | read_directory(fullname, fullname, |
| 282 | baselen + len + 1); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 283 | continue; |
| 284 | case DT_REG: |
Junio C Hamano | a15c1c6 | 2005-05-13 00:16:04 | [diff] [blame] | 285 | case DT_LNK: |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | add_name(fullname, baselen + len); |
| 289 | } |
| 290 | closedir(dir); |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 291 | |
| 292 | pop_exclude_per_directory(exclude_stk); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | static int cmp_name(const void *p1, const void *p2) |
| 297 | { |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 298 | const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1; |
| 299 | const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 300 | |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 301 | return cache_name_compare(e1->name, e1->len, |
| 302 | e2->name, e2->len); |
| 303 | } |
| 304 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 305 | /* |
| 306 | * Match a pathspec against a filename. The first "len" characters |
| 307 | * are the common prefix |
| 308 | */ |
| 309 | static int match(const char **spec, const char *filename, int len) |
| 310 | { |
| 311 | const char *m; |
| 312 | |
| 313 | while ((m = *spec++) != NULL) { |
| 314 | int matchlen = strlen(m + len); |
| 315 | |
| 316 | if (!matchlen) |
| 317 | return 1; |
| 318 | if (!strncmp(m + len, filename + len, matchlen)) { |
| 319 | if (m[len + matchlen - 1] == '/') |
| 320 | return 1; |
| 321 | switch (filename[len + matchlen]) { |
| 322 | case '/': case '\0': |
| 323 | return 1; |
| 324 | } |
| 325 | } |
| 326 | if (!fnmatch(m + len, filename + len, 0)) |
| 327 | return 1; |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 332 | static void show_dir_entry(const char *tag, struct nond_on_fs *ent) |
| 333 | { |
| 334 | int len = prefix_len; |
| 335 | int offset = prefix_offset; |
| 336 | |
| 337 | if (len >= ent->len) |
| 338 | die("git-ls-files: internal error - directory entry not superset of prefix"); |
| 339 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 340 | if (pathspec && !match(pathspec, ent->name, len)) |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 341 | return; |
| 342 | |
| 343 | printf("%s%s%c", tag, ent->name + offset, line_terminator); |
| 344 | } |
| 345 | |
Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 346 | static void show_killed_files(void) |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 347 | { |
| 348 | int i; |
| 349 | for (i = 0; i < nr_dir; i++) { |
| 350 | struct nond_on_fs *ent = dir[i]; |
| 351 | char *cp, *sp; |
| 352 | int pos, len, killed = 0; |
| 353 | |
| 354 | for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) { |
| 355 | sp = strchr(cp, '/'); |
| 356 | if (!sp) { |
| 357 | /* If ent->name is prefix of an entry in the |
| 358 | * cache, it will be killed. |
| 359 | */ |
| 360 | pos = cache_name_pos(ent->name, ent->len); |
| 361 | if (0 <= pos) |
| 362 | die("bug in show-killed-files"); |
| 363 | pos = -pos - 1; |
| 364 | while (pos < active_nr && |
| 365 | ce_stage(active_cache[pos])) |
| 366 | pos++; /* skip unmerged */ |
| 367 | if (active_nr <= pos) |
| 368 | break; |
| 369 | /* pos points at a name immediately after |
| 370 | * ent->name in the cache. Does it expect |
| 371 | * ent->name to be a directory? |
| 372 | */ |
| 373 | len = ce_namelen(active_cache[pos]); |
| 374 | if ((ent->len < len) && |
| 375 | !strncmp(active_cache[pos]->name, |
| 376 | ent->name, ent->len) && |
| 377 | active_cache[pos]->name[ent->len] == '/') |
| 378 | killed = 1; |
| 379 | break; |
| 380 | } |
| 381 | if (0 <= cache_name_pos(ent->name, sp - ent->name)) { |
| 382 | /* If any of the leading directories in |
| 383 | * ent->name is registered in the cache, |
| 384 | * ent->name will be killed. |
| 385 | */ |
| 386 | killed = 1; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | if (killed) |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 391 | show_dir_entry(tag_killed, dir[i]); |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 392 | } |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 393 | } |
| 394 | |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 395 | static void show_ce_entry(const char *tag, struct cache_entry *ce) |
| 396 | { |
| 397 | int len = prefix_len; |
| 398 | int offset = prefix_offset; |
| 399 | |
| 400 | if (len >= ce_namelen(ce)) |
| 401 | die("git-ls-files: internal error - cache entry not superset of prefix"); |
| 402 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 403 | if (pathspec && !match(pathspec, ce->name, len)) |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 404 | return; |
| 405 | |
| 406 | if (!show_stage) |
| 407 | printf("%s%s%c", tag, ce->name + offset, line_terminator); |
| 408 | else |
| 409 | printf("%s%06o %s %d\t%s%c", |
| 410 | tag, |
| 411 | ntohl(ce->ce_mode), |
| 412 | sha1_to_hex(ce->sha1), |
| 413 | ce_stage(ce), |
| 414 | ce->name + offset, line_terminator); |
| 415 | } |
| 416 | |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 417 | static void show_files(void) |
| 418 | { |
| 419 | int i; |
| 420 | |
| 421 | /* 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] | 422 | if (show_others || show_killed) { |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 423 | const char *path = ".", *base = ""; |
| 424 | int baselen = prefix_len; |
| 425 | |
| 426 | if (baselen) |
| 427 | path = base = prefix; |
| 428 | read_directory(path, base, baselen); |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 429 | qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name); |
| 430 | if (show_others) |
| 431 | for (i = 0; i < nr_dir; i++) |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 432 | show_dir_entry(tag_other, dir[i]); |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 433 | if (show_killed) |
| 434 | show_killed_files(); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 435 | } |
Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 436 | if (show_cached | show_stage) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 437 | for (i = 0; i < active_nr; i++) { |
| 438 | struct cache_entry *ce = active_cache[i]; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 439 | if (excluded(ce->name) != show_ignored) |
| 440 | continue; |
Linus Torvalds | eec8c63 | 2005-04-16 19:43:32 | [diff] [blame] | 441 | if (show_unmerged && !ce_stage(ce)) |
| 442 | continue; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 443 | show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | if (show_deleted) { |
| 447 | for (i = 0; i < active_nr; i++) { |
| 448 | struct cache_entry *ce = active_cache[i]; |
| 449 | struct stat st; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 450 | if (excluded(ce->name) != show_ignored) |
| 451 | continue; |
Kay Sievers | 8ae0a8c | 2005-05-05 12:38:25 | [diff] [blame] | 452 | if (!lstat(ce->name, &st)) |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 453 | continue; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 454 | show_ce_entry(tag_removed, ce); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 455 | } |
| 456 | } |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 457 | } |
| 458 | |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 459 | /* |
| 460 | * Prune the index to only contain stuff starting with "prefix" |
| 461 | */ |
| 462 | static void prune_cache(void) |
| 463 | { |
| 464 | int pos = cache_name_pos(prefix, prefix_len); |
| 465 | unsigned int first, last; |
| 466 | |
| 467 | if (pos < 0) |
| 468 | pos = -pos-1; |
| 469 | active_cache += pos; |
| 470 | active_nr -= pos; |
| 471 | first = 0; |
| 472 | last = active_nr; |
| 473 | while (last > first) { |
| 474 | int next = (last + first) >> 1; |
| 475 | struct cache_entry *ce = active_cache[next]; |
| 476 | if (!strncmp(ce->name, prefix, prefix_len)) { |
| 477 | first = next+1; |
| 478 | continue; |
| 479 | } |
| 480 | last = next; |
| 481 | } |
| 482 | active_nr = last; |
| 483 | } |
| 484 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 485 | static void verify_pathspec(void) |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 486 | { |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 487 | const char **p, *n, *prev; |
| 488 | char *real_prefix; |
| 489 | unsigned long max; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 490 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 491 | prev = NULL; |
| 492 | max = PATH_MAX; |
| 493 | for (p = pathspec; (n = *p) != NULL; p++) { |
| 494 | int i, len = 0; |
| 495 | for (i = 0; i < max; i++) { |
| 496 | char c = n[i]; |
| 497 | if (prev && prev[i] != c) |
| 498 | break; |
Linus Torvalds | 5690614 | 2005-08-24 00:14:13 | [diff] [blame] | 499 | if (!c || c == '*' || c == '?') |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 500 | break; |
| 501 | if (c == '/') |
| 502 | len = i+1; |
| 503 | } |
| 504 | prev = n; |
| 505 | if (len < max) { |
| 506 | max = len; |
| 507 | if (!max) |
| 508 | break; |
| 509 | } |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 510 | } |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 511 | |
| 512 | if (prefix_offset > max || memcmp(prev, prefix, prefix_offset)) |
| 513 | die("git-ls-files: cannot generate relative filenames containing '..'"); |
| 514 | |
| 515 | real_prefix = NULL; |
| 516 | prefix_len = max; |
| 517 | if (max) { |
| 518 | real_prefix = xmalloc(max + 1); |
| 519 | memcpy(real_prefix, prev, max); |
| 520 | real_prefix[max] = 0; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 521 | } |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 522 | prefix = real_prefix; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 523 | } |
| 524 | |
Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 525 | static const char ls_files_usage[] = |
Alexey Nezhdanov | 667bb59 | 2005-05-19 11:17:16 | [diff] [blame] | 526 | "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* " |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 527 | "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] " |
| 528 | "[ --exclude-per-directory=<filename> ]"; |
Nicolas Pitre | cf9a113 | 2005-04-28 22:06:25 | [diff] [blame] | 529 | |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 530 | int main(int argc, char **argv) |
| 531 | { |
| 532 | int i; |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 533 | int exc_given = 0; |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 534 | |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 535 | prefix = setup_git_directory(); |
| 536 | if (prefix) |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 537 | prefix_offset = strlen(prefix); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 538 | |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 539 | for (i = 1; i < argc; i++) { |
| 540 | char *arg = argv[i]; |
| 541 | |
Junio C Hamano | b83c834 | 2005-04-15 18:11:01 | [diff] [blame] | 542 | if (!strcmp(arg, "-z")) { |
| 543 | line_terminator = 0; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 544 | continue; |
| 545 | } |
| 546 | if (!strcmp(arg, "-t")) { |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 547 | tag_cached = "H "; |
| 548 | tag_unmerged = "M "; |
| 549 | tag_removed = "R "; |
| 550 | tag_other = "? "; |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 551 | tag_killed = "K "; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 552 | continue; |
| 553 | } |
| 554 | if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 555 | show_cached = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 556 | continue; |
| 557 | } |
| 558 | if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 559 | show_deleted = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 560 | continue; |
| 561 | } |
| 562 | if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 563 | show_others = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 564 | continue; |
| 565 | } |
| 566 | if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) { |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 567 | show_ignored = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) { |
Junio C Hamano | aee4619 | 2005-04-16 15:33:23 | [diff] [blame] | 571 | show_stage = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 572 | continue; |
| 573 | } |
| 574 | if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) { |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 575 | show_killed = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 576 | continue; |
| 577 | } |
| 578 | if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) { |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 579 | /* There's no point in showing unmerged unless |
| 580 | * you also show the stage information. |
| 581 | */ |
Linus Torvalds | eec8c63 | 2005-04-16 19:43:32 | [diff] [blame] | 582 | show_stage = 1; |
| 583 | show_unmerged = 1; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 584 | continue; |
| 585 | } |
| 586 | if (!strcmp(arg, "-x") && i+1 < argc) { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 587 | exc_given = 1; |
| 588 | add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 589 | continue; |
| 590 | } |
| 591 | if (!strncmp(arg, "--exclude=", 10)) { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 592 | exc_given = 1; |
| 593 | add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 594 | continue; |
| 595 | } |
| 596 | if (!strcmp(arg, "-X") && i+1 < argc) { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 597 | exc_given = 1; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 598 | add_excludes_from_file(argv[++i]); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 599 | continue; |
| 600 | } |
| 601 | if (!strncmp(arg, "--exclude-from=", 15)) { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 602 | exc_given = 1; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 603 | add_excludes_from_file(arg+15); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 604 | continue; |
| 605 | } |
| 606 | if (!strncmp(arg, "--exclude-per-directory=", 24)) { |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 607 | exc_given = 1; |
Junio C Hamano | f87f949 | 2005-07-24 22:26:09 | [diff] [blame] | 608 | exclude_per_dir = arg + 24; |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 609 | continue; |
| 610 | } |
| 611 | if (!strcmp(arg, "--full-name")) { |
| 612 | prefix_offset = 0; |
| 613 | continue; |
| 614 | } |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 615 | if (*arg == '-') |
Nicolas Pitre | 1771039 | 2005-04-30 20:59:38 | [diff] [blame] | 616 | usage(ls_files_usage); |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 617 | break; |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 618 | } |
| 619 | |
Linus Torvalds | 56fc510 | 2005-08-22 00:27:50 | [diff] [blame] | 620 | pathspec = get_pathspec(prefix, argv + i); |
| 621 | |
| 622 | /* Verify that the pathspec matches the prefix */ |
| 623 | if (pathspec) |
| 624 | verify_pathspec(); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 625 | |
Junio C Hamano | fee8825 | 2005-07-29 06:32:20 | [diff] [blame] | 626 | if (show_ignored && !exc_given) { |
Petr Baudis | 20d37ef | 2005-04-22 02:47:08 | [diff] [blame] | 627 | fprintf(stderr, "%s: --ignored needs some exclude pattern\n", |
| 628 | argv[0]); |
Nicolas Pitre | 9ff768e | 2005-04-28 18:44:04 | [diff] [blame] | 629 | exit(1); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* With no flags, we default to showing the cached files */ |
Junio C Hamano | 6ca4594 | 2005-05-13 00:17:54 | [diff] [blame] | 633 | if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed)) |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 634 | show_cached = 1; |
| 635 | |
| 636 | read_cache(); |
Linus Torvalds | 5be4efb | 2005-08-21 19:55:33 | [diff] [blame] | 637 | if (prefix) |
| 638 | prune_cache(); |
Linus Torvalds | 8695c8b | 2005-04-12 01:55:38 | [diff] [blame] | 639 | show_files(); |
| 640 | return 0; |
| 641 | } |