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