Merge branch 'hn/reftable-coverity-fixes'
[git/debian.git] / builtin / difftool.c
blobc79fbbf67e5ee0d4e7d09c7997f1fa04dc1400bc
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 buf = STRBUF_INIT;
206 const char *git_dir = absolute_path(get_git_dir());
207 FILE *fp;
209 strvec_pushl(&update_index.args,
210 "--git-dir", git_dir, "--work-tree", workdir,
211 "update-index", "--really-refresh", "-q",
212 "--unmerged", NULL);
213 update_index.no_stdin = 1;
214 update_index.no_stdout = 1;
215 update_index.no_stderr = 1;
216 update_index.git_cmd = 1;
217 update_index.use_shell = 0;
218 update_index.clean_on_exit = 1;
219 update_index.dir = workdir;
220 strvec_pushf(&update_index.env_array, "GIT_INDEX_FILE=%s", index_path);
221 /* Ignore any errors of update-index */
222 run_command(&update_index);
224 strvec_pushl(&diff_files.args,
225 "--git-dir", git_dir, "--work-tree", workdir,
226 "diff-files", "--name-only", "-z", NULL);
227 diff_files.no_stdin = 1;
228 diff_files.git_cmd = 1;
229 diff_files.use_shell = 0;
230 diff_files.clean_on_exit = 1;
231 diff_files.out = -1;
232 diff_files.dir = workdir;
233 strvec_pushf(&diff_files.env_array, "GIT_INDEX_FILE=%s", index_path);
234 if (start_command(&diff_files))
235 die("could not obtain raw diff");
236 fp = xfdopen(diff_files.out, "r");
237 while (!strbuf_getline_nul(&buf, fp)) {
238 struct path_entry *entry;
239 FLEX_ALLOC_STR(entry, path, buf.buf);
240 hashmap_entry_init(&entry->entry, strhash(buf.buf));
241 hashmap_add(result, &entry->entry);
243 fclose(fp);
244 if (finish_command(&diff_files))
245 die("diff-files did not exit properly");
246 strbuf_release(&buf);
249 static int ensure_leading_directories(char *path)
251 switch (safe_create_leading_directories(path)) {
252 case SCLD_OK:
253 case SCLD_EXISTS:
254 return 0;
255 default:
256 return error(_("could not create leading directories "
257 "of '%s'"), path);
262 * Unconditional writing of a plain regular file is what
263 * "git difftool --dir-diff" wants to do for symlinks. We are preparing two
264 * temporary directories to be fed to a Git-unaware tool that knows how to
265 * show a diff of two directories (e.g. "diff -r A B").
267 * Because the tool is Git-unaware, if a symbolic link appears in either of
268 * these temporary directories, it will try to dereference and show the
269 * difference of the target of the symbolic link, which is not what we want,
270 * as the goal of the dir-diff mode is to produce an output that is logically
271 * equivalent to what "git diff" produces.
273 * Most importantly, we want to get textual comparison of the result of the
274 * readlink(2). get_symlink() provides that---it returns the contents of
275 * the symlink that gets written to a regular file to force the external tool
276 * to compare the readlink(2) result as text, even on a filesystem that is
277 * capable of doing a symbolic link.
279 static char *get_symlink(const struct object_id *oid, const char *path)
281 char *data;
282 if (is_null_oid(oid)) {
283 /* The symlink is unknown to Git so read from the filesystem */
284 struct strbuf link = STRBUF_INIT;
285 if (has_symlinks) {
286 if (strbuf_readlink(&link, path, strlen(path)))
287 die(_("could not read symlink %s"), path);
288 } else if (strbuf_read_file(&link, path, 128))
289 die(_("could not read symlink file %s"), path);
291 data = strbuf_detach(&link, NULL);
292 } else {
293 enum object_type type;
294 unsigned long size;
295 data = read_object_file(oid, &type, &size);
296 if (!data)
297 die(_("could not read object %s for symlink %s"),
298 oid_to_hex(oid), path);
301 return data;
304 static int checkout_path(unsigned mode, struct object_id *oid,
305 const char *path, const struct checkout *state)
307 struct cache_entry *ce;
308 int ret;
310 ce = make_transient_cache_entry(mode, oid, path, 0, NULL);
311 ret = checkout_entry(ce, state, NULL, NULL);
313 discard_cache_entry(ce);
314 return ret;
317 static void write_file_in_directory(struct strbuf *dir, size_t dir_len,
318 const char *path, const char *content)
320 add_path(dir, dir_len, path);
321 ensure_leading_directories(dir->buf);
322 unlink(dir->buf);
323 write_file(dir->buf, "%s", content);
326 /* Write the file contents for the left and right sides of the difftool
327 * dir-diff representation for submodules and symlinks. Symlinks and submodules
328 * are written as regular text files so that external diff tools can diff them
329 * as text files, resulting in behavior that is analogous to to what "git diff"
330 * displays for symlink and submodule diffs.
332 static void write_standin_files(struct pair_entry *entry,
333 struct strbuf *ldir, size_t ldir_len,
334 struct strbuf *rdir, size_t rdir_len)
336 if (*entry->left)
337 write_file_in_directory(ldir, ldir_len, entry->path, entry->left);
338 if (*entry->right)
339 write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
342 static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
343 struct child_process *child)
345 struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
346 struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
347 struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
348 struct strbuf wtdir = STRBUF_INIT;
349 struct strbuf tmpdir = STRBUF_INIT;
350 char *lbase_dir = NULL, *rbase_dir = NULL;
351 size_t ldir_len, rdir_len, wtdir_len;
352 const char *workdir, *tmp;
353 int ret = 0, i;
354 FILE *fp = NULL;
355 struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
356 NULL);
357 struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
358 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
359 struct hashmap_iter iter;
360 struct pair_entry *entry;
361 struct index_state wtindex;
362 struct checkout lstate, rstate;
363 int flags = RUN_GIT_CMD, err = 0;
364 const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL };
365 struct hashmap wt_modified, tmp_modified;
366 int indices_loaded = 0;
368 workdir = get_git_work_tree();
370 /* Setup temp directories */
371 tmp = getenv("TMPDIR");
372 strbuf_add_absolute_path(&tmpdir, tmp ? tmp : "/tmp");
373 strbuf_trim_trailing_dir_sep(&tmpdir);
374 strbuf_addstr(&tmpdir, "/git-difftool.XXXXXX");
375 if (!mkdtemp(tmpdir.buf)) {
376 ret = error("could not create '%s'", tmpdir.buf);
377 goto finish;
379 strbuf_addf(&ldir, "%s/left/", tmpdir.buf);
380 strbuf_addf(&rdir, "%s/right/", tmpdir.buf);
381 strbuf_addstr(&wtdir, workdir);
382 if (!wtdir.len || !is_dir_sep(wtdir.buf[wtdir.len - 1]))
383 strbuf_addch(&wtdir, '/');
384 mkdir(ldir.buf, 0700);
385 mkdir(rdir.buf, 0700);
387 memset(&wtindex, 0, sizeof(wtindex));
389 memset(&lstate, 0, sizeof(lstate));
390 lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
391 lstate.base_dir_len = ldir.len;
392 lstate.force = 1;
393 memset(&rstate, 0, sizeof(rstate));
394 rstate.base_dir = rbase_dir = xstrdup(rdir.buf);
395 rstate.base_dir_len = rdir.len;
396 rstate.force = 1;
398 ldir_len = ldir.len;
399 rdir_len = rdir.len;
400 wtdir_len = wtdir.len;
402 child->no_stdin = 1;
403 child->git_cmd = 1;
404 child->use_shell = 0;
405 child->clean_on_exit = 1;
406 child->dir = prefix;
407 child->out = -1;
408 if (start_command(child))
409 die("could not obtain raw diff");
410 fp = xfdopen(child->out, "r");
412 /* Build index info for left and right sides of the diff */
413 i = 0;
414 while (!strbuf_getline_nul(&info, fp)) {
415 int lmode, rmode;
416 struct object_id loid, roid;
417 char status;
418 const char *src_path, *dst_path;
420 if (starts_with(info.buf, "::"))
421 die(N_("combined diff formats ('-c' and '--cc') are "
422 "not supported in\n"
423 "directory diff mode ('-d' and '--dir-diff')."));
425 if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
426 &status))
427 break;
428 if (strbuf_getline_nul(&lpath, fp))
429 break;
430 src_path = lpath.buf;
432 i++;
433 if (status != 'C' && status != 'R') {
434 dst_path = src_path;
435 } else {
436 if (strbuf_getline_nul(&rpath, fp))
437 break;
438 dst_path = rpath.buf;
441 if (S_ISGITLINK(lmode) || S_ISGITLINK(rmode)) {
442 strbuf_reset(&buf);
443 strbuf_addf(&buf, "Subproject commit %s",
444 oid_to_hex(&loid));
445 add_left_or_right(&submodules, src_path, buf.buf, 0);
446 strbuf_reset(&buf);
447 strbuf_addf(&buf, "Subproject commit %s",
448 oid_to_hex(&roid));
449 if (oideq(&loid, &roid))
450 strbuf_addstr(&buf, "-dirty");
451 add_left_or_right(&submodules, dst_path, buf.buf, 1);
452 continue;
455 if (S_ISLNK(lmode)) {
456 char *content = get_symlink(&loid, src_path);
457 add_left_or_right(&symlinks2, src_path, content, 0);
458 free(content);
461 if (S_ISLNK(rmode)) {
462 char *content = get_symlink(&roid, dst_path);
463 add_left_or_right(&symlinks2, dst_path, content, 1);
464 free(content);
467 if (lmode && status != 'C') {
468 if (checkout_path(lmode, &loid, src_path, &lstate)) {
469 ret = error("could not write '%s'", src_path);
470 goto finish;
474 if (rmode && !S_ISLNK(rmode)) {
475 struct working_tree_entry *entry;
477 /* Avoid duplicate working_tree entries */
478 FLEX_ALLOC_STR(entry, path, dst_path);
479 hashmap_entry_init(&entry->entry, strhash(dst_path));
480 if (hashmap_get(&working_tree_dups, &entry->entry,
481 NULL)) {
482 free(entry);
483 continue;
485 hashmap_add(&working_tree_dups, &entry->entry);
487 if (!use_wt_file(workdir, dst_path, &roid)) {
488 if (checkout_path(rmode, &roid, dst_path,
489 &rstate)) {
490 ret = error("could not write '%s'",
491 dst_path);
492 goto finish;
494 } else if (!is_null_oid(&roid)) {
496 * Changes in the working tree need special
497 * treatment since they are not part of the
498 * index.
500 struct cache_entry *ce2 =
501 make_cache_entry(&wtindex, rmode, &roid,
502 dst_path, 0, 0);
504 add_index_entry(&wtindex, ce2,
505 ADD_CACHE_JUST_APPEND);
507 add_path(&rdir, rdir_len, dst_path);
508 if (ensure_leading_directories(rdir.buf)) {
509 ret = error("could not create "
510 "directory for '%s'",
511 dst_path);
512 goto finish;
514 add_path(&wtdir, wtdir_len, dst_path);
515 if (symlinks) {
516 if (symlink(wtdir.buf, rdir.buf)) {
517 ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
518 goto finish;
520 } else {
521 struct stat st;
522 if (stat(wtdir.buf, &st))
523 st.st_mode = 0644;
524 if (copy_file(rdir.buf, wtdir.buf,
525 st.st_mode)) {
526 ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
527 goto finish;
534 fclose(fp);
535 fp = NULL;
536 if (finish_command(child)) {
537 ret = error("error occurred running diff --raw");
538 goto finish;
541 if (!i)
542 goto finish;
545 * Changes to submodules require special treatment.This loop writes a
546 * temporary file to both the left and right directories to show the
547 * change in the recorded SHA1 for the submodule.
549 hashmap_for_each_entry(&submodules, &iter, entry,
550 entry /* member name */) {
551 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
555 * Symbolic links require special treatment. The standard "git diff"
556 * shows only the link itself, not the contents of the link target.
557 * This loop replicates that behavior.
559 hashmap_for_each_entry(&symlinks2, &iter, entry,
560 entry /* member name */) {
562 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
565 strbuf_setlen(&ldir, ldir_len);
566 helper_argv[1] = ldir.buf;
567 strbuf_setlen(&rdir, rdir_len);
568 helper_argv[2] = rdir.buf;
570 if (extcmd) {
571 helper_argv[0] = extcmd;
572 flags = 0;
573 } else
574 setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
575 ret = run_command_v_opt(helper_argv, flags);
577 /* TODO: audit for interaction with sparse-index. */
578 ensure_full_index(&wtindex);
581 * If the diff includes working copy files and those
582 * files were modified during the diff, then the changes
583 * should be copied back to the working tree.
584 * Do not copy back files when symlinks are used and the
585 * external tool did not replace the original link with a file.
587 * These hashes are loaded lazily since they aren't needed
588 * in the common case of --symlinks and the difftool updating
589 * files through the symlink.
591 hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
592 hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
594 for (i = 0; i < wtindex.cache_nr; i++) {
595 struct hashmap_entry dummy;
596 const char *name = wtindex.cache[i]->name;
597 struct stat st;
599 add_path(&rdir, rdir_len, name);
600 if (lstat(rdir.buf, &st))
601 continue;
603 if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
604 continue;
606 if (!indices_loaded) {
607 struct lock_file lock = LOCK_INIT;
608 strbuf_reset(&buf);
609 strbuf_addf(&buf, "%s/wtindex", tmpdir.buf);
610 if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
611 write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
612 ret = error("could not write %s", buf.buf);
613 goto finish;
615 changed_files(&wt_modified, buf.buf, workdir);
616 strbuf_setlen(&rdir, rdir_len);
617 changed_files(&tmp_modified, buf.buf, rdir.buf);
618 add_path(&rdir, rdir_len, name);
619 indices_loaded = 1;
622 hashmap_entry_init(&dummy, strhash(name));
623 if (hashmap_get(&tmp_modified, &dummy, name)) {
624 add_path(&wtdir, wtdir_len, name);
625 if (hashmap_get(&wt_modified, &dummy, name)) {
626 warning(_("both files modified: '%s' and '%s'."),
627 wtdir.buf, rdir.buf);
628 warning(_("working tree file has been left."));
629 warning("%s", "");
630 err = 1;
631 } else if (unlink(wtdir.buf) ||
632 copy_file(wtdir.buf, rdir.buf, st.st_mode))
633 warning_errno(_("could not copy '%s' to '%s'"),
634 rdir.buf, wtdir.buf);
638 if (err) {
639 warning(_("temporary files exist in '%s'."), tmpdir.buf);
640 warning(_("you may want to cleanup or recover these."));
641 ret = 1;
642 } else {
643 remove_dir_recursively(&tmpdir, 0);
644 if (ret)
645 warning(_("failed: %d"), ret);
648 finish:
649 if (fp)
650 fclose(fp);
652 free(lbase_dir);
653 free(rbase_dir);
654 strbuf_release(&ldir);
655 strbuf_release(&rdir);
656 strbuf_release(&wtdir);
657 strbuf_release(&buf);
658 strbuf_release(&tmpdir);
660 return (ret < 0) ? 1 : ret;
663 static int run_file_diff(int prompt, const char *prefix,
664 struct child_process *child)
666 const char *env[] = {
667 "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
668 NULL
671 if (prompt > 0)
672 env[2] = "GIT_DIFFTOOL_PROMPT=true";
673 else if (!prompt)
674 env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";
676 child->git_cmd = 1;
677 child->dir = prefix;
678 strvec_pushv(&child->env_array, env);
680 return run_command(child);
683 int cmd_difftool(int argc, const char **argv, const char *prefix)
685 int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
686 tool_help = 0, no_index = 0;
687 static char *difftool_cmd = NULL, *extcmd = NULL;
688 struct option builtin_difftool_options[] = {
689 OPT_BOOL('g', "gui", &use_gui_tool,
690 N_("use `diff.guitool` instead of `diff.tool`")),
691 OPT_BOOL('d', "dir-diff", &dir_diff,
692 N_("perform a full-directory diff")),
693 OPT_SET_INT_F('y', "no-prompt", &prompt,
694 N_("do not prompt before launching a diff tool"),
695 0, PARSE_OPT_NONEG),
696 OPT_SET_INT_F(0, "prompt", &prompt, NULL,
697 1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
698 OPT_BOOL(0, "symlinks", &symlinks,
699 N_("use symlinks in dir-diff mode")),
700 OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
701 N_("use the specified diff tool")),
702 OPT_BOOL(0, "tool-help", &tool_help,
703 N_("print a list of diff tools that may be used with "
704 "`--tool`")),
705 OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
706 N_("make 'git-difftool' exit when an invoked diff "
707 "tool returns a non-zero exit code")),
708 OPT_STRING('x', "extcmd", &extcmd, N_("command"),
709 N_("specify a custom command for viewing diffs")),
710 OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
711 OPT_END()
713 struct child_process child = CHILD_PROCESS_INIT;
715 git_config(difftool_config, NULL);
716 symlinks = has_symlinks;
718 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
719 builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
720 PARSE_OPT_KEEP_DASHDASH);
722 if (tool_help)
723 return print_tool_help();
725 if (!no_index && !startup_info->have_repository)
726 die(_("difftool requires worktree or --no-index"));
728 if (!no_index){
729 setup_work_tree();
730 setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
731 setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
732 } else if (dir_diff)
733 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
735 if (use_gui_tool + !!difftool_cmd + !!extcmd > 1)
736 die(_("options '%s', '%s', and '%s' cannot be used together"), "--gui", "--tool", "--extcmd");
738 if (use_gui_tool)
739 setenv("GIT_MERGETOOL_GUI", "true", 1);
740 else if (difftool_cmd) {
741 if (*difftool_cmd)
742 setenv("GIT_DIFF_TOOL", difftool_cmd, 1);
743 else
744 die(_("no <tool> given for --tool=<tool>"));
747 if (extcmd) {
748 if (*extcmd)
749 setenv("GIT_DIFFTOOL_EXTCMD", extcmd, 1);
750 else
751 die(_("no <cmd> given for --extcmd=<cmd>"));
754 setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
755 trust_exit_code ? "true" : "false", 1);
758 * In directory diff mode, 'git-difftool--helper' is called once
759 * to compare the a / b directories. In file diff mode, 'git diff'
760 * will invoke a separate instance of 'git-difftool--helper' for
761 * each file that changed.
763 strvec_push(&child.args, "diff");
764 if (no_index)
765 strvec_push(&child.args, "--no-index");
766 if (dir_diff)
767 strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
768 strvec_pushv(&child.args, argv);
770 if (dir_diff)
771 return run_dir_diff(extcmd, symlinks, prefix, &child);
772 return run_file_diff(prompt, prefix, &child);