ls-files.con commit t/Makefile: OPTS -> GIT_TEST_OPTS (d6928eb)
   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 <fnmatch.h>
  10
  11#include "cache.h"
  12
  13static int show_deleted = 0;
  14static int show_cached = 0;
  15static int show_others = 0;
  16static int show_ignored = 0;
  17static int show_stage = 0;
  18static int show_unmerged = 0;
  19static int show_killed = 0;
  20static int line_terminator = '\n';
  21
  22static const char *tag_cached = "";
  23static const char *tag_unmerged = "";
  24static const char *tag_removed = "";
  25static const char *tag_other = "";
  26static const char *tag_killed = "";
  27
  28static int nr_excludes;
  29static const char **excludes;
  30static int excludes_alloc;
  31
  32static void add_exclude(const char *string)
  33{
  34        if (nr_excludes == excludes_alloc) {
  35                excludes_alloc = alloc_nr(excludes_alloc);
  36                excludes = realloc(excludes, excludes_alloc*sizeof(char *));
  37        }
  38        excludes[nr_excludes++] = string;
  39}
  40
  41static void add_excludes_from_file(const char *fname)
  42{
  43        int fd, i;
  44        long size;
  45        char *buf, *entry;
  46
  47        fd = open(fname, O_RDONLY);
  48        if (fd < 0)
  49                goto err;
  50        size = lseek(fd, 0, SEEK_END);
  51        if (size < 0)
  52                goto err;
  53        lseek(fd, 0, SEEK_SET);
  54        if (size == 0) {
  55                close(fd);
  56                return;
  57        }
  58        buf = xmalloc(size);
  59        if (read(fd, buf, size) != size)
  60                goto err;
  61        close(fd);
  62
  63        entry = buf;
  64        for (i = 0; i < size; i++) {
  65                if (buf[i] == '\n') {
  66                        if (entry != buf + i) {
  67                                buf[i] = 0;
  68                                add_exclude(entry);
  69                        }
  70                        entry = buf + i + 1;
  71                }
  72        }
  73        return;
  74
  75err:    perror(fname);
  76        exit(1);
  77}
  78
  79static int excluded(const char *pathname)
  80{
  81        int i;
  82        if (nr_excludes) {
  83                const char *basename = strrchr(pathname, '/');
  84                basename = (basename) ? basename+1 : pathname;
  85                for (i = 0; i < nr_excludes; i++)
  86                        if (fnmatch(excludes[i], basename, 0) == 0)
  87                                return 1;
  88        }
  89        return 0;
  90}
  91
  92struct nond_on_fs {
  93        int len;
  94        char name[0];
  95};
  96
  97static struct nond_on_fs **dir;
  98static int nr_dir;
  99static int dir_alloc;
 100
 101static void add_name(const char *pathname, int len)
 102{
 103        struct nond_on_fs *ent;
 104
 105        if (cache_name_pos(pathname, len) >= 0)
 106                return;
 107
 108        if (nr_dir == dir_alloc) {
 109                dir_alloc = alloc_nr(dir_alloc);
 110                dir = xrealloc(dir, dir_alloc*sizeof(ent));
 111        }
 112        ent = xmalloc(sizeof(*ent) + len + 1);
 113        ent->len = len;
 114        memcpy(ent->name, pathname, len);
 115        dir[nr_dir++] = ent;
 116}
 117
 118/*
 119 * Read a directory tree. We currently ignore anything but
 120 * directories, regular files and symlinks. That's because git
 121 * doesn't handle them at all yet. Maybe that will change some
 122 * day.
 123 *
 124 * Also, we currently ignore all names starting with a dot.
 125 * That likely will not change.
 126 */
 127static void read_directory(const char *path, const char *base, int baselen)
 128{
 129        DIR *dir = opendir(path);
 130
 131        if (dir) {
 132                struct dirent *de;
 133                char fullname[MAXPATHLEN + 1];
 134                memcpy(fullname, base, baselen);
 135
 136                while ((de = readdir(dir)) != NULL) {
 137                        int len;
 138
 139                        if (de->d_name[0] == '.')
 140                                continue;
 141                        if (excluded(de->d_name) != show_ignored)
 142                                continue;
 143                        len = strlen(de->d_name);
 144                        memcpy(fullname + baselen, de->d_name, len+1);
 145
 146                        switch (DTYPE(de)) {
 147                        struct stat st;
 148                        default:
 149                                continue;
 150                        case DT_UNKNOWN:
 151                                if (lstat(fullname, &st))
 152                                        continue;
 153                                if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
 154                                        break;
 155                                if (!S_ISDIR(st.st_mode))
 156                                        continue;
 157                                /* fallthrough */
 158                        case DT_DIR:
 159                                memcpy(fullname + baselen + len, "/", 2);
 160                                read_directory(fullname, fullname,
 161                                               baselen + len + 1);
 162                                continue;
 163                        case DT_REG:
 164                        case DT_LNK:
 165                                break;
 166                        }
 167                        add_name(fullname, baselen + len);
 168                }
 169                closedir(dir);
 170        }
 171}
 172
 173static int cmp_name(const void *p1, const void *p2)
 174{
 175        const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
 176        const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
 177
 178        return cache_name_compare(e1->name, e1->len,
 179                                  e2->name, e2->len);
 180}
 181
 182static void show_killed_files()
 183{
 184        int i;
 185        for (i = 0; i < nr_dir; i++) {
 186                struct nond_on_fs *ent = dir[i];
 187                char *cp, *sp;
 188                int pos, len, killed = 0;
 189
 190                for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
 191                        sp = strchr(cp, '/');
 192                        if (!sp) {
 193                                /* If ent->name is prefix of an entry in the
 194                                 * cache, it will be killed.
 195                                 */
 196                                pos = cache_name_pos(ent->name, ent->len);
 197                                if (0 <= pos)
 198                                        die("bug in show-killed-files");
 199                                pos = -pos - 1;
 200                                while (pos < active_nr &&
 201                                       ce_stage(active_cache[pos]))
 202                                        pos++; /* skip unmerged */
 203                                if (active_nr <= pos)
 204                                        break;
 205                                /* pos points at a name immediately after
 206                                 * ent->name in the cache.  Does it expect
 207                                 * ent->name to be a directory?
 208                                 */
 209                                len = ce_namelen(active_cache[pos]);
 210                                if ((ent->len < len) &&
 211                                    !strncmp(active_cache[pos]->name,
 212                                             ent->name, ent->len) &&
 213                                    active_cache[pos]->name[ent->len] == '/')
 214                                        killed = 1;
 215                                break;
 216                        }
 217                        if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
 218                                /* If any of the leading directories in
 219                                 * ent->name is registered in the cache,
 220                                 * ent->name will be killed.
 221                                 */
 222                                killed = 1;
 223                                break;
 224                        }
 225                }
 226                if (killed)
 227                        printf("%s%.*s%c", tag_killed,
 228                               dir[i]->len, dir[i]->name,
 229                               line_terminator);
 230        }
 231}
 232
 233static void show_files(void)
 234{
 235        int i;
 236
 237        /* For cached/deleted files we don't need to even do the readdir */
 238        if (show_others || show_killed) {
 239                read_directory(".", "", 0);
 240                qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
 241                if (show_others)
 242                        for (i = 0; i < nr_dir; i++)
 243                                printf("%s%.*s%c", tag_other,
 244                                       dir[i]->len, dir[i]->name,
 245                                       line_terminator);
 246                if (show_killed)
 247                        show_killed_files();
 248        }
 249        if (show_cached | show_stage) {
 250                for (i = 0; i < active_nr; i++) {
 251                        struct cache_entry *ce = active_cache[i];
 252                        if (excluded(ce->name) != show_ignored)
 253                                continue;
 254                        if (show_unmerged && !ce_stage(ce))
 255                                continue;
 256                        if (!show_stage)
 257                                printf("%s%s%c",
 258                                       ce_stage(ce) ? tag_unmerged :
 259                                       tag_cached,
 260                                       ce->name, line_terminator);
 261                        else
 262                                printf("%s%06o %s %d %s%c",
 263                                       ce_stage(ce) ? tag_unmerged :
 264                                       tag_cached,
 265                                       ntohl(ce->ce_mode),
 266                                       sha1_to_hex(ce->sha1),
 267                                       ce_stage(ce),
 268                                       ce->name, line_terminator); 
 269                }
 270        }
 271        if (show_deleted) {
 272                for (i = 0; i < active_nr; i++) {
 273                        struct cache_entry *ce = active_cache[i];
 274                        struct stat st;
 275                        if (excluded(ce->name) != show_ignored)
 276                                continue;
 277                        if (!lstat(ce->name, &st))
 278                                continue;
 279                        printf("%s%s%c", tag_removed, ce->name,
 280                               line_terminator);
 281                }
 282        }
 283}
 284
 285static const char *ls_files_usage =
 286        "ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
 287        "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
 288
 289int main(int argc, char **argv)
 290{
 291        int i;
 292
 293        for (i = 1; i < argc; i++) {
 294                char *arg = argv[i];
 295
 296                if (!strcmp(arg, "-z")) {
 297                        line_terminator = 0;
 298                } else if (!strcmp(arg, "-t")) {
 299                        tag_cached = "H ";
 300                        tag_unmerged = "M ";
 301                        tag_removed = "R ";
 302                        tag_other = "? ";
 303                        tag_killed = "K ";
 304                } else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
 305                        show_cached = 1;
 306                } else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
 307                        show_deleted = 1;
 308                } else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
 309                        show_others = 1;
 310                } else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
 311                        show_ignored = 1;
 312                } else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
 313                        show_stage = 1;
 314                } else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
 315                        show_killed = 1;
 316                } else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
 317                        /* There's no point in showing unmerged unless
 318                         * you also show the stage information.
 319                         */
 320                        show_stage = 1;
 321                        show_unmerged = 1;
 322                } else if (!strcmp(arg, "-x") && i+1 < argc) {
 323                        add_exclude(argv[++i]);
 324                } else if (!strncmp(arg, "--exclude=", 10)) {
 325                        add_exclude(arg+10);
 326                } else if (!strcmp(arg, "-X") && i+1 < argc) {
 327                        add_excludes_from_file(argv[++i]);
 328                } else if (!strncmp(arg, "--exclude-from=", 15)) {
 329                        add_excludes_from_file(arg+15);
 330                } else
 331                        usage(ls_files_usage);
 332        }
 333
 334        if (show_ignored && !nr_excludes) {
 335                fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
 336                        argv[0]);
 337                exit(1);
 338        }
 339
 340        /* With no flags, we default to showing the cached files */
 341        if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))
 342                show_cached = 1;
 343
 344        read_cache();
 345        show_files();
 346        return 0;
 347}