Merge branch 'jk/ref-paranoia'
[git.git] / builtin / difftool.c
blob210da03908c7ccd1cb041d8c3af7c09ba3c0baed
1 /*
2 * "git difftool" builtin command
4 * This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
5 * git-difftool--helper script.
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.
10 * Any arguments that are unknown to this script are forwarded to 'git diff'.
12 * Copyright (C) 2016 Johannes Schindelin
14 #define USE_THE_INDEX_COMPATIBILITY_MACROS
15 #include "cache.h"
16 #include "config.h"
17 #include "builtin.h"
18 #include "run-command.h"
19 #include "exec-cmd.h"
20 #include "parse-options.h"
21 #include "strvec.h"
22 #include "strbuf.h"
23 #include "lockfile.h"
24 #include "object-store.h"
25 #include "dir.h"
26 #include "entry.h"
28 static int trust_exit_code;
30 static const char *const builtin_difftool_usage[] = {
31 N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
32 NULL
35 static int difftool_config(const char *var, const char *value, void *cb)
37 if (!strcmp(var, "difftool.trustexitcode")) {
38 trust_exit_code = git_config_bool(var, value);
39 return 0;
42 return git_default_config(var, value, cb);
45 static int print_tool_help(void)
47 const char *argv[] = { "mergetool", "--tool-help=diff", NULL };
48 return run_command_v_opt(argv, RUN_GIT_CMD);
51 static int parse_index_info(char *p, int *mode1, int *mode2,
52 struct object_id *oid1, struct object_id *oid2,
53 char *status)
55 if (*p != ':')
56 return error("expected ':', got '%c'", *p);
57 *mode1 = (int)strtol(p + 1, &p, 8);
58 if (*p != ' ')
59 return error("expected ' ', got '%c'", *p);
60 *mode2 = (int)strtol(p + 1, &p, 8);
61 if (*p != ' ')
62 return error("expected ' ', got '%c'", *p);
63 if (parse_oid_hex(++p, oid1, (const char **)&p))
64 return error("expected object ID, got '%s'", p);
65 if (*p != ' ')
66 return error("expected ' ', got '%c'", *p);
67 if (parse_oid_hex(++p, oid2, (const char **)&p))
68 return error("expected object ID, got '%s'", p);
69 if (*p != ' ')
70 return error("expected ' ', got '%c'", *p);
71 *status = *++p;
72 if (!*status)
73 return error("missing status");
74 if (p[1] && !isdigit(p[1]))
75 return error("unexpected trailer: '%s'", p + 1);
76 return 0;
80 * Remove any trailing slash from $workdir
81 * before starting to avoid double slashes in symlink targets.
83 static void add_path(struct strbuf *buf, size_t base_len, const char *path)
85 strbuf_setlen(buf, base_len);
86 if (buf->len && buf->buf[buf->len - 1] != '/')
87 strbuf_addch(buf, '/');
88 strbuf_addstr(buf, path);
92 * Determine whether we can simply reuse the file in the worktree.
94 static int use_wt_file(const char *workdir, const char *name,
95 struct object_id *oid)
97 struct strbuf buf = STRBUF_INIT;
98 struct stat st;
99 int use = 0;
101 strbuf_addstr(&buf, workdir);
102 add_path(&buf, buf.len, name);
104 if (!lstat(buf.buf, &st) && !S_ISLNK(st.st_mode)) {
105 struct object_id wt_oid;
106 int fd = open(buf.buf, O_RDONLY);
108 if (fd >= 0 &&
109 !index_fd(&the_index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
110 if (is_null_oid(oid)) {
111 oidcpy(oid, &wt_oid);
112 use = 1;
113 } else if (oideq(oid, &wt_oid))
114 use = 1;
118 strbuf_release(&buf);
120 return use;
123 struct working_tree_entry {
124 struct hashmap_entry entry;
125 char path[FLEX_ARRAY];
128 static int working_tree_entry_cmp(const void *unused_cmp_data,
129 const struct hashmap_entry *eptr,
130 const struct hashmap_entry *entry_or_key,
131 const void *unused_keydata)
133 const struct working_tree_entry *a, *b;
135 a = container_of(eptr, const struct working_tree_entry, entry);
136 b = container_of(entry_or_key, const struct working_tree_entry, entry);
138 return strcmp(a->path, b->path);
142 * The `left` and `right` entries hold paths for the symlinks hashmap,
143 * and a SHA-1 surrounded by brief text for submodules.
145 struct pair_entry {
146 struct hashmap_entry entry;
147 char left[PATH_MAX], right[PATH_MAX];
148 const char path[FLEX_ARRAY];
151 static int pair_cmp(const void *unused_cmp_data,
152 const struct hashmap_entry *eptr,
153 const struct hashmap_entry *entry_or_key,
154 const void *unused_keydata)
156 const struct pair_entry *a, *b;
158 a = container_of(eptr, const struct pair_entry, entry);
159 b = container_of(entry_or_key, const struct pair_entry, entry);
161 return strcmp(a->path, b->path);
164 static void add_left_or_right(struct hashmap *map, const char *path,
165 const char *content, int is_right)
167 struct pair_entry *e, *existing;
169 FLEX_ALLOC_STR(e, path, path);
170 hashmap_entry_init(&e->entry, strhash(path));
171 existing = hashmap_get_entry(map, e, entry, NULL);
172 if (existing) {
173 free(e);
174 e = existing;
175 } else {
176 e->left[0] = e->right[0] = '\0';
177 hashmap_add(map, &e->entry);
179 strlcpy(is_right ? e->right : e->left, content, PATH_MAX);
182 struct path_entry {
183 struct hashmap_entry entry;
184 char path[FLEX_ARRAY];
187 static int path_entry_cmp(const void *unused_cmp_data,
188 const struct hashmap_entry *eptr,
189 const struct hashmap_entry *entry_or_key,
190 const void *key)
192 const struct path_entry *a, *b;
194 a = container_of(eptr, const struct path_entry, entry);
195 b = container_of(entry_or_key, const struct path_entry, entry);
197 return strcmp(a->path, key ? key : b->path);
200 static void changed_files(struct hashmap *result, const char *index_path,
201 const char *workdir)
203 struct child_process update_index = CHILD_PROCESS_INIT;
204 struct child_process diff_files = CHILD_PROCESS_INIT;
205 struct strbuf index_env = STRBUF_INIT, buf = STRBUF_INIT;
206 const char *git_dir = absolute_path(get_git_dir()), *env[] = {
207 NULL, NULL
209 FILE *fp;
211 strbuf_addf(&index_env, "GIT_INDEX_FILE=%s", index_path);
212 env[0] = index_env.buf;
214 strvec_pushl(&update_index.args,
215 "--git-dir", git_dir, "--work-tree", workdir,
216 "update-index", "--really-refresh", "-q",
217 "--unmerged", NULL);
218 update_index.no_stdin = 1;
219 update_index.no_stdout = 1;
220 update_index.no_stderr = 1;
221 update_index.git_cmd = 1;
222 update_index.use_shell = 0;
223 update_index.clean_on_exit = 1;
224 update_index.dir = workdir;
225 update_index.env = env;
226 /* Ignore any errors of update-index */
227 run_command(&update_index);
229 strvec_pushl(&diff_files.args,
230 "--git-dir", git_dir, "--work-tree", workdir,
231 "diff-files", "--name-only", "-z", NULL);
232 diff_files.no_stdin = 1;
233 diff_files.git_cmd = 1;
234 diff_files.use_shell = 0;
235 diff_files.clean_on_exit = 1;
236 diff_files.out = -1;
237 diff_files.dir = workdir;
238 diff_files.env = env;
239 if (start_command(&diff_files))
240 die("could not obtain raw diff");
241 fp = xfdopen(diff_files.out, "r");
242 while (!strbuf_getline_nul(&buf, fp)) {
243 struct path_entry *entry;
244 FLEX_ALLOC_STR(entry, path, buf.buf);
245 hashmap_entry_init(&entry->entry, strhash(buf.buf));
246 hashmap_add(result, &entry->entry);
248 fclose(fp);
249 if (finish_command(&diff_files))
250 die("diff-files did not exit properly");
251 strbuf_release(&index_env);
252 strbuf_release(&buf);
255 static NORETURN void exit_cleanup(const char *tmpdir, int exit_code)
257 struct strbuf buf = STRBUF_INIT;
258 strbuf_addstr(&buf, tmpdir);
259 remove_dir_recursively(&buf, 0);
260 if (exit_code)
261 warning(_("failed: %d"), exit_code);
262 exit(exit_code);
265 static int ensure_leading_directories(char *path)
267 switch (safe_create_leading_directories(path)) {
268 case SCLD_OK:
269 case SCLD_EXISTS:
270 return 0;
271 default:
272 return error(_("could not create leading directories "
273 "of '%s'"), path);
278 * Unconditional writing of a plain regular file is what
279 * "git difftool --dir-diff" wants to do for symlinks. We are preparing two
280 * temporary directories to be fed to a Git-unaware tool that knows how to
281 * show a diff of two directories (e.g. "diff -r A B").
283 * Because the tool is Git-unaware, if a symbolic link appears in either of
284 * these temporary directories, it will try to dereference and show the
285 * difference of the target of the symbolic link, which is not what we want,
286 * as the goal of the dir-diff mode is to produce an output that is logically
287 * equivalent to what "git diff" produces.
289 * Most importantly, we want to get textual comparison of the result of the
290 * readlink(2). get_symlink() provides that---it returns the contents of
291 * the symlink that gets written to a regular file to force the external tool
292 * to compare the readlink(2) result as text, even on a filesystem that is
293 * capable of doing a symbolic link.
295 static char *get_symlink(const struct object_id *oid, const char *path)
297 char *data;
298 if (is_null_oid(oid)) {
299 /* The symlink is unknown to Git so read from the filesystem */
300 struct strbuf link = STRBUF_INIT;
301 if (has_symlinks) {
302 if (strbuf_readlink(&link, path, strlen(path)))
303 die(_("could not read symlink %s"), path);
304 } else if (strbuf_read_file(&link, path, 128))
305 die(_("could not read symlink file %s"), path);
307 data = strbuf_detach(&link, NULL);
308 } else {
309 enum object_type type;
310 unsigned long size;
311 data = read_object_file(oid, &type, &size);
312 if (!data)
313 die(_("could not read object %s for symlink %s"),
314 oid_to_hex(oid), path);
317 return data;
320 static int checkout_path(unsigned mode, struct object_id *oid,
321 const char *path, const struct checkout *state)
323 struct cache_entry *ce;
324 int ret;
326 ce = make_transient_cache_entry(mode, oid, path, 0, NULL);
327 ret = checkout_entry(ce, state, NULL, NULL);
329 discard_cache_entry(ce);
330 return ret;
333 static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
334 struct child_process *child)
336 char tmpdir[PATH_MAX];
337 struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
338 struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
339 struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
340 struct strbuf wtdir = STRBUF_INIT;
341 char *lbase_dir, *rbase_dir;
342 size_t ldir_len, rdir_len, wtdir_len;
343 const char *workdir, *tmp;
344 int ret = 0, i;
345 FILE *fp;
346 struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
347 NULL);
348 struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
349 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
350 struct hashmap_iter iter;
351 struct pair_entry *entry;
352 struct index_state wtindex;
353 struct checkout lstate, rstate;
354 int rc, flags = RUN_GIT_CMD, err = 0;
355 const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL };
356 struct hashmap wt_modified, tmp_modified;
357 int indices_loaded = 0;
359 workdir = get_git_work_tree();
361 /* Setup temp directories */
362 tmp = getenv("TMPDIR");
363 xsnprintf(tmpdir, sizeof(tmpdir), "%s/git-difftool.XXXXXX", tmp ? tmp : "/tmp");
364 if (!mkdtemp(tmpdir))
365 return error("could not create '%s'", tmpdir);
366 strbuf_addf(&ldir, "%s/left/", tmpdir);
367 strbuf_addf(&rdir, "%s/right/", tmpdir);
368 strbuf_addstr(&wtdir, workdir);
369 if (!wtdir.len || !is_dir_sep(wtdir.buf[wtdir.len - 1]))
370 strbuf_addch(&wtdir, '/');
371 mkdir(ldir.buf, 0700);
372 mkdir(rdir.buf, 0700);
374 memset(&wtindex, 0, sizeof(wtindex));
376 memset(&lstate, 0, sizeof(lstate));
377 lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
378 lstate.base_dir_len = ldir.len;
379 lstate.force = 1;
380 memset(&rstate, 0, sizeof(rstate));
381 rstate.base_dir = rbase_dir = xstrdup(rdir.buf);
382 rstate.base_dir_len = rdir.len;
383 rstate.force = 1;
385 ldir_len = ldir.len;
386 rdir_len = rdir.len;
387 wtdir_len = wtdir.len;
389 child->no_stdin = 1;
390 child->git_cmd = 1;
391 child->use_shell = 0;
392 child->clean_on_exit = 1;
393 child->dir = prefix;
394 child->out = -1;
395 if (start_command(child))
396 die("could not obtain raw diff");
397 fp = xfdopen(child->out, "r");
399 /* Build index info for left and right sides of the diff */
400 i = 0;
401 while (!strbuf_getline_nul(&info, fp)) {
402 int lmode, rmode;
403 struct object_id loid, roid;
404 char status;
405 const char *src_path, *dst_path;
407 if (starts_with(info.buf, "::"))
408 die(N_("combined diff formats ('-c' and '--cc') are "
409 "not supported in\n"
410 "directory diff mode ('-d' and '--dir-diff')."));
412 if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
413 &status))
414 break;
415 if (strbuf_getline_nul(&lpath, fp))
416 break;
417 src_path = lpath.buf;
419 i++;
420 if (status != 'C' && status != 'R') {
421 dst_path = src_path;
422 } else {
423 if (strbuf_getline_nul(&rpath, fp))
424 break;
425 dst_path = rpath.buf;
428 if (S_ISGITLINK(lmode) || S_ISGITLINK(rmode)) {
429 strbuf_reset(&buf);
430 strbuf_addf(&buf, "Subproject commit %s",
431 oid_to_hex(&loid));
432 add_left_or_right(&submodules, src_path, buf.buf, 0);
433 strbuf_reset(&buf);
434 strbuf_addf(&buf, "Subproject commit %s",
435 oid_to_hex(&roid));
436 if (oideq(&loid, &roid))
437 strbuf_addstr(&buf, "-dirty");
438 add_left_or_right(&submodules, dst_path, buf.buf, 1);
439 continue;
442 if (S_ISLNK(lmode)) {
443 char *content = get_symlink(&loid, src_path);
444 add_left_or_right(&symlinks2, src_path, content, 0);
445 free(content);
448 if (S_ISLNK(rmode)) {
449 char *content = get_symlink(&roid, dst_path);
450 add_left_or_right(&symlinks2, dst_path, content, 1);
451 free(content);
454 if (lmode && status != 'C') {
455 if (checkout_path(lmode, &loid, src_path, &lstate)) {
456 ret = error("could not write '%s'", src_path);
457 goto finish;
461 if (rmode && !S_ISLNK(rmode)) {
462 struct working_tree_entry *entry;
464 /* Avoid duplicate working_tree entries */
465 FLEX_ALLOC_STR(entry, path, dst_path);
466 hashmap_entry_init(&entry->entry, strhash(dst_path));
467 if (hashmap_get(&working_tree_dups, &entry->entry,
468 NULL)) {
469 free(entry);
470 continue;
472 hashmap_add(&working_tree_dups, &entry->entry);
474 if (!use_wt_file(workdir, dst_path, &roid)) {
475 if (checkout_path(rmode, &roid, dst_path,
476 &rstate)) {
477 ret = error("could not write '%s'",
478 dst_path);
479 goto finish;
481 } else if (!is_null_oid(&roid)) {
483 * Changes in the working tree need special
484 * treatment since they are not part of the
485 * index.
487 struct cache_entry *ce2 =
488 make_cache_entry(&wtindex, rmode, &roid,
489 dst_path, 0, 0);
491 add_index_entry(&wtindex, ce2,
492 ADD_CACHE_JUST_APPEND);
494 add_path(&rdir, rdir_len, dst_path);
495 if (ensure_leading_directories(rdir.buf)) {
496 ret = error("could not create "
497 "directory for '%s'",
498 dst_path);
499 goto finish;
501 add_path(&wtdir, wtdir_len, dst_path);
502 if (symlinks) {
503 if (symlink(wtdir.buf, rdir.buf)) {
504 ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
505 goto finish;
507 } else {
508 struct stat st;
509 if (stat(wtdir.buf, &st))
510 st.st_mode = 0644;
511 if (copy_file(rdir.buf, wtdir.buf,
512 st.st_mode)) {
513 ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
514 goto finish;
521 fclose(fp);
522 fp = NULL;
523 if (finish_command(child)) {
524 ret = error("error occurred running diff --raw");
525 goto finish;
528 if (!i)
529 goto finish;
532 * Changes to submodules require special treatment.This loop writes a
533 * temporary file to both the left and right directories to show the
534 * change in the recorded SHA1 for the submodule.
536 hashmap_for_each_entry(&submodules, &iter, entry,
537 entry /* member name */) {
538 if (*entry->left) {
539 add_path(&ldir, ldir_len, entry->path);
540 ensure_leading_directories(ldir.buf);
541 write_file(ldir.buf, "%s", entry->left);
543 if (*entry->right) {
544 add_path(&rdir, rdir_len, entry->path);
545 ensure_leading_directories(rdir.buf);
546 write_file(rdir.buf, "%s", entry->right);
551 * Symbolic links require special treatment.The standard "git diff"
552 * shows only the link itself, not the contents of the link target.
553 * This loop replicates that behavior.
555 hashmap_for_each_entry(&symlinks2, &iter, entry,
556 entry /* member name */) {
557 if (*entry->left) {
558 add_path(&ldir, ldir_len, entry->path);
559 ensure_leading_directories(ldir.buf);
560 unlink(ldir.buf);
561 write_file(ldir.buf, "%s", entry->left);
563 if (*entry->right) {
564 add_path(&rdir, rdir_len, entry->path);
565 ensure_leading_directories(rdir.buf);
566 unlink(rdir.buf);
567 write_file(rdir.buf, "%s", entry->right);
571 strbuf_release(&buf);
573 strbuf_setlen(&ldir, ldir_len);
574 helper_argv[1] = ldir.buf;
575 strbuf_setlen(&rdir, rdir_len);
576 helper_argv[2] = rdir.buf;
578 if (extcmd) {
579 helper_argv[0] = extcmd;
580 flags = 0;
581 } else
582 setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
583 rc = run_command_v_opt(helper_argv, flags);
585 /* TODO: audit for interaction with sparse-index. */
586 ensure_full_index(&wtindex);
589 * If the diff includes working copy files and those
590 * files were modified during the diff, then the changes
591 * should be copied back to the working tree.
592 * Do not copy back files when symlinks are used and the
593 * external tool did not replace the original link with a file.
595 * These hashes are loaded lazily since they aren't needed
596 * in the common case of --symlinks and the difftool updating
597 * files through the symlink.
599 hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
600 hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
602 for (i = 0; i < wtindex.cache_nr; i++) {
603 struct hashmap_entry dummy;
604 const char *name = wtindex.cache[i]->name;
605 struct stat st;
607 add_path(&rdir, rdir_len, name);
608 if (lstat(rdir.buf, &st))
609 continue;
611 if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
612 continue;
614 if (!indices_loaded) {
615 struct lock_file lock = LOCK_INIT;
616 strbuf_reset(&buf);
617 strbuf_addf(&buf, "%s/wtindex", tmpdir);
618 if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
619 write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
620 ret = error("could not write %s", buf.buf);
621 goto finish;
623 changed_files(&wt_modified, buf.buf, workdir);
624 strbuf_setlen(&rdir, rdir_len);
625 changed_files(&tmp_modified, buf.buf, rdir.buf);
626 add_path(&rdir, rdir_len, name);
627 indices_loaded = 1;
630 hashmap_entry_init(&dummy, strhash(name));
631 if (hashmap_get(&tmp_modified, &dummy, name)) {
632 add_path(&wtdir, wtdir_len, name);
633 if (hashmap_get(&wt_modified, &dummy, name)) {
634 warning(_("both files modified: '%s' and '%s'."),
635 wtdir.buf, rdir.buf);
636 warning(_("working tree file has been left."));
637 warning("%s", "");
638 err = 1;
639 } else if (unlink(wtdir.buf) ||
640 copy_file(wtdir.buf, rdir.buf, st.st_mode))
641 warning_errno(_("could not copy '%s' to '%s'"),
642 rdir.buf, wtdir.buf);
646 if (err) {
647 warning(_("temporary files exist in '%s'."), tmpdir);
648 warning(_("you may want to cleanup or recover these."));
649 exit(1);
650 } else
651 exit_cleanup(tmpdir, rc);
653 finish:
654 if (fp)
655 fclose(fp);
657 free(lbase_dir);
658 free(rbase_dir);
659 strbuf_release(&ldir);
660 strbuf_release(&rdir);
661 strbuf_release(&wtdir);
662 strbuf_release(&buf);
664 return ret;
667 static int run_file_diff(int prompt, const char *prefix,
668 struct child_process *child)
670 const char *env[] = {
671 "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
672 NULL
675 if (prompt > 0)
676 env[2] = "GIT_DIFFTOOL_PROMPT=true";
677 else if (!prompt)
678 env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";
680 child->git_cmd = 1;
681 child->dir = prefix;
682 strvec_pushv(&child->env_array, env);
684 return run_command(child);
687 int cmd_difftool(int argc, const char **argv, const char *prefix)
689 int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
690 tool_help = 0, no_index = 0;
691 static char *difftool_cmd = NULL, *extcmd = NULL;
692 struct option builtin_difftool_options[] = {
693 OPT_BOOL('g', "gui", &use_gui_tool,
694 N_("use `diff.guitool` instead of `diff.tool`")),
695 OPT_BOOL('d', "dir-diff", &dir_diff,
696 N_("perform a full-directory diff")),
697 OPT_SET_INT_F('y', "no-prompt", &prompt,
698 N_("do not prompt before launching a diff tool"),
699 0, PARSE_OPT_NONEG),
700 OPT_SET_INT_F(0, "prompt", &prompt, NULL,
701 1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
702 OPT_BOOL(0, "symlinks", &symlinks,
703 N_("use symlinks in dir-diff mode")),
704 OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
705 N_("use the specified diff tool")),
706 OPT_BOOL(0, "tool-help", &tool_help,
707 N_("print a list of diff tools that may be used with "
708 "`--tool`")),
709 OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
710 N_("make 'git-difftool' exit when an invoked diff "
711 "tool returns a non-zero exit code")),
712 OPT_STRING('x', "extcmd", &extcmd, N_("command"),
713 N_("specify a custom command for viewing diffs")),
714 OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
715 OPT_END()
717 struct child_process child = CHILD_PROCESS_INIT;
719 git_config(difftool_config, NULL);
720 symlinks = has_symlinks;
722 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
723 builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
724 PARSE_OPT_KEEP_DASHDASH);
726 if (tool_help)
727 return print_tool_help();
729 if (!no_index && !startup_info->have_repository)
730 die(_("difftool requires worktree or --no-index"));
732 if (!no_index){
733 setup_work_tree();
734 setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
735 setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
736 } else if (dir_diff)
737 die(_("--dir-diff is incompatible with --no-index"));
739 if (use_gui_tool + !!difftool_cmd + !!extcmd > 1)
740 die(_("--gui, --tool and --extcmd are mutually exclusive"));
742 if (use_gui_tool)
743 setenv("GIT_MERGETOOL_GUI", "true", 1);
744 else if (difftool_cmd) {
745 if (*difftool_cmd)
746 setenv("GIT_DIFF_TOOL", difftool_cmd, 1);
747 else
748 die(_("no <tool> given for --tool=<tool>"));
751 if (extcmd) {
752 if (*extcmd)
753 setenv("GIT_DIFFTOOL_EXTCMD", extcmd, 1);
754 else
755 die(_("no <cmd> given for --extcmd=<cmd>"));
758 setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
759 trust_exit_code ? "true" : "false", 1);
762 * In directory diff mode, 'git-difftool--helper' is called once
763 * to compare the a / b directories. In file diff mode, 'git diff'
764 * will invoke a separate instance of 'git-difftool--helper' for
765 * each file that changed.
767 strvec_push(&child.args, "diff");
768 if (no_index)
769 strvec_push(&child.args, "--no-index");
770 if (dir_diff)
771 strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
772 strvec_pushv(&child.args, argv);
774 if (dir_diff)
775 return run_dir_diff(extcmd, symlinks, prefix, &child);
776 return run_file_diff(prompt, prefix, &child);