9905cdb0122afecc282273c0e728c3f3b75fc51d
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include <sys/types.h>
   5#include <sys/wait.h>
   6#include "cache.h"
   7#include "diff.h"
   8
   9static char *diff_cmd = "diff -L'k/%s' -L'l/%s'";
  10static char *diff_opts = "-pu";
  11
  12static const char *external_diff(void)
  13{
  14        static char *external_diff_cmd = NULL;
  15        static int done_preparing = 0;
  16
  17        if (done_preparing)
  18                return external_diff_cmd;
  19
  20        /*
  21         * Default values above are meant to match the
  22         * Linux kernel development style.  Examples of
  23         * alternative styles you can specify via environment
  24         * variables are:
  25         *
  26         * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
  27         * GIT_DIFF_OPTS="-c";
  28         */
  29        if (getenv("GIT_EXTERNAL_DIFF"))
  30                external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
  31
  32        /* In case external diff fails... */
  33        diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
  34        diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
  35
  36        done_preparing = 1;
  37        return external_diff_cmd;
  38}
  39
  40/* Help to copy the thing properly quoted for the shell safety.
  41 * any single quote is replaced with '\'', and the caller is
  42 * expected to enclose the result within a single quote pair.
  43 *
  44 * E.g.
  45 *  original     sq_expand     result
  46 *  name     ==> name      ==> 'name'
  47 *  a b      ==> a b       ==> 'a b'
  48 *  a'b      ==> a'\''b    ==> 'a'\''b'
  49 */
  50static char *sq_expand(const char *src)
  51{
  52        static char *buf = NULL;
  53        int cnt, c;
  54        const char *cp;
  55        char *bp;
  56
  57        /* count bytes needed to store the quoted string. */ 
  58        for (cnt = 1, cp = src; *cp; cnt++, cp++)
  59                if (*cp == '\'')
  60                        cnt += 3;
  61
  62        if (! (buf = malloc(cnt)))
  63            return buf;
  64        bp = buf;
  65        while ((c = *src++)) {
  66                if (c != '\'')
  67                        *bp++ = c;
  68                else {
  69                        bp = strcpy(bp, "'\\''");
  70                        bp += 4;
  71                }
  72        }
  73        *bp = 0;
  74        return buf;
  75}
  76
  77static struct diff_tempfile {
  78        const char *name;
  79        char hex[41];
  80        char mode[10];
  81        char tmp_path[50];
  82} diff_temp[2];
  83
  84static void builtin_diff(const char *name,
  85                         struct diff_tempfile *temp)
  86{
  87        static char *diff_arg  = "'%s' '%s'";
  88        const char *name_1_sq = sq_expand(temp[0].name);
  89        const char *name_2_sq = sq_expand(temp[1].name);
  90        const char *name_sq = sq_expand(name);
  91
  92        /* diff_cmd and diff_arg have 4 %s in total which makes
  93         * the sum of these strings 8 bytes larger than required.
  94         * we use 2 spaces around diff-opts, and we need to count
  95         * terminating NUL, so we subtract 5 here.
  96         */
  97        int cmd_size = (strlen(diff_cmd) + 
  98                        strlen(name_sq) * 2 +
  99                        strlen(diff_opts) +
 100                        strlen(diff_arg) +
 101                        strlen(name_1_sq) + strlen(name_2_sq)
 102                        - 5);
 103        char *cmd = malloc(cmd_size);
 104        int next_at = 0;
 105
 106        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 107                            diff_cmd, name_sq, name_sq);
 108        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 109                            " %s ", diff_opts);
 110        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 111                            diff_arg, name_1_sq, name_2_sq);
 112        execlp("/bin/sh","sh", "-c", cmd, NULL);
 113}
 114
 115static void prepare_temp_file(const char *name,
 116                              struct diff_tempfile *temp,
 117                              struct diff_spec *one)
 118{
 119        static unsigned char null_sha1[20] = { 0, };
 120
 121        if (!one->file_valid) {
 122        not_a_valid_file:
 123                temp->name = "/dev/null";
 124                strcpy(temp->hex, ".");
 125                strcpy(temp->mode, ".");
 126                return;
 127        }
 128
 129        if (one->sha1_valid &&
 130            !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
 131                one->sha1_valid = 0;
 132                one->u.name = name;
 133        }
 134
 135        if (!one->sha1_valid) {
 136                struct stat st;
 137                temp->name = one->u.name;
 138                if (stat(temp->name, &st) < 0) {
 139                        if (errno == ENOENT)
 140                                goto not_a_valid_file;
 141                        die("stat(%s): %s", temp->name, strerror(errno));
 142                }
 143                strcpy(temp->hex, ".");
 144                sprintf(temp->mode, "%06o",
 145                        S_IFREG |ce_permissions(st.st_mode));
 146        }
 147        else {
 148                int fd;
 149                void *blob;
 150                char type[20];
 151                unsigned long size;
 152
 153                blob = read_sha1_file(one->u.sha1, type, &size);
 154                if (!blob || strcmp(type, "blob"))
 155                        die("unable to read blob object for %s (%s)",
 156                            name, sha1_to_hex(one->u.sha1));
 157
 158                strcpy(temp->tmp_path, ".diff_XXXXXX");
 159                fd = mkstemp(temp->tmp_path);
 160                if (fd < 0)
 161                        die("unable to create temp-file");
 162                if (write(fd, blob, size) != size)
 163                        die("unable to write temp-file");
 164                close(fd);
 165                free(blob);
 166                temp->name = temp->tmp_path;
 167                strcpy(temp->hex, sha1_to_hex(one->u.sha1));
 168                temp->hex[40] = 0;
 169                sprintf(temp->mode, "%06o", one->mode);
 170        }
 171}
 172
 173static void remove_tempfile(void)
 174{
 175        int i;
 176
 177        for (i = 0; i < 2; i++)
 178                if (diff_temp[i].name == diff_temp[i].tmp_path) {
 179                        unlink(diff_temp[i].name);
 180                        diff_temp[i].name = NULL;
 181                }
 182}
 183
 184/* An external diff command takes:
 185 *
 186 * diff-cmd name infile1 infile1-sha1 infile1-mode \
 187 *               infile2 infile2-sha1 infile2-mode.
 188 *
 189 */
 190void run_external_diff(const char *name,
 191                       struct diff_spec *one,
 192                       struct diff_spec *two)
 193{
 194        struct diff_tempfile *temp = diff_temp;
 195        int pid, status;
 196        static int atexit_asked = 0;
 197
 198        prepare_temp_file(name, &temp[0], one);
 199        prepare_temp_file(name, &temp[1], two);
 200        if (! atexit_asked &&
 201            (temp[0].name == temp[0].tmp_path ||
 202             temp[1].name == temp[1].tmp_path)) {
 203                atexit_asked = 1;
 204                atexit(remove_tempfile);
 205        }
 206
 207        fflush(NULL);
 208        pid = fork();
 209        if (pid < 0)
 210                die("unable to fork");
 211        if (!pid) {
 212                const char *pgm = external_diff();
 213                if (pgm)
 214                        execlp(pgm, pgm,
 215                               name,
 216                               temp[0].name, temp[0].hex, temp[0].mode,
 217                               temp[1].name, temp[1].hex, temp[1].mode,
 218                               NULL);
 219                /*
 220                 * otherwise we use the built-in one.
 221                 */
 222                builtin_diff(name, temp);
 223                exit(0);
 224        }
 225        if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
 226                die("diff program failed");
 227
 228        remove_tempfile();
 229}
 230
 231void show_diff_empty(const struct cache_entry *ce, int reverse)
 232{
 233        struct diff_spec spec[2], *one, *two;
 234
 235        memcpy(spec[0].u.sha1, ce->sha1, 20);
 236        spec[0].mode = ntohl(ce->ce_mode);
 237        spec[0].sha1_valid = spec[0].file_valid = 1;
 238        spec[1].file_valid = 0;
 239
 240        if (reverse) {
 241                one = spec + 1; two = spec;
 242        } else {
 243                one = spec; two = one + 1;
 244        }
 245
 246        run_external_diff(ce->name, one, two);
 247}
 248
 249void show_differences(const struct cache_entry *ce, int reverse) 
 250{
 251        struct diff_spec spec[2], *one, *two;
 252
 253        memcpy(spec[0].u.sha1, ce->sha1, 20);
 254        spec[0].mode = ntohl(ce->ce_mode);
 255        spec[0].sha1_valid = spec[0].file_valid = 1;
 256
 257        spec[1].u.name = ce->name; /* the name we stated */
 258        spec[1].sha1_valid = 0;
 259        spec[1].file_valid = 1;
 260
 261        if (reverse) {
 262                one = spec + 1; two = spec;
 263        } else {
 264                one = spec; two = one + 1;
 265        }
 266
 267        run_external_diff(ce->name, one, two);
 268}