diff-no-index.con commit Merge branch 'da/difftool-git-files' into maint (01e13d0)
   1/*
   2 * "diff --no-index" support
   3 * Copyright (c) 2007 by Johannes Schindelin
   4 * Copyright (c) 2008 by Junio C Hamano
   5 */
   6
   7#include "cache.h"
   8#include "color.h"
   9#include "commit.h"
  10#include "blob.h"
  11#include "tag.h"
  12#include "diff.h"
  13#include "diffcore.h"
  14#include "revision.h"
  15#include "log-tree.h"
  16#include "builtin.h"
  17#include "string-list.h"
  18
  19static int read_directory(const char *path, struct string_list *list)
  20{
  21        DIR *dir;
  22        struct dirent *e;
  23
  24        if (!(dir = opendir(path)))
  25                return error("Could not open directory %s", path);
  26
  27        while ((e = readdir(dir)))
  28                if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
  29                        string_list_insert(list, e->d_name);
  30
  31        closedir(dir);
  32        return 0;
  33}
  34
  35/*
  36 * This should be "(standard input)" or something, but it will
  37 * probably expose many more breakages in the way no-index code
  38 * is bolted onto the diff callchain.
  39 */
  40static const char file_from_standard_input[] = "-";
  41
  42static int get_mode(const char *path, int *mode)
  43{
  44        struct stat st;
  45
  46        if (!path || !strcmp(path, "/dev/null"))
  47                *mode = 0;
  48#ifdef GIT_WINDOWS_NATIVE
  49        else if (!strcasecmp(path, "nul"))
  50                *mode = 0;
  51#endif
  52        else if (path == file_from_standard_input)
  53                *mode = create_ce_mode(0666);
  54        else if (lstat(path, &st))
  55                return error("Could not access '%s'", path);
  56        else
  57                *mode = st.st_mode;
  58        return 0;
  59}
  60
  61static int populate_from_stdin(struct diff_filespec *s)
  62{
  63        struct strbuf buf = STRBUF_INIT;
  64        size_t size = 0;
  65
  66        if (strbuf_read(&buf, 0, 0) < 0)
  67                return error("error while reading from stdin %s",
  68                                     strerror(errno));
  69
  70        s->should_munmap = 0;
  71        s->data = strbuf_detach(&buf, &size);
  72        s->size = size;
  73        s->should_free = 1;
  74        s->is_stdin = 1;
  75        return 0;
  76}
  77
  78static struct diff_filespec *noindex_filespec(const char *name, int mode)
  79{
  80        struct diff_filespec *s;
  81
  82        if (!name)
  83                name = "/dev/null";
  84        s = alloc_filespec(name);
  85        fill_filespec(s, null_sha1, 0, mode);
  86        if (name == file_from_standard_input)
  87                populate_from_stdin(s);
  88        return s;
  89}
  90
  91static int queue_diff(struct diff_options *o,
  92                      const char *name1, const char *name2)
  93{
  94        int mode1 = 0, mode2 = 0;
  95
  96        if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
  97                return -1;
  98
  99        if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
 100                return error("file/directory conflict: %s, %s", name1, name2);
 101
 102        if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
 103                struct strbuf buffer1 = STRBUF_INIT;
 104                struct strbuf buffer2 = STRBUF_INIT;
 105                struct string_list p1 = STRING_LIST_INIT_DUP;
 106                struct string_list p2 = STRING_LIST_INIT_DUP;
 107                int i1, i2, ret = 0;
 108                size_t len1 = 0, len2 = 0;
 109
 110                if (name1 && read_directory(name1, &p1))
 111                        return -1;
 112                if (name2 && read_directory(name2, &p2)) {
 113                        string_list_clear(&p1, 0);
 114                        return -1;
 115                }
 116
 117                if (name1) {
 118                        strbuf_addstr(&buffer1, name1);
 119                        if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
 120                                strbuf_addch(&buffer1, '/');
 121                        len1 = buffer1.len;
 122                }
 123
 124                if (name2) {
 125                        strbuf_addstr(&buffer2, name2);
 126                        if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
 127                                strbuf_addch(&buffer2, '/');
 128                        len2 = buffer2.len;
 129                }
 130
 131                for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
 132                        const char *n1, *n2;
 133                        int comp;
 134
 135                        strbuf_setlen(&buffer1, len1);
 136                        strbuf_setlen(&buffer2, len2);
 137
 138                        if (i1 == p1.nr)
 139                                comp = 1;
 140                        else if (i2 == p2.nr)
 141                                comp = -1;
 142                        else
 143                                comp = strcmp(p1.items[i1].string, p2.items[i2].string);
 144
 145                        if (comp > 0)
 146                                n1 = NULL;
 147                        else {
 148                                strbuf_addstr(&buffer1, p1.items[i1++].string);
 149                                n1 = buffer1.buf;
 150                        }
 151
 152                        if (comp < 0)
 153                                n2 = NULL;
 154                        else {
 155                                strbuf_addstr(&buffer2, p2.items[i2++].string);
 156                                n2 = buffer2.buf;
 157                        }
 158
 159                        ret = queue_diff(o, n1, n2);
 160                }
 161                string_list_clear(&p1, 0);
 162                string_list_clear(&p2, 0);
 163                strbuf_release(&buffer1);
 164                strbuf_release(&buffer2);
 165
 166                return ret;
 167        } else {
 168                struct diff_filespec *d1, *d2;
 169
 170                if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
 171                        unsigned tmp;
 172                        const char *tmp_c;
 173                        tmp = mode1; mode1 = mode2; mode2 = tmp;
 174                        tmp_c = name1; name1 = name2; name2 = tmp_c;
 175                }
 176
 177                d1 = noindex_filespec(name1, mode1);
 178                d2 = noindex_filespec(name2, mode2);
 179                diff_queue(&diff_queued_diff, d1, d2);
 180                return 0;
 181        }
 182}
 183
 184void diff_no_index(struct rev_info *revs,
 185                   int argc, const char **argv,
 186                   const char *prefix)
 187{
 188        int i, prefixlen;
 189        unsigned deprecated_show_diff_q_option_used = 0;
 190        const char *paths[2];
 191
 192        diff_setup(&revs->diffopt);
 193        for (i = 1; i < argc - 2; ) {
 194                int j;
 195                if (!strcmp(argv[i], "--no-index"))
 196                        i++;
 197                else if (!strcmp(argv[i], "-q")) {
 198                        deprecated_show_diff_q_option_used = 1;
 199                        i++;
 200                }
 201                else if (!strcmp(argv[i], "--"))
 202                        i++;
 203                else {
 204                        j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
 205                        if (!j)
 206                                die("invalid diff option/value: %s", argv[i]);
 207                        i += j;
 208                }
 209        }
 210
 211        prefixlen = prefix ? strlen(prefix) : 0;
 212        for (i = 0; i < 2; i++) {
 213                const char *p = argv[argc - 2 + i];
 214                if (!strcmp(p, "-"))
 215                        /*
 216                         * stdin should be spelled as "-"; if you have
 217                         * path that is "-", spell it as "./-".
 218                         */
 219                        p = file_from_standard_input;
 220                else if (prefixlen)
 221                        p = xstrdup(prefix_filename(prefix, prefixlen, p));
 222                paths[i] = p;
 223        }
 224        revs->diffopt.skip_stat_unmatch = 1;
 225        if (!revs->diffopt.output_format)
 226                revs->diffopt.output_format = DIFF_FORMAT_PATCH;
 227
 228        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 229
 230        revs->max_count = -2;
 231        diff_setup_done(&revs->diffopt);
 232
 233        if (deprecated_show_diff_q_option_used)
 234                handle_deprecated_show_diff_q(&revs->diffopt);
 235
 236        setup_diff_pager(&revs->diffopt);
 237        DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
 238
 239        if (queue_diff(&revs->diffopt, paths[0], paths[1]))
 240                exit(1);
 241        diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
 242        diffcore_std(&revs->diffopt);
 243        diff_flush(&revs->diffopt);
 244
 245        /*
 246         * The return code for --no-index imitates diff(1):
 247         * 0 = no changes, 1 = changes, else error
 248         */
 249        exit(diff_result_code(&revs->diffopt, 0));
 250}