treewide: remove cache.h inclusion due to object-file.h changes
[alt-git.git] / builtin / difftool.c
blob3ffb0524be76bb9bd258f9eeef12280fcbfed266
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_VARIABLE
15 #include "cache.h"
16 #include "abspath.h"
17 #include "config.h"
18 #include "builtin.h"
19 #include "run-command.h"
20 #include "environment.h"
21 #include "exec-cmd.h"
22 #include "gettext.h"
23 #include "hex.h"
24 #include "parse-options.h"
25 #include "strvec.h"
26 #include "strbuf.h"
27 #include "lockfile.h"
28 #include "object-file.h"
29 #include "object-store.h"
30 #include "dir.h"
31 #include "entry.h"
32 #include "setup.h"
33 #include "wrapper.h"
35 static int trust_exit_code;
37 static const char *const builtin_difftool_usage[] = {
38 N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
39 NULL
42 static int difftool_config(const char *var, const char *value, void *cb)
44 if (!strcmp(var, "difftool.trustexitcode")) {
45 trust_exit_code = git_config_bool(var, value);
46 return 0;
49 return git_default_config(var, value, cb);
52 static int print_tool_help(void)
54 struct child_process cmd = CHILD_PROCESS_INIT;
56 cmd.git_cmd = 1;
57 strvec_pushl(&cmd.args, "mergetool", "--tool-help=diff", NULL);
58 return run_command(&cmd);
61 static int parse_index_info(char *p, int *mode1, int *mode2,
62 struct object_id *oid1, struct object_id *oid2,
63 char *status)
65 if (*p != ':')
66 return error("expected ':', got '%c'", *p);
67 *mode1 = (int)strtol(p + 1, &p, 8);
68 if (*p != ' ')
69 return error("expected ' ', got '%c'", *p);
70 *mode2 = (int)strtol(p + 1, &p, 8);
71 if (*p != ' ')
72 return error("expected ' ', got '%c'", *p);
73 if (parse_oid_hex(++p, oid1, (const char **)&p))
74 return error("expected object ID, got '%s'", p);
75 if (*p != ' ')
76 return error("expected ' ', got '%c'", *p);
77 if (parse_oid_hex(++p, oid2, (const char **)&p))
78 return error("expected object ID, got '%s'", p);
79 if (*p != ' ')
80 return error("expected ' ', got '%c'", *p);
81 *status = *++p;
82 if (!*status)
83 return error("missing status");
84 if (p[1] && !isdigit(p[1]))
85 return error("unexpected trailer: '%s'", p + 1);
86 return 0;
90 * Remove any trailing slash from $workdir
91 * before starting to avoid double slashes in symlink targets.
93 static void add_path(struct strbuf *buf, size_t base_len, const char *path)
95 strbuf_setlen(buf, base_len);
96 if (buf->len && buf->buf[buf->len - 1] != '/')
97 strbuf_addch(buf, '/');
98 strbuf_addstr(buf, path);
102 * Determine whether we can simply reuse the file in the worktree.
104 static int use_wt_file(const char *workdir, const char *name,
105 struct object_id *oid)
107 struct strbuf buf = STRBUF_INIT;
108 struct stat st;
109 int use = 0;
111 strbuf_addstr(&buf, workdir);
112 add_path(&buf, buf.len, name);
114 if (!lstat(buf.buf, &st) && !S_ISLNK(st.st_mode)) {
115 struct object_id wt_oid;
116 int fd = open(buf.buf, O_RDONLY);
118 if (fd >= 0 &&
119 !index_fd(&the_index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
120 if (is_null_oid(oid)) {
121 oidcpy(oid, &wt_oid);
122 use = 1;
123 } else if (oideq(oid, &wt_oid))
124 use = 1;
128 strbuf_release(&buf);
130 return use;
133 struct working_tree_entry {
134 struct hashmap_entry entry;
135 char path[FLEX_ARRAY];
138 static int working_tree_entry_cmp(const void *cmp_data UNUSED,
139 const struct hashmap_entry *eptr,
140 const struct hashmap_entry *entry_or_key,
141 const void *keydata UNUSED)
143 const struct working_tree_entry *a, *b;
145 a = container_of(eptr, const struct working_tree_entry, entry);
146 b = container_of(entry_or_key, const struct working_tree_entry, entry);
148 return strcmp(a->path, b->path);
152 * The `left` and `right` entries hold paths for the symlinks hashmap,
153 * and a SHA-1 surrounded by brief text for submodules.
155 struct pair_entry {
156 struct hashmap_entry entry;
157 char left[PATH_MAX], right[PATH_MAX];
158 const char path[FLEX_ARRAY];
161 static int pair_cmp(const void *cmp_data UNUSED,
162 const struct hashmap_entry *eptr,
163 const struct hashmap_entry *entry_or_key,
164 const void *keydata UNUSED)
166 const struct pair_entry *a, *b;
168 a = container_of(eptr, const struct pair_entry, entry);
169 b = container_of(entry_or_key, const struct pair_entry, entry);
171 return strcmp(a->path, b->path);
174 static void add_left_or_right(struct hashmap *map, const char *path,
175 const char *content, int is_right)
177 struct pair_entry *e, *existing;
179 FLEX_ALLOC_STR(e, path, path);
180 hashmap_entry_init(&e->entry, strhash(path));
181 existing = hashmap_get_entry(map, e, entry, NULL);
182 if (existing) {
183 free(e);
184 e = existing;
185 } else {
186 e->left[0] = e->right[0] = '\0';
187 hashmap_add(map, &e->entry);
189 strlcpy(is_right ? e->right : e->left, content, PATH_MAX);
192 struct path_entry {
193 struct hashmap_entry entry;
194 char path[FLEX_ARRAY];
197 static int path_entry_cmp(const void *cmp_data UNUSED,
198 const struct hashmap_entry *eptr,
199 const struct hashmap_entry *entry_or_key,
200 const void *key)
202 const struct path_entry *a, *b;
204 a = container_of(eptr, const struct path_entry, entry);
205 b = container_of(entry_or_key, const struct path_entry, entry);
207 return strcmp(a->path, key ? key : b->path);
210 static void changed_files(struct hashmap *result, const char *index_path,
211 const char *workdir)
213 struct child_process update_index = CHILD_PROCESS_INIT;
214 struct child_process diff_files = CHILD_PROCESS_INIT;
215 struct strbuf buf = STRBUF_INIT;
216 const char *git_dir = absolute_path(get_git_dir());
217 FILE *fp;
219 strvec_pushl(&update_index.args,
220 "--git-dir", git_dir, "--work-tree", workdir,
221 "update-index", "--really-refresh", "-q",
222 "--unmerged", NULL);
223 update_index.no_stdin = 1;
224 update_index.no_stdout = 1;
225 update_index.no_stderr = 1;
226 update_index.git_cmd = 1;
227 update_index.use_shell = 0;
228 update_index.clean_on_exit = 1;
229 update_index.dir = workdir;
230 strvec_pushf(&update_index.env, "GIT_INDEX_FILE=%s", index_path);
231 /* Ignore any errors of update-index */
232 run_command(&update_index);
234 strvec_pushl(&diff_files.args,
235 "--git-dir", git_dir, "--work-tree", workdir,
236 "diff-files", "--name-only", "-z", NULL);
237 diff_files.no_stdin = 1;
238 diff_files.git_cmd = 1;
239 diff_files.use_shell = 0;
240 diff_files.clean_on_exit = 1;
241 diff_files.out = -1;
242 diff_files.dir = workdir;
243 strvec_pushf(&diff_files.env, "GIT_INDEX_FILE=%s", index_path);
244 if (start_command(&diff_files))
245 die("could not obtain raw diff");
246 fp = xfdopen(diff_files.out, "r");
247 while (!strbuf_getline_nul(&buf, fp)) {
248 struct path_entry *entry;
249 FLEX_ALLOC_STR(entry, path, buf.buf);
250 hashmap_entry_init(&entry->entry, strhash(buf.buf));
251 hashmap_add(result, &entry->entry);
253 fclose(fp);
254 if (finish_command(&diff_files))
255 die("diff-files did not exit properly");
256 strbuf_release(&buf);
259 static int ensure_leading_directories(char *path)
261 switch (safe_create_leading_directories(path)) {
262 case SCLD_OK:
263 case SCLD_EXISTS:
264 return 0;
265 default:
266 return error(_("could not create leading directories "
267 "of '%s'"), path);
272 * Unconditional writing of a plain regular file is what
273 * "git difftool --dir-diff" wants to do for symlinks. We are preparing two
274 * temporary directories to be fed to a Git-unaware tool that knows how to
275 * show a diff of two directories (e.g. "diff -r A B").
277 * Because the tool is Git-unaware, if a symbolic link appears in either of
278 * these temporary directories, it will try to dereference and show the
279 * difference of the target of the symbolic link, which is not what we want,
280 * as the goal of the dir-diff mode is to produce an output that is logically
281 * equivalent to what "git diff" produces.
283 * Most importantly, we want to get textual comparison of the result of the
284 * readlink(2). get_symlink() provides that---it returns the contents of
285 * the symlink that gets written to a regular file to force the external tool
286 * to compare the readlink(2) result as text, even on a filesystem that is
287 * capable of doing a symbolic link.
289 static char *get_symlink(const struct object_id *oid, const char *path)
291 char *data;
292 if (is_null_oid(oid)) {
293 /* The symlink is unknown to Git so read from the filesystem */
294 struct strbuf link = STRBUF_INIT;
295 if (has_symlinks) {
296 if (strbuf_readlink(&link, path, strlen(path)))
297 die(_("could not read symlink %s"), path);
298 } else if (strbuf_read_file(&link, path, 128))
299 die(_("could not read symlink file %s"), path);
301 data = strbuf_detach(&link, NULL);
302 } else {
303 enum object_type type;
304 unsigned long size;
305 data = repo_read_object_file(the_repository, oid, &type,
306 &size);
307 if (!data)
308 die(_("could not read object %s for symlink %s"),
309 oid_to_hex(oid), path);
312 return data;
315 static int checkout_path(unsigned mode, struct object_id *oid,
316 const char *path, const struct checkout *state)
318 struct cache_entry *ce;
319 int ret;
321 ce = make_transient_cache_entry(mode, oid, path, 0, NULL);
322 ret = checkout_entry(ce, state, NULL, NULL);
324 discard_cache_entry(ce);
325 return ret;
328 static void write_file_in_directory(struct strbuf *dir, size_t dir_len,
329 const char *path, const char *content)
331 add_path(dir, dir_len, path);
332 ensure_leading_directories(dir->buf);
333 unlink(dir->buf);
334 write_file(dir->buf, "%s", content);
337 /* Write the file contents for the left and right sides of the difftool
338 * dir-diff representation for submodules and symlinks. Symlinks and submodules
339 * are written as regular text files so that external diff tools can diff them
340 * as text files, resulting in behavior that is analogous to to what "git diff"
341 * displays for symlink and submodule diffs.
343 static void write_standin_files(struct pair_entry *entry,
344 struct strbuf *ldir, size_t ldir_len,
345 struct strbuf *rdir, size_t rdir_len)
347 if (*entry->left)
348 write_file_in_directory(ldir, ldir_len, entry->path, entry->left);
349 if (*entry->right)
350 write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
353 static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
354 struct child_process *child)
356 struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
357 struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
358 struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
359 struct strbuf wtdir = STRBUF_INIT;
360 struct strbuf tmpdir = STRBUF_INIT;
361 char *lbase_dir = NULL, *rbase_dir = NULL;
362 size_t ldir_len, rdir_len, wtdir_len;
363 const char *workdir, *tmp;
364 int ret = 0, i;
365 FILE *fp = NULL;
366 struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
367 NULL);
368 struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
369 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
370 struct hashmap_iter iter;
371 struct pair_entry *entry;
372 struct index_state wtindex = INDEX_STATE_INIT(the_repository);
373 struct checkout lstate, rstate;
374 int err = 0;
375 struct child_process cmd = CHILD_PROCESS_INIT;
376 struct hashmap wt_modified, tmp_modified;
377 int indices_loaded = 0;
379 workdir = get_git_work_tree();
381 /* Setup temp directories */
382 tmp = getenv("TMPDIR");
383 strbuf_add_absolute_path(&tmpdir, tmp ? tmp : "/tmp");
384 strbuf_trim_trailing_dir_sep(&tmpdir);
385 strbuf_addstr(&tmpdir, "/git-difftool.XXXXXX");
386 if (!mkdtemp(tmpdir.buf)) {
387 ret = error("could not create '%s'", tmpdir.buf);
388 goto finish;
390 strbuf_addf(&ldir, "%s/left/", tmpdir.buf);
391 strbuf_addf(&rdir, "%s/right/", tmpdir.buf);
392 strbuf_addstr(&wtdir, workdir);
393 if (!wtdir.len || !is_dir_sep(wtdir.buf[wtdir.len - 1]))
394 strbuf_addch(&wtdir, '/');
395 mkdir(ldir.buf, 0700);
396 mkdir(rdir.buf, 0700);
398 memset(&lstate, 0, sizeof(lstate));
399 lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
400 lstate.base_dir_len = ldir.len;
401 lstate.force = 1;
402 memset(&rstate, 0, sizeof(rstate));
403 rstate.base_dir = rbase_dir = xstrdup(rdir.buf);
404 rstate.base_dir_len = rdir.len;
405 rstate.force = 1;
407 ldir_len = ldir.len;
408 rdir_len = rdir.len;
409 wtdir_len = wtdir.len;
411 child->no_stdin = 1;
412 child->git_cmd = 1;
413 child->use_shell = 0;
414 child->clean_on_exit = 1;
415 child->dir = prefix;
416 child->out = -1;
417 if (start_command(child))
418 die("could not obtain raw diff");
419 fp = xfdopen(child->out, "r");
421 /* Build index info for left and right sides of the diff */
422 i = 0;
423 while (!strbuf_getline_nul(&info, fp)) {
424 int lmode, rmode;
425 struct object_id loid, roid;
426 char status;
427 const char *src_path, *dst_path;
429 if (starts_with(info.buf, "::"))
430 die(N_("combined diff formats ('-c' and '--cc') are "
431 "not supported in\n"
432 "directory diff mode ('-d' and '--dir-diff')."));
434 if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
435 &status))
436 break;
437 if (strbuf_getline_nul(&lpath, fp))
438 break;
439 src_path = lpath.buf;
441 i++;
442 if (status != 'C' && status != 'R') {
443 dst_path = src_path;
444 } else {
445 if (strbuf_getline_nul(&rpath, fp))
446 break;
447 dst_path = rpath.buf;
450 if (S_ISGITLINK(lmode) || S_ISGITLINK(rmode)) {
451 strbuf_reset(&buf);
452 strbuf_addf(&buf, "Subproject commit %s",
453 oid_to_hex(&loid));
454 add_left_or_right(&submodules, src_path, buf.buf, 0);
455 strbuf_reset(&buf);
456 strbuf_addf(&buf, "Subproject commit %s",
457 oid_to_hex(&roid));
458 if (oideq(&loid, &roid))
459 strbuf_addstr(&buf, "-dirty");
460 add_left_or_right(&submodules, dst_path, buf.buf, 1);
461 continue;
464 if (S_ISLNK(lmode)) {
465 char *content = get_symlink(&loid, src_path);
466 add_left_or_right(&symlinks2, src_path, content, 0);
467 free(content);
470 if (S_ISLNK(rmode)) {
471 char *content = get_symlink(&roid, dst_path);
472 add_left_or_right(&symlinks2, dst_path, content, 1);
473 free(content);
476 if (lmode && status != 'C') {
477 if (checkout_path(lmode, &loid, src_path, &lstate)) {
478 ret = error("could not write '%s'", src_path);
479 goto finish;
483 if (rmode && !S_ISLNK(rmode)) {
484 struct working_tree_entry *entry;
486 /* Avoid duplicate working_tree entries */
487 FLEX_ALLOC_STR(entry, path, dst_path);
488 hashmap_entry_init(&entry->entry, strhash(dst_path));
489 if (hashmap_get(&working_tree_dups, &entry->entry,
490 NULL)) {
491 free(entry);
492 continue;
494 hashmap_add(&working_tree_dups, &entry->entry);
496 if (!use_wt_file(workdir, dst_path, &roid)) {
497 if (checkout_path(rmode, &roid, dst_path,
498 &rstate)) {
499 ret = error("could not write '%s'",
500 dst_path);
501 goto finish;
503 } else if (!is_null_oid(&roid)) {
505 * Changes in the working tree need special
506 * treatment since they are not part of the
507 * index.
509 struct cache_entry *ce2 =
510 make_cache_entry(&wtindex, rmode, &roid,
511 dst_path, 0, 0);
513 add_index_entry(&wtindex, ce2,
514 ADD_CACHE_JUST_APPEND);
516 add_path(&rdir, rdir_len, dst_path);
517 if (ensure_leading_directories(rdir.buf)) {
518 ret = error("could not create "
519 "directory for '%s'",
520 dst_path);
521 goto finish;
523 add_path(&wtdir, wtdir_len, dst_path);
524 if (symlinks) {
525 if (symlink(wtdir.buf, rdir.buf)) {
526 ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
527 goto finish;
529 } else {
530 struct stat st;
531 if (stat(wtdir.buf, &st))
532 st.st_mode = 0644;
533 if (copy_file(rdir.buf, wtdir.buf,
534 st.st_mode)) {
535 ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
536 goto finish;
543 fclose(fp);
544 fp = NULL;
545 if (finish_command(child)) {
546 ret = error("error occurred running diff --raw");
547 goto finish;
550 if (!i)
551 goto finish;
554 * Changes to submodules require special treatment.This loop writes a
555 * temporary file to both the left and right directories to show the
556 * change in the recorded SHA1 for the submodule.
558 hashmap_for_each_entry(&submodules, &iter, entry,
559 entry /* member name */) {
560 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
564 * Symbolic links require special treatment. The standard "git diff"
565 * shows only the link itself, not the contents of the link target.
566 * This loop replicates that behavior.
568 hashmap_for_each_entry(&symlinks2, &iter, entry,
569 entry /* member name */) {
571 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
574 strbuf_setlen(&ldir, ldir_len);
575 strbuf_setlen(&rdir, rdir_len);
577 if (extcmd) {
578 strvec_push(&cmd.args, extcmd);
579 } else {
580 strvec_push(&cmd.args, "difftool--helper");
581 cmd.git_cmd = 1;
582 setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
584 strvec_pushl(&cmd.args, ldir.buf, rdir.buf, NULL);
585 ret = run_command(&cmd);
587 /* TODO: audit for interaction with sparse-index. */
588 ensure_full_index(&wtindex);
591 * If the diff includes working copy files and those
592 * files were modified during the diff, then the changes
593 * should be copied back to the working tree.
594 * Do not copy back files when symlinks are used and the
595 * external tool did not replace the original link with a file.
597 * These hashes are loaded lazily since they aren't needed
598 * in the common case of --symlinks and the difftool updating
599 * files through the symlink.
601 hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
602 hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
604 for (i = 0; i < wtindex.cache_nr; i++) {
605 struct hashmap_entry dummy;
606 const char *name = wtindex.cache[i]->name;
607 struct stat st;
609 add_path(&rdir, rdir_len, name);
610 if (lstat(rdir.buf, &st))
611 continue;
613 if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
614 continue;
616 if (!indices_loaded) {
617 struct lock_file lock = LOCK_INIT;
618 strbuf_reset(&buf);
619 strbuf_addf(&buf, "%s/wtindex", tmpdir.buf);
620 if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
621 write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
622 ret = error("could not write %s", buf.buf);
623 goto finish;
625 changed_files(&wt_modified, buf.buf, workdir);
626 strbuf_setlen(&rdir, rdir_len);
627 changed_files(&tmp_modified, buf.buf, rdir.buf);
628 add_path(&rdir, rdir_len, name);
629 indices_loaded = 1;
632 hashmap_entry_init(&dummy, strhash(name));
633 if (hashmap_get(&tmp_modified, &dummy, name)) {
634 add_path(&wtdir, wtdir_len, name);
635 if (hashmap_get(&wt_modified, &dummy, name)) {
636 warning(_("both files modified: '%s' and '%s'."),
637 wtdir.buf, rdir.buf);
638 warning(_("working tree file has been left."));
639 warning("%s", "");
640 err = 1;
641 } else if (unlink(wtdir.buf) ||
642 copy_file(wtdir.buf, rdir.buf, st.st_mode))
643 warning_errno(_("could not copy '%s' to '%s'"),
644 rdir.buf, wtdir.buf);
648 if (err) {
649 warning(_("temporary files exist in '%s'."), tmpdir.buf);
650 warning(_("you may want to cleanup or recover these."));
651 ret = 1;
652 } else {
653 remove_dir_recursively(&tmpdir, 0);
654 if (ret)
655 warning(_("failed: %d"), ret);
658 finish:
659 if (fp)
660 fclose(fp);
662 free(lbase_dir);
663 free(rbase_dir);
664 strbuf_release(&ldir);
665 strbuf_release(&rdir);
666 strbuf_release(&wtdir);
667 strbuf_release(&buf);
668 strbuf_release(&tmpdir);
670 return (ret < 0) ? 1 : ret;
673 static int run_file_diff(int prompt, const char *prefix,
674 struct child_process *child)
676 const char *env[] = {
677 "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
678 NULL
681 if (prompt > 0)
682 env[2] = "GIT_DIFFTOOL_PROMPT=true";
683 else if (!prompt)
684 env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";
686 child->git_cmd = 1;
687 child->dir = prefix;
688 strvec_pushv(&child->env, env);
690 return run_command(child);
693 int cmd_difftool(int argc, const char **argv, const char *prefix)
695 int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
696 tool_help = 0, no_index = 0;
697 static char *difftool_cmd = NULL, *extcmd = NULL;
698 struct option builtin_difftool_options[] = {
699 OPT_BOOL('g', "gui", &use_gui_tool,
700 N_("use `diff.guitool` instead of `diff.tool`")),
701 OPT_BOOL('d', "dir-diff", &dir_diff,
702 N_("perform a full-directory diff")),
703 OPT_SET_INT_F('y', "no-prompt", &prompt,
704 N_("do not prompt before launching a diff tool"),
705 0, PARSE_OPT_NONEG),
706 OPT_SET_INT_F(0, "prompt", &prompt, NULL,
707 1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
708 OPT_BOOL(0, "symlinks", &symlinks,
709 N_("use symlinks in dir-diff mode")),
710 OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
711 N_("use the specified diff tool")),
712 OPT_BOOL(0, "tool-help", &tool_help,
713 N_("print a list of diff tools that may be used with "
714 "`--tool`")),
715 OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
716 N_("make 'git-difftool' exit when an invoked diff "
717 "tool returns a non-zero exit code")),
718 OPT_STRING('x', "extcmd", &extcmd, N_("command"),
719 N_("specify a custom command for viewing diffs")),
720 OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
721 OPT_END()
723 struct child_process child = CHILD_PROCESS_INIT;
725 git_config(difftool_config, NULL);
726 symlinks = has_symlinks;
728 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
729 builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
730 PARSE_OPT_KEEP_DASHDASH);
732 if (tool_help)
733 return print_tool_help();
735 if (!no_index && !startup_info->have_repository)
736 die(_("difftool requires worktree or --no-index"));
738 if (!no_index){
739 setup_work_tree();
740 setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
741 setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
742 } else if (dir_diff)
743 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
745 die_for_incompatible_opt3(use_gui_tool, "--gui",
746 !!difftool_cmd, "--tool",
747 !!extcmd, "--extcmd");
749 if (use_gui_tool)
750 setenv("GIT_MERGETOOL_GUI", "true", 1);
751 else if (difftool_cmd) {
752 if (*difftool_cmd)
753 setenv("GIT_DIFF_TOOL", difftool_cmd, 1);
754 else
755 die(_("no <tool> given for --tool=<tool>"));
758 if (extcmd) {
759 if (*extcmd)
760 setenv("GIT_DIFFTOOL_EXTCMD", extcmd, 1);
761 else
762 die(_("no <cmd> given for --extcmd=<cmd>"));
765 setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
766 trust_exit_code ? "true" : "false", 1);
769 * In directory diff mode, 'git-difftool--helper' is called once
770 * to compare the a / b directories. In file diff mode, 'git diff'
771 * will invoke a separate instance of 'git-difftool--helper' for
772 * each file that changed.
774 strvec_push(&child.args, "diff");
775 if (no_index)
776 strvec_push(&child.args, "--no-index");
777 if (dir_diff)
778 strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
779 strvec_pushv(&child.args, argv);
781 if (dir_diff)
782 return run_dir_diff(extcmd, symlinks, prefix, &child);
783 return run_file_diff(prompt, prefix, &child);