diff-no-index.con commit Merge branch 'pw/t5800-import-race-fix' into maint (1d9fd66)
   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
  35static int get_mode(const char *path, int *mode)
  36{
  37        struct stat st;
  38
  39        if (!path || !strcmp(path, "/dev/null"))
  40                *mode = 0;
  41#ifdef _WIN32
  42        else if (!strcasecmp(path, "nul"))
  43                *mode = 0;
  44#endif
  45        else if (!strcmp(path, "-"))
  46                *mode = create_ce_mode(0666);
  47        else if (lstat(path, &st))
  48                return error("Could not access '%s'", path);
  49        else
  50                *mode = st.st_mode;
  51        return 0;
  52}
  53
  54static int queue_diff(struct diff_options *o,
  55                      const char *name1, const char *name2)
  56{
  57        int mode1 = 0, mode2 = 0;
  58
  59        if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
  60                return -1;
  61
  62        if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
  63                return error("file/directory conflict: %s, %s", name1, name2);
  64
  65        if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
  66                struct strbuf buffer1 = STRBUF_INIT;
  67                struct strbuf buffer2 = STRBUF_INIT;
  68                struct string_list p1 = STRING_LIST_INIT_DUP;
  69                struct string_list p2 = STRING_LIST_INIT_DUP;
  70                int i1, i2, ret = 0;
  71
  72                if (name1 && read_directory(name1, &p1))
  73                        return -1;
  74                if (name2 && read_directory(name2, &p2)) {
  75                        string_list_clear(&p1, 0);
  76                        return -1;
  77                }
  78
  79                if (name1) {
  80                        strbuf_addstr(&buffer1, name1);
  81                        if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
  82                                strbuf_addch(&buffer1, '/');
  83                }
  84
  85                if (name2) {
  86                        strbuf_addstr(&buffer2, name2);
  87                        if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
  88                                strbuf_addch(&buffer2, '/');
  89                }
  90
  91                for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
  92                        const char *n1, *n2;
  93                        int comp;
  94
  95                        if (i1 == p1.nr)
  96                                comp = 1;
  97                        else if (i2 == p2.nr)
  98                                comp = -1;
  99                        else
 100                                comp = strcmp(p1.items[i1].string, p2.items[i2].string);
 101
 102                        if (comp > 0)
 103                                n1 = NULL;
 104                        else {
 105                                strbuf_addstr(&buffer1, p1.items[i1++].string);
 106                                n1 = buffer1.buf;
 107                        }
 108
 109                        if (comp < 0)
 110                                n2 = NULL;
 111                        else {
 112                                strbuf_addstr(&buffer2, p2.items[i2++].string);
 113                                n2 = buffer2.buf;
 114                        }
 115
 116                        ret = queue_diff(o, n1, n2);
 117                }
 118                string_list_clear(&p1, 0);
 119                string_list_clear(&p2, 0);
 120                strbuf_reset(&buffer1);
 121                strbuf_reset(&buffer2);
 122
 123                return ret;
 124        } else {
 125                struct diff_filespec *d1, *d2;
 126
 127                if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
 128                        unsigned tmp;
 129                        const char *tmp_c;
 130                        tmp = mode1; mode1 = mode2; mode2 = tmp;
 131                        tmp_c = name1; name1 = name2; name2 = tmp_c;
 132                }
 133
 134                if (!name1)
 135                        name1 = "/dev/null";
 136                if (!name2)
 137                        name2 = "/dev/null";
 138                d1 = alloc_filespec(name1);
 139                d2 = alloc_filespec(name2);
 140                fill_filespec(d1, null_sha1, mode1);
 141                fill_filespec(d2, null_sha1, mode2);
 142
 143                diff_queue(&diff_queued_diff, d1, d2);
 144                return 0;
 145        }
 146}
 147
 148static int path_outside_repo(const char *path)
 149{
 150        const char *work_tree;
 151        size_t len;
 152
 153        if (!is_absolute_path(path))
 154                return 0;
 155        work_tree = get_git_work_tree();
 156        if (!work_tree)
 157                return 1;
 158        len = strlen(work_tree);
 159        if (strncmp(path, work_tree, len) ||
 160            (path[len] != '\0' && path[len] != '/'))
 161                return 1;
 162        return 0;
 163}
 164
 165void diff_no_index(struct rev_info *revs,
 166                   int argc, const char **argv,
 167                   int nongit, const char *prefix)
 168{
 169        int i;
 170        int no_index = 0;
 171        unsigned options = 0;
 172
 173        /* Were we asked to do --no-index explicitly? */
 174        for (i = 1; i < argc; i++) {
 175                if (!strcmp(argv[i], "--")) {
 176                        i++;
 177                        break;
 178                }
 179                if (!strcmp(argv[i], "--no-index"))
 180                        no_index = 1;
 181                if (argv[i][0] != '-')
 182                        break;
 183        }
 184
 185        if (!no_index && !nongit) {
 186                /*
 187                 * Inside a git repository, without --no-index.  Only
 188                 * when a path outside the repository is given,
 189                 * e.g. "git diff /var/tmp/[12]", or "git diff
 190                 * Makefile /var/tmp/Makefile", allow it to be used as
 191                 * a colourful "diff" replacement.
 192                 */
 193                if ((argc != i + 2) ||
 194                    (!path_outside_repo(argv[i]) &&
 195                     !path_outside_repo(argv[i+1])))
 196                        return;
 197        }
 198        if (argc != i + 2)
 199                usagef("git diff %s <path> <path>",
 200                       no_index ? "--no-index" : "[--no-index]");
 201
 202        diff_setup(&revs->diffopt);
 203        for (i = 1; i < argc - 2; ) {
 204                int j;
 205                if (!strcmp(argv[i], "--no-index"))
 206                        i++;
 207                else if (!strcmp(argv[i], "-q")) {
 208                        options |= DIFF_SILENT_ON_REMOVED;
 209                        i++;
 210                }
 211                else if (!strcmp(argv[i], "--"))
 212                        i++;
 213                else {
 214                        j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
 215                        if (!j)
 216                                die("invalid diff option/value: %s", argv[i]);
 217                        i += j;
 218                }
 219        }
 220
 221        /*
 222         * If the user asked for our exit code then don't start a
 223         * pager or we would end up reporting its exit code instead.
 224         */
 225        if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
 226                setup_pager();
 227
 228        if (prefix) {
 229                int len = strlen(prefix);
 230                const char *paths[3];
 231                memset(paths, 0, sizeof(paths));
 232
 233                for (i = 0; i < 2; i++) {
 234                        const char *p = argv[argc - 2 + i];
 235                        /*
 236                         * stdin should be spelled as '-'; if you have
 237                         * path that is '-', spell it as ./-.
 238                         */
 239                        p = (strcmp(p, "-")
 240                             ? xstrdup(prefix_filename(prefix, len, p))
 241                             : p);
 242                        paths[i] = p;
 243                }
 244                diff_tree_setup_paths(paths, &revs->diffopt);
 245        }
 246        else
 247                diff_tree_setup_paths(argv + argc - 2, &revs->diffopt);
 248        revs->diffopt.skip_stat_unmatch = 1;
 249        if (!revs->diffopt.output_format)
 250                revs->diffopt.output_format = DIFF_FORMAT_PATCH;
 251
 252        DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
 253        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 254
 255        revs->max_count = -2;
 256        if (diff_setup_done(&revs->diffopt) < 0)
 257                die("diff_setup_done failed");
 258
 259        if (queue_diff(&revs->diffopt, revs->diffopt.pathspec.raw[0],
 260                       revs->diffopt.pathspec.raw[1]))
 261                exit(1);
 262        diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
 263        diffcore_std(&revs->diffopt);
 264        diff_flush(&revs->diffopt);
 265
 266        /*
 267         * The return code for --no-index imitates diff(1):
 268         * 0 = no changes, 1 = changes, else error
 269         */
 270        exit(revs->diffopt.found_changes);
 271}