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