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