builtin / difftool.con commit Git 2.12.5 (9752ad0)
   1/*
   2 * "git difftool" builtin command
   3 *
   4 * This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
   5 * git-difftool--helper script.
   6 *
   7 * This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
   8 * The GIT_DIFF* variables are exported for use by git-difftool--helper.
   9 *
  10 * Any arguments that are unknown to this script are forwarded to 'git diff'.
  11 *
  12 * Copyright (C) 2016 Johannes Schindelin
  13 */
  14#include "cache.h"
  15#include "builtin.h"
  16#include "run-command.h"
  17#include "exec_cmd.h"
  18#include "parse-options.h"
  19#include "argv-array.h"
  20#include "strbuf.h"
  21#include "lockfile.h"
  22#include "dir.h"
  23
  24static char *diff_gui_tool;
  25static int trust_exit_code;
  26
  27static const char *const builtin_difftool_usage[] = {
  28        N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
  29        NULL
  30};
  31
  32static int difftool_config(const char *var, const char *value, void *cb)
  33{
  34        if (!strcmp(var, "diff.guitool")) {
  35                diff_gui_tool = xstrdup(value);
  36                return 0;
  37        }
  38
  39        if (!strcmp(var, "difftool.trustexitcode")) {
  40                trust_exit_code = git_config_bool(var, value);
  41                return 0;
  42        }
  43
  44        return git_default_config(var, value, cb);
  45}
  46
  47static int print_tool_help(void)
  48{
  49        const char *argv[] = { "mergetool", "--tool-help=diff", NULL };
  50        return run_command_v_opt(argv, RUN_GIT_CMD);
  51}
  52
  53static int parse_index_info(char *p, int *mode1, int *mode2,
  54                            struct object_id *oid1, struct object_id *oid2,
  55                            char *status)
  56{
  57        if (*p != ':')
  58                return error("expected ':', got '%c'", *p);
  59        *mode1 = (int)strtol(p + 1, &p, 8);
  60        if (*p != ' ')
  61                return error("expected ' ', got '%c'", *p);
  62        *mode2 = (int)strtol(p + 1, &p, 8);
  63        if (*p != ' ')
  64                return error("expected ' ', got '%c'", *p);
  65        if (get_oid_hex(++p, oid1))
  66                return error("expected object ID, got '%s'", p + 1);
  67        p += GIT_SHA1_HEXSZ;
  68        if (*p != ' ')
  69                return error("expected ' ', got '%c'", *p);
  70        if (get_oid_hex(++p, oid2))
  71                return error("expected object ID, got '%s'", p + 1);
  72        p += GIT_SHA1_HEXSZ;
  73        if (*p != ' ')
  74                return error("expected ' ', got '%c'", *p);
  75        *status = *++p;
  76        if (!*status)
  77                return error("missing status");
  78        if (p[1] && !isdigit(p[1]))
  79                return error("unexpected trailer: '%s'", p + 1);
  80        return 0;
  81}
  82
  83/*
  84 * Remove any trailing slash from $workdir
  85 * before starting to avoid double slashes in symlink targets.
  86 */
  87static void add_path(struct strbuf *buf, size_t base_len, const char *path)
  88{
  89        strbuf_setlen(buf, base_len);
  90        if (buf->len && buf->buf[buf->len - 1] != '/')
  91                strbuf_addch(buf, '/');
  92        strbuf_addstr(buf, path);
  93}
  94
  95/*
  96 * Determine whether we can simply reuse the file in the worktree.
  97 */
  98static int use_wt_file(const char *workdir, const char *name,
  99                       struct object_id *oid)
 100{
 101        struct strbuf buf = STRBUF_INIT;
 102        struct stat st;
 103        int use = 0;
 104
 105        strbuf_addstr(&buf, workdir);
 106        add_path(&buf, buf.len, name);
 107
 108        if (!lstat(buf.buf, &st) && !S_ISLNK(st.st_mode)) {
 109                struct object_id wt_oid;
 110                int fd = open(buf.buf, O_RDONLY);
 111
 112                if (fd >= 0 &&
 113                    !index_fd(wt_oid.hash, fd, &st, OBJ_BLOB, name, 0)) {
 114                        if (is_null_oid(oid)) {
 115                                oidcpy(oid, &wt_oid);
 116                                use = 1;
 117                        } else if (!oidcmp(oid, &wt_oid))
 118                                use = 1;
 119                }
 120        }
 121
 122        strbuf_release(&buf);
 123
 124        return use;
 125}
 126
 127struct working_tree_entry {
 128        struct hashmap_entry entry;
 129        char path[FLEX_ARRAY];
 130};
 131
 132static int working_tree_entry_cmp(struct working_tree_entry *a,
 133                                  struct working_tree_entry *b, void *keydata)
 134{
 135        return strcmp(a->path, b->path);
 136}
 137
 138/*
 139 * The `left` and `right` entries hold paths for the symlinks hashmap,
 140 * and a SHA-1 surrounded by brief text for submodules.
 141 */
 142struct pair_entry {
 143        struct hashmap_entry entry;
 144        char left[PATH_MAX], right[PATH_MAX];
 145        const char path[FLEX_ARRAY];
 146};
 147
 148static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
 149{
 150        return strcmp(a->path, b->path);
 151}
 152
 153static void add_left_or_right(struct hashmap *map, const char *path,
 154                              const char *content, int is_right)
 155{
 156        struct pair_entry *e, *existing;
 157
 158        FLEX_ALLOC_STR(e, path, path);
 159        hashmap_entry_init(e, strhash(path));
 160        existing = hashmap_get(map, e, NULL);
 161        if (existing) {
 162                free(e);
 163                e = existing;
 164        } else {
 165                e->left[0] = e->right[0] = '\0';
 166                hashmap_add(map, e);
 167        }
 168        strlcpy(is_right ? e->right : e->left, content, PATH_MAX);
 169}
 170
 171struct path_entry {
 172        struct hashmap_entry entry;
 173        char path[FLEX_ARRAY];
 174};
 175
 176static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
 177{
 178        return strcmp(a->path, key ? key : b->path);
 179}
 180
 181static void changed_files(struct hashmap *result, const char *index_path,
 182                          const char *workdir)
 183{
 184        struct child_process update_index = CHILD_PROCESS_INIT;
 185        struct child_process diff_files = CHILD_PROCESS_INIT;
 186        struct strbuf index_env = STRBUF_INIT, buf = STRBUF_INIT;
 187        const char *git_dir = absolute_path(get_git_dir()), *env[] = {
 188                NULL, NULL
 189        };
 190        FILE *fp;
 191
 192        strbuf_addf(&index_env, "GIT_INDEX_FILE=%s", index_path);
 193        env[0] = index_env.buf;
 194
 195        argv_array_pushl(&update_index.args,
 196                         "--git-dir", git_dir, "--work-tree", workdir,
 197                         "update-index", "--really-refresh", "-q",
 198                         "--unmerged", NULL);
 199        update_index.no_stdin = 1;
 200        update_index.no_stdout = 1;
 201        update_index.no_stderr = 1;
 202        update_index.git_cmd = 1;
 203        update_index.use_shell = 0;
 204        update_index.clean_on_exit = 1;
 205        update_index.dir = workdir;
 206        update_index.env = env;
 207        /* Ignore any errors of update-index */
 208        run_command(&update_index);
 209
 210        argv_array_pushl(&diff_files.args,
 211                         "--git-dir", git_dir, "--work-tree", workdir,
 212                         "diff-files", "--name-only", "-z", NULL);
 213        diff_files.no_stdin = 1;
 214        diff_files.git_cmd = 1;
 215        diff_files.use_shell = 0;
 216        diff_files.clean_on_exit = 1;
 217        diff_files.out = -1;
 218        diff_files.dir = workdir;
 219        diff_files.env = env;
 220        if (start_command(&diff_files))
 221                die("could not obtain raw diff");
 222        fp = xfdopen(diff_files.out, "r");
 223        while (!strbuf_getline_nul(&buf, fp)) {
 224                struct path_entry *entry;
 225                FLEX_ALLOC_STR(entry, path, buf.buf);
 226                hashmap_entry_init(entry, strhash(buf.buf));
 227                hashmap_add(result, entry);
 228        }
 229        if (finish_command(&diff_files))
 230                die("diff-files did not exit properly");
 231        strbuf_release(&index_env);
 232        strbuf_release(&buf);
 233}
 234
 235static NORETURN void exit_cleanup(const char *tmpdir, int exit_code)
 236{
 237        struct strbuf buf = STRBUF_INIT;
 238        strbuf_addstr(&buf, tmpdir);
 239        remove_dir_recursively(&buf, 0);
 240        if (exit_code)
 241                warning(_("failed: %d"), exit_code);
 242        exit(exit_code);
 243}
 244
 245static int ensure_leading_directories(char *path)
 246{
 247        switch (safe_create_leading_directories(path)) {
 248                case SCLD_OK:
 249                case SCLD_EXISTS:
 250                        return 0;
 251                default:
 252                        return error(_("could not create leading directories "
 253                                       "of '%s'"), path);
 254        }
 255}
 256
 257static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 258                        int argc, const char **argv)
 259{
 260        char tmpdir[PATH_MAX];
 261        struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
 262        struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
 263        struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
 264        struct strbuf wtdir = STRBUF_INIT;
 265        size_t ldir_len, rdir_len, wtdir_len;
 266        struct cache_entry *ce = xcalloc(1, sizeof(ce) + PATH_MAX + 1);
 267        const char *workdir, *tmp;
 268        int ret = 0, i;
 269        FILE *fp;
 270        struct hashmap working_tree_dups, submodules, symlinks2;
 271        struct hashmap_iter iter;
 272        struct pair_entry *entry;
 273        enum object_type type;
 274        unsigned long size;
 275        struct index_state wtindex;
 276        struct checkout lstate, rstate;
 277        int rc, flags = RUN_GIT_CMD, err = 0;
 278        struct child_process child = CHILD_PROCESS_INIT;
 279        const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL };
 280        struct hashmap wt_modified, tmp_modified;
 281        int indices_loaded = 0;
 282
 283        workdir = get_git_work_tree();
 284
 285        /* Setup temp directories */
 286        tmp = getenv("TMPDIR");
 287        xsnprintf(tmpdir, sizeof(tmpdir), "%s/git-difftool.XXXXXX", tmp ? tmp : "/tmp");
 288        if (!mkdtemp(tmpdir))
 289                return error("could not create '%s'", tmpdir);
 290        strbuf_addf(&ldir, "%s/left/", tmpdir);
 291        strbuf_addf(&rdir, "%s/right/", tmpdir);
 292        strbuf_addstr(&wtdir, workdir);
 293        if (!wtdir.len || !is_dir_sep(wtdir.buf[wtdir.len - 1]))
 294                strbuf_addch(&wtdir, '/');
 295        mkdir(ldir.buf, 0700);
 296        mkdir(rdir.buf, 0700);
 297
 298        memset(&wtindex, 0, sizeof(wtindex));
 299
 300        memset(&lstate, 0, sizeof(lstate));
 301        lstate.base_dir = ldir.buf;
 302        lstate.base_dir_len = ldir.len;
 303        lstate.force = 1;
 304        memset(&rstate, 0, sizeof(rstate));
 305        rstate.base_dir = rdir.buf;
 306        rstate.base_dir_len = rdir.len;
 307        rstate.force = 1;
 308
 309        ldir_len = ldir.len;
 310        rdir_len = rdir.len;
 311        wtdir_len = wtdir.len;
 312
 313        hashmap_init(&working_tree_dups,
 314                     (hashmap_cmp_fn)working_tree_entry_cmp, 0);
 315        hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
 316        hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
 317
 318        child.no_stdin = 1;
 319        child.git_cmd = 1;
 320        child.use_shell = 0;
 321        child.clean_on_exit = 1;
 322        child.dir = prefix;
 323        child.out = -1;
 324        argv_array_pushl(&child.args, "diff", "--raw", "--no-abbrev", "-z",
 325                         NULL);
 326        for (i = 0; i < argc; i++)
 327                argv_array_push(&child.args, argv[i]);
 328        if (start_command(&child))
 329                die("could not obtain raw diff");
 330        fp = xfdopen(child.out, "r");
 331
 332        /* Build index info for left and right sides of the diff */
 333        i = 0;
 334        while (!strbuf_getline_nul(&info, fp)) {
 335                int lmode, rmode;
 336                struct object_id loid, roid;
 337                char status;
 338                const char *src_path, *dst_path;
 339                size_t src_path_len, dst_path_len;
 340
 341                if (starts_with(info.buf, "::"))
 342                        die(N_("combined diff formats('-c' and '--cc') are "
 343                               "not supported in\n"
 344                               "directory diff mode('-d' and '--dir-diff')."));
 345
 346                if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
 347                                     &status))
 348                        break;
 349                if (strbuf_getline_nul(&lpath, fp))
 350                        break;
 351                src_path = lpath.buf;
 352                src_path_len = lpath.len;
 353
 354                i++;
 355                if (status != 'C' && status != 'R') {
 356                        dst_path = src_path;
 357                        dst_path_len = src_path_len;
 358                } else {
 359                        if (strbuf_getline_nul(&rpath, fp))
 360                                break;
 361                        dst_path = rpath.buf;
 362                        dst_path_len = rpath.len;
 363                }
 364
 365                if (S_ISGITLINK(lmode) || S_ISGITLINK(rmode)) {
 366                        strbuf_reset(&buf);
 367                        strbuf_addf(&buf, "Subproject commit %s",
 368                                    oid_to_hex(&loid));
 369                        add_left_or_right(&submodules, src_path, buf.buf, 0);
 370                        strbuf_reset(&buf);
 371                        strbuf_addf(&buf, "Subproject commit %s",
 372                                    oid_to_hex(&roid));
 373                        if (!oidcmp(&loid, &roid))
 374                                strbuf_addstr(&buf, "-dirty");
 375                        add_left_or_right(&submodules, dst_path, buf.buf, 1);
 376                        continue;
 377                }
 378
 379                if (S_ISLNK(lmode)) {
 380                        char *content = read_sha1_file(loid.hash, &type, &size);
 381                        add_left_or_right(&symlinks2, src_path, content, 0);
 382                        free(content);
 383                }
 384
 385                if (S_ISLNK(rmode)) {
 386                        char *content = read_sha1_file(roid.hash, &type, &size);
 387                        add_left_or_right(&symlinks2, dst_path, content, 1);
 388                        free(content);
 389                }
 390
 391                if (lmode && status != 'C') {
 392                        ce->ce_mode = lmode;
 393                        oidcpy(&ce->oid, &loid);
 394                        strcpy(ce->name, src_path);
 395                        ce->ce_namelen = src_path_len;
 396                        if (checkout_entry(ce, &lstate, NULL))
 397                                return error("could not write '%s'", src_path);
 398                }
 399
 400                if (rmode) {
 401                        struct working_tree_entry *entry;
 402
 403                        /* Avoid duplicate working_tree entries */
 404                        FLEX_ALLOC_STR(entry, path, dst_path);
 405                        hashmap_entry_init(entry, strhash(dst_path));
 406                        if (hashmap_get(&working_tree_dups, entry, NULL)) {
 407                                free(entry);
 408                                continue;
 409                        }
 410                        hashmap_add(&working_tree_dups, entry);
 411
 412                        if (!use_wt_file(workdir, dst_path, &roid)) {
 413                                ce->ce_mode = rmode;
 414                                oidcpy(&ce->oid, &roid);
 415                                strcpy(ce->name, dst_path);
 416                                ce->ce_namelen = dst_path_len;
 417                                if (checkout_entry(ce, &rstate, NULL))
 418                                        return error("could not write '%s'",
 419                                                     dst_path);
 420                        } else if (!is_null_oid(&roid)) {
 421                                /*
 422                                 * Changes in the working tree need special
 423                                 * treatment since they are not part of the
 424                                 * index.
 425                                 */
 426                                struct cache_entry *ce2 =
 427                                        make_cache_entry(rmode, roid.hash,
 428                                                         dst_path, 0, 0);
 429
 430                                add_index_entry(&wtindex, ce2,
 431                                                ADD_CACHE_JUST_APPEND);
 432
 433                                add_path(&rdir, rdir_len, dst_path);
 434                                if (ensure_leading_directories(rdir.buf))
 435                                        return error("could not create "
 436                                                     "directory for '%s'",
 437                                                     dst_path);
 438                                add_path(&wtdir, wtdir_len, dst_path);
 439                                if (symlinks) {
 440                                        if (symlink(wtdir.buf, rdir.buf)) {
 441                                                ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
 442                                                goto finish;
 443                                        }
 444                                } else {
 445                                        struct stat st;
 446                                        if (stat(wtdir.buf, &st))
 447                                                st.st_mode = 0644;
 448                                        if (copy_file(rdir.buf, wtdir.buf,
 449                                                      st.st_mode)) {
 450                                                ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
 451                                                goto finish;
 452                                        }
 453                                }
 454                        }
 455                }
 456        }
 457
 458        if (finish_command(&child)) {
 459                ret = error("error occurred running diff --raw");
 460                goto finish;
 461        }
 462
 463        if (!i)
 464                return 0;
 465
 466        /*
 467         * Changes to submodules require special treatment.This loop writes a
 468         * temporary file to both the left and right directories to show the
 469         * change in the recorded SHA1 for the submodule.
 470         */
 471        hashmap_iter_init(&submodules, &iter);
 472        while ((entry = hashmap_iter_next(&iter))) {
 473                if (*entry->left) {
 474                        add_path(&ldir, ldir_len, entry->path);
 475                        ensure_leading_directories(ldir.buf);
 476                        write_file(ldir.buf, "%s", entry->left);
 477                }
 478                if (*entry->right) {
 479                        add_path(&rdir, rdir_len, entry->path);
 480                        ensure_leading_directories(rdir.buf);
 481                        write_file(rdir.buf, "%s", entry->right);
 482                }
 483        }
 484
 485        /*
 486         * Symbolic links require special treatment.The standard "git diff"
 487         * shows only the link itself, not the contents of the link target.
 488         * This loop replicates that behavior.
 489         */
 490        hashmap_iter_init(&symlinks2, &iter);
 491        while ((entry = hashmap_iter_next(&iter))) {
 492                if (*entry->left) {
 493                        add_path(&ldir, ldir_len, entry->path);
 494                        ensure_leading_directories(ldir.buf);
 495                        write_file(ldir.buf, "%s", entry->left);
 496                }
 497                if (*entry->right) {
 498                        add_path(&rdir, rdir_len, entry->path);
 499                        ensure_leading_directories(rdir.buf);
 500                        write_file(rdir.buf, "%s", entry->right);
 501                }
 502        }
 503
 504        strbuf_release(&buf);
 505
 506        strbuf_setlen(&ldir, ldir_len);
 507        helper_argv[1] = ldir.buf;
 508        strbuf_setlen(&rdir, rdir_len);
 509        helper_argv[2] = rdir.buf;
 510
 511        if (extcmd) {
 512                helper_argv[0] = extcmd;
 513                flags = 0;
 514        } else
 515                setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
 516        rc = run_command_v_opt(helper_argv, flags);
 517
 518        /*
 519         * If the diff includes working copy files and those
 520         * files were modified during the diff, then the changes
 521         * should be copied back to the working tree.
 522         * Do not copy back files when symlinks are used and the
 523         * external tool did not replace the original link with a file.
 524         *
 525         * These hashes are loaded lazily since they aren't needed
 526         * in the common case of --symlinks and the difftool updating
 527         * files through the symlink.
 528         */
 529        hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
 530                     wtindex.cache_nr);
 531        hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
 532                     wtindex.cache_nr);
 533
 534        for (i = 0; i < wtindex.cache_nr; i++) {
 535                struct hashmap_entry dummy;
 536                const char *name = wtindex.cache[i]->name;
 537                struct stat st;
 538
 539                add_path(&rdir, rdir_len, name);
 540                if (lstat(rdir.buf, &st))
 541                        continue;
 542
 543                if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
 544                        continue;
 545
 546                if (!indices_loaded) {
 547                        static struct lock_file lock;
 548                        strbuf_reset(&buf);
 549                        strbuf_addf(&buf, "%s/wtindex", tmpdir);
 550                        if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
 551                            write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
 552                                ret = error("could not write %s", buf.buf);
 553                                rollback_lock_file(&lock);
 554                                goto finish;
 555                        }
 556                        changed_files(&wt_modified, buf.buf, workdir);
 557                        strbuf_setlen(&rdir, rdir_len);
 558                        changed_files(&tmp_modified, buf.buf, rdir.buf);
 559                        add_path(&rdir, rdir_len, name);
 560                        indices_loaded = 1;
 561                }
 562
 563                hashmap_entry_init(&dummy, strhash(name));
 564                if (hashmap_get(&tmp_modified, &dummy, name)) {
 565                        add_path(&wtdir, wtdir_len, name);
 566                        if (hashmap_get(&wt_modified, &dummy, name)) {
 567                                warning(_("both files modified: '%s' and '%s'."),
 568                                        wtdir.buf, rdir.buf);
 569                                warning(_("working tree file has been left."));
 570                                warning("%s", "");
 571                                err = 1;
 572                        } else if (unlink(wtdir.buf) ||
 573                                   copy_file(wtdir.buf, rdir.buf, st.st_mode))
 574                                warning_errno(_("could not copy '%s' to '%s'"),
 575                                              rdir.buf, wtdir.buf);
 576                }
 577        }
 578
 579        if (err) {
 580                warning(_("temporary files exist in '%s'."), tmpdir);
 581                warning(_("you may want to cleanup or recover these."));
 582                exit(1);
 583        } else
 584                exit_cleanup(tmpdir, rc);
 585
 586finish:
 587        free(ce);
 588        strbuf_release(&ldir);
 589        strbuf_release(&rdir);
 590        strbuf_release(&wtdir);
 591        strbuf_release(&buf);
 592
 593        return ret;
 594}
 595
 596static int run_file_diff(int prompt, const char *prefix,
 597                         int argc, const char **argv)
 598{
 599        struct argv_array args = ARGV_ARRAY_INIT;
 600        const char *env[] = {
 601                "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
 602                NULL
 603        };
 604        int ret = 0, i;
 605
 606        if (prompt > 0)
 607                env[2] = "GIT_DIFFTOOL_PROMPT=true";
 608        else if (!prompt)
 609                env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";
 610
 611
 612        argv_array_push(&args, "diff");
 613        for (i = 0; i < argc; i++)
 614                argv_array_push(&args, argv[i]);
 615        ret = run_command_v_opt_cd_env(args.argv, RUN_GIT_CMD, prefix, env);
 616        exit(ret);
 617}
 618
 619int cmd_difftool(int argc, const char **argv, const char *prefix)
 620{
 621        int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
 622            tool_help = 0;
 623        static char *difftool_cmd = NULL, *extcmd = NULL;
 624        struct option builtin_difftool_options[] = {
 625                OPT_BOOL('g', "gui", &use_gui_tool,
 626                         N_("use `diff.guitool` instead of `diff.tool`")),
 627                OPT_BOOL('d', "dir-diff", &dir_diff,
 628                         N_("perform a full-directory diff")),
 629                { OPTION_SET_INT, 'y', "no-prompt", &prompt, NULL,
 630                        N_("do not prompt before launching a diff tool"),
 631                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
 632                { OPTION_SET_INT, 0, "prompt", &prompt, NULL, NULL,
 633                        PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_HIDDEN,
 634                        NULL, 1 },
 635                OPT_BOOL(0, "symlinks", &symlinks,
 636                         N_("use symlinks in dir-diff mode")),
 637                OPT_STRING('t', "tool", &difftool_cmd, N_("<tool>"),
 638                           N_("use the specified diff tool")),
 639                OPT_BOOL(0, "tool-help", &tool_help,
 640                         N_("print a list of diff tools that may be used with "
 641                            "`--tool`")),
 642                OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
 643                         N_("make 'git-difftool' exit when an invoked diff "
 644                            "tool returns a non - zero exit code")),
 645                OPT_STRING('x', "extcmd", &extcmd, N_("<command>"),
 646                           N_("specify a custom command for viewing diffs")),
 647                OPT_END()
 648        };
 649
 650        git_config(difftool_config, NULL);
 651        symlinks = has_symlinks;
 652
 653        argc = parse_options(argc, argv, prefix, builtin_difftool_options,
 654                             builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
 655                             PARSE_OPT_KEEP_DASHDASH);
 656
 657        if (tool_help)
 658                return print_tool_help();
 659
 660        /* NEEDSWORK: once we no longer spawn anything, remove this */
 661        setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
 662        setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
 663
 664        if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
 665                setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
 666        else if (difftool_cmd) {
 667                if (*difftool_cmd)
 668                        setenv("GIT_DIFF_TOOL", difftool_cmd, 1);
 669                else
 670                        die(_("no <tool> given for --tool=<tool>"));
 671        }
 672
 673        if (extcmd) {
 674                if (*extcmd)
 675                        setenv("GIT_DIFFTOOL_EXTCMD", extcmd, 1);
 676                else
 677                        die(_("no <cmd> given for --extcmd=<cmd>"));
 678        }
 679
 680        setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
 681               trust_exit_code ? "true" : "false", 1);
 682
 683        /*
 684         * In directory diff mode, 'git-difftool--helper' is called once
 685         * to compare the a / b directories. In file diff mode, 'git diff'
 686         * will invoke a separate instance of 'git-difftool--helper' for
 687         * each file that changed.
 688         */
 689        if (dir_diff)
 690                return run_dir_diff(extcmd, symlinks, prefix, argc, argv);
 691        return run_file_diff(prompt, prefix, argc, argv);
 692}