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