show-diff.con commit [PATCH] Do not let rsync obliterate .git/object symbolic link. (0ffb0bc)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8static char *diff_cmd = "diff -L 'a/%s' -L 'b/%s' ";
   9static char *diff_opts = "-p -u";
  10static char *diff_arg_forward  = " - '%s'";
  11static char *diff_arg_reverse  = " '%s' -";
  12
  13static void prepare_diff_cmd(void)
  14{
  15        /*
  16         * Default values above are meant to match the
  17         * Linux kernel development style.  Examples of
  18         * alternative styles you can specify via environment
  19         * variables are:
  20         *
  21         * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
  22         * GIT_DIFF_OPTS="-c";
  23         */
  24        diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
  25        diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
  26}
  27
  28/* Help to copy the thing properly quoted for the shell safety.
  29 * any single quote is replaced with '\'', and the caller is
  30 * expected to enclose the result within a single quote pair.
  31 *
  32 * E.g.
  33 *  original     sq_expand     result
  34 *  name     ==> name      ==> 'name'
  35 *  a b      ==> a b       ==> 'a b'
  36 *  a'b      ==> a'\''b    ==> 'a'\''b'
  37 *
  38 * NOTE! The returned memory belongs to this function so
  39 * do not free it.
  40 */
  41static char *sq_expand(char *src)
  42{
  43        static char *buf = NULL;
  44        int cnt, c;
  45        char *cp;
  46
  47        /* count bytes needed to store the quoted string. */ 
  48        for (cnt = 1, cp = src; *cp; cnt++, cp++)
  49                if (*cp == '\'')
  50                        cnt += 3;
  51
  52        if (! (buf = malloc(cnt)))
  53            return buf;
  54        cp = buf;
  55        while ((c = *src++)) {
  56                if (c != '\'')
  57                        *cp++ = c;
  58                else {
  59                        cp = strcpy(cp, "'\\''");
  60                        cp += 4;
  61                }
  62        }
  63        *cp = 0;
  64        return buf;
  65}
  66
  67static void show_differences(char *name, char *label, void *old_contents,
  68                             unsigned long long old_size, int reverse)
  69{
  70        FILE *f;
  71        char *name_sq = sq_expand(name);
  72        char *label_sq = (name != label) ? sq_expand(label) : name_sq;
  73        char *diff_arg = reverse ? diff_arg_reverse : diff_arg_forward;
  74        int cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +
  75                strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);
  76        char *cmd = malloc(cmd_size);
  77        int next_at;
  78
  79        fflush(stdout);
  80        next_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);
  81        next_at += snprintf(cmd+next_at, cmd_size-next_at, "%s", diff_opts);
  82        next_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);
  83        f = popen(cmd, "w");
  84        if (old_size)
  85                fwrite(old_contents, old_size, 1, f);
  86        pclose(f);
  87        if (label_sq != name_sq)
  88                free(label_sq);
  89        free(name_sq);
  90        free(cmd);
  91}
  92
  93static void show_diff_empty(struct cache_entry *ce, int reverse)
  94{
  95        char *old;
  96        unsigned long int size;
  97        unsigned char type[20];
  98
  99        old = read_sha1_file(ce->sha1, type, &size);
 100        if (! old) {
 101                error("unable to read blob object for %s (%s)", ce->name,
 102                      sha1_to_hex(ce->sha1));
 103                return;
 104        }
 105        show_differences("/dev/null", ce->name, old, size, reverse);
 106}
 107
 108static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
 109
 110static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
 111{
 112        int i;
 113        int namelen = ce_namelen(ce);
 114        for (i = 0; i < cnt; i++) {
 115                int speclen = strlen(spec[i]);
 116                if (! strncmp(spec[i], ce->name, speclen) &&
 117                    speclen <= namelen &&
 118                    (ce->name[speclen] == 0 ||
 119                     ce->name[speclen] == '/'))
 120                        return 1;
 121        }
 122        return 0;
 123}
 124
 125int main(int argc, char **argv)
 126{
 127        int silent = 0;
 128        int silent_on_nonexisting_files = 0;
 129        int machine_readable = 0;
 130        int reverse = 0;
 131        int entries = read_cache();
 132        int i;
 133
 134        while (1 < argc && argv[1][0] == '-') {
 135                if  (!strcmp(argv[1], "-R"))
 136                        reverse = 1;
 137                else if (!strcmp(argv[1], "-s"))
 138                        silent_on_nonexisting_files = silent = 1;
 139                else if (!strcmp(argv[1], "-q"))
 140                        silent_on_nonexisting_files = 1;
 141                else if (!strcmp(argv[1], "-z"))
 142                        machine_readable = 1;
 143                else
 144                        usage(show_diff_usage);
 145                argv++; argc--;
 146        }
 147
 148        /* At this point, if argc == 1, then we are doing everything.
 149         * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
 150         */
 151        if (entries < 0) {
 152                perror("read_cache");
 153                exit(1);
 154        }
 155        prepare_diff_cmd();
 156        for (i = 0; i < entries; i++) {
 157                struct stat st;
 158                struct cache_entry *ce = active_cache[i];
 159                int changed;
 160                unsigned long size;
 161                char type[20];
 162                void *old;
 163
 164                if (1 < argc &&
 165                    ! matches_pathspec(ce, argv+1, argc-1))
 166                        continue;
 167
 168                if (ce_stage(ce)) {
 169                        if (machine_readable)
 170                                printf("U %s%c", ce->name, 0);
 171                        else
 172                                printf("%s: Unmerged\n",
 173                                       ce->name);
 174                        while (i < entries &&
 175                               !strcmp(ce->name, active_cache[i]->name))
 176                                i++;
 177                        i--; /* compensate for loop control increments */
 178                        continue;
 179                }
 180 
 181                if (stat(ce->name, &st) < 0) {
 182                        if (errno == ENOENT && silent_on_nonexisting_files)
 183                                continue;
 184                        if (machine_readable)
 185                                printf("X %s%c", ce->name, 0);
 186                        else {
 187                                printf("%s: %s\n", ce->name, strerror(errno));
 188                                if (errno == ENOENT)
 189                                        show_diff_empty(ce, reverse);
 190                        }
 191                        continue;
 192                }
 193                changed = cache_match_stat(ce, &st);
 194                if (!changed)
 195                        continue;
 196                if (!machine_readable)
 197                        printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
 198                else {
 199                        printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
 200                        continue;
 201                }
 202                if (silent)
 203                        continue;
 204
 205                old = read_sha1_file(ce->sha1, type, &size);
 206                if (! old)
 207                        error("unable to read blob object for %s (%s)",
 208                              ce->name, sha1_to_hex(ce->sha1));
 209                else
 210                        show_differences(ce->name, ce->name, old, size,
 211                                         reverse);
 212                free(old);
 213        }
 214        return 0;
 215}