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
20 #include "run-command.h"
21 #include "environment.h"
24 #include "parse-options.h"
26 #include "read-cache-ll.h"
27 #include "repository.h"
28 #include "sparse-index.h"
32 #include "object-file.h"
33 #include "object-store.h"
38 static const char *const builtin_difftool_usage
[] = {
39 N_("git difftool [<options>] [<commit> [<commit>]] [--] [<path>...]"),
43 struct difftool_options
{
49 static int difftool_config(const char *var
, const char *value
,
50 const struct config_context
*ctx
, void *cb
)
52 struct difftool_options
*dt_options
= (struct difftool_options
*)cb
;
53 if (!strcmp(var
, "difftool.trustexitcode")) {
54 dt_options
->trust_exit_code
= git_config_bool(var
, value
);
57 if (!strcmp(var
, "core.symlinks")) {
58 dt_options
->has_symlinks
= git_config_bool(var
, value
);
62 return git_default_config(var
, value
, ctx
, cb
);
65 static int print_tool_help(void)
67 struct child_process cmd
= CHILD_PROCESS_INIT
;
70 strvec_pushl(&cmd
.args
, "mergetool", "--tool-help=diff", NULL
);
71 return run_command(&cmd
);
74 static int parse_index_info(struct repository
*repo
,
75 char *p
, int *mode1
, int *mode2
,
76 struct object_id
*oid1
, struct object_id
*oid2
,
80 return error("expected ':', got '%c'", *p
);
81 *mode1
= (int)strtol(p
+ 1, &p
, 8);
83 return error("expected ' ', got '%c'", *p
);
84 *mode2
= (int)strtol(p
+ 1, &p
, 8);
86 return error("expected ' ', got '%c'", *p
);
87 if (parse_oid_hex_algop(++p
, oid1
, (const char **)&p
, repo
->hash_algo
))
88 return error("expected object ID, got '%s'", p
);
90 return error("expected ' ', got '%c'", *p
);
91 if (parse_oid_hex_algop(++p
, oid2
, (const char **)&p
, repo
->hash_algo
))
92 return error("expected object ID, got '%s'", p
);
94 return error("expected ' ', got '%c'", *p
);
97 return error("missing status");
98 if (p
[1] && !isdigit(p
[1]))
99 return error("unexpected trailer: '%s'", p
+ 1);
104 * Remove any trailing slash from $workdir
105 * before starting to avoid double slashes in symlink targets.
107 static void add_path(struct strbuf
*buf
, size_t base_len
, const char *path
)
109 strbuf_setlen(buf
, base_len
);
110 if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '/')
111 strbuf_addch(buf
, '/');
112 strbuf_addstr(buf
, path
);
116 * Determine whether we can simply reuse the file in the worktree.
118 static int use_wt_file(struct repository
*repo
,
119 const char *workdir
, const char *name
,
120 struct object_id
*oid
)
122 struct strbuf buf
= STRBUF_INIT
;
126 strbuf_addstr(&buf
, workdir
);
127 add_path(&buf
, buf
.len
, name
);
129 if (!lstat(buf
.buf
, &st
) && !S_ISLNK(st
.st_mode
)) {
130 struct object_id wt_oid
;
131 int fd
= open(buf
.buf
, O_RDONLY
);
134 !index_fd(repo
->index
, &wt_oid
, fd
, &st
, OBJ_BLOB
, name
, 0)) {
135 if (is_null_oid(oid
)) {
136 oidcpy(oid
, &wt_oid
);
138 } else if (oideq(oid
, &wt_oid
))
143 strbuf_release(&buf
);
148 struct working_tree_entry
{
149 struct hashmap_entry entry
;
150 char path
[FLEX_ARRAY
];
153 static int working_tree_entry_cmp(const void *cmp_data UNUSED
,
154 const struct hashmap_entry
*eptr
,
155 const struct hashmap_entry
*entry_or_key
,
156 const void *keydata UNUSED
)
158 const struct working_tree_entry
*a
, *b
;
160 a
= container_of(eptr
, const struct working_tree_entry
, entry
);
161 b
= container_of(entry_or_key
, const struct working_tree_entry
, entry
);
163 return strcmp(a
->path
, b
->path
);
167 * The `left` and `right` entries hold paths for the symlinks hashmap,
168 * and a SHA-1 surrounded by brief text for submodules.
171 struct hashmap_entry entry
;
172 char left
[PATH_MAX
], right
[PATH_MAX
];
173 const char path
[FLEX_ARRAY
];
176 static int pair_cmp(const void *cmp_data UNUSED
,
177 const struct hashmap_entry
*eptr
,
178 const struct hashmap_entry
*entry_or_key
,
179 const void *keydata UNUSED
)
181 const struct pair_entry
*a
, *b
;
183 a
= container_of(eptr
, const struct pair_entry
, entry
);
184 b
= container_of(entry_or_key
, const struct pair_entry
, entry
);
186 return strcmp(a
->path
, b
->path
);
189 static void add_left_or_right(struct hashmap
*map
, const char *path
,
190 const char *content
, int is_right
)
192 struct pair_entry
*e
, *existing
;
194 FLEX_ALLOC_STR(e
, path
, path
);
195 hashmap_entry_init(&e
->entry
, strhash(path
));
196 existing
= hashmap_get_entry(map
, e
, entry
, NULL
);
201 e
->left
[0] = e
->right
[0] = '\0';
202 hashmap_add(map
, &e
->entry
);
204 strlcpy(is_right
? e
->right
: e
->left
, content
, PATH_MAX
);
208 struct hashmap_entry entry
;
209 char path
[FLEX_ARRAY
];
212 static int path_entry_cmp(const void *cmp_data UNUSED
,
213 const struct hashmap_entry
*eptr
,
214 const struct hashmap_entry
*entry_or_key
,
217 const struct path_entry
*a
, *b
;
219 a
= container_of(eptr
, const struct path_entry
, entry
);
220 b
= container_of(entry_or_key
, const struct path_entry
, entry
);
222 return strcmp(a
->path
, key
? key
: b
->path
);
225 static void changed_files(struct repository
*repo
,
226 struct hashmap
*result
, const char *index_path
,
229 struct child_process update_index
= CHILD_PROCESS_INIT
;
230 struct child_process diff_files
= CHILD_PROCESS_INIT
;
231 struct strbuf buf
= STRBUF_INIT
;
232 const char *git_dir
= absolute_path(repo_get_git_dir(repo
));
235 strvec_pushl(&update_index
.args
,
236 "--git-dir", git_dir
, "--work-tree", workdir
,
237 "update-index", "--really-refresh", "-q",
239 update_index
.no_stdin
= 1;
240 update_index
.no_stdout
= 1;
241 update_index
.no_stderr
= 1;
242 update_index
.git_cmd
= 1;
243 update_index
.use_shell
= 0;
244 update_index
.clean_on_exit
= 1;
245 update_index
.dir
= workdir
;
246 strvec_pushf(&update_index
.env
, "GIT_INDEX_FILE=%s", index_path
);
247 /* Ignore any errors of update-index */
248 run_command(&update_index
);
250 strvec_pushl(&diff_files
.args
,
251 "--git-dir", git_dir
, "--work-tree", workdir
,
252 "diff-files", "--name-only", "-z", NULL
);
253 diff_files
.no_stdin
= 1;
254 diff_files
.git_cmd
= 1;
255 diff_files
.use_shell
= 0;
256 diff_files
.clean_on_exit
= 1;
258 diff_files
.dir
= workdir
;
259 strvec_pushf(&diff_files
.env
, "GIT_INDEX_FILE=%s", index_path
);
260 if (start_command(&diff_files
))
261 die("could not obtain raw diff");
262 fp
= xfdopen(diff_files
.out
, "r");
263 while (!strbuf_getline_nul(&buf
, fp
)) {
264 struct path_entry
*entry
;
265 FLEX_ALLOC_STR(entry
, path
, buf
.buf
);
266 hashmap_entry_init(&entry
->entry
, strhash(buf
.buf
));
267 hashmap_add(result
, &entry
->entry
);
270 if (finish_command(&diff_files
))
271 die("diff-files did not exit properly");
272 strbuf_release(&buf
);
275 static int ensure_leading_directories(struct repository
*repo
, char *path
)
277 switch (safe_create_leading_directories(repo
, path
)) {
282 return error(_("could not create leading directories "
288 * Unconditional writing of a plain regular file is what
289 * "git difftool --dir-diff" wants to do for symlinks. We are preparing two
290 * temporary directories to be fed to a Git-unaware tool that knows how to
291 * show a diff of two directories (e.g. "diff -r A B").
293 * Because the tool is Git-unaware, if a symbolic link appears in either of
294 * these temporary directories, it will try to dereference and show the
295 * difference of the target of the symbolic link, which is not what we want,
296 * as the goal of the dir-diff mode is to produce an output that is logically
297 * equivalent to what "git diff" produces.
299 * Most importantly, we want to get textual comparison of the result of the
300 * readlink(2). get_symlink() provides that---it returns the contents of
301 * the symlink that gets written to a regular file to force the external tool
302 * to compare the readlink(2) result as text, even on a filesystem that is
303 * capable of doing a symbolic link.
305 static char *get_symlink(struct repository
*repo
,
306 struct difftool_options
*dt_options
,
307 const struct object_id
*oid
, const char *path
)
310 if (is_null_oid(oid
)) {
311 /* The symlink is unknown to Git so read from the filesystem */
312 struct strbuf link
= STRBUF_INIT
;
313 if (dt_options
->has_symlinks
) {
314 if (strbuf_readlink(&link
, path
, strlen(path
)))
315 die(_("could not read symlink %s"), path
);
316 } else if (strbuf_read_file(&link
, path
, 128))
317 die(_("could not read symlink file %s"), path
);
319 data
= strbuf_detach(&link
, NULL
);
321 enum object_type type
;
323 data
= repo_read_object_file(repo
, oid
, &type
, &size
);
325 die(_("could not read object %s for symlink %s"),
326 oid_to_hex(oid
), path
);
332 static int checkout_path(unsigned mode
, struct object_id
*oid
,
333 const char *path
, const struct checkout
*state
)
335 struct cache_entry
*ce
;
338 ce
= make_transient_cache_entry(mode
, oid
, path
, 0, NULL
);
339 ret
= checkout_entry(ce
, state
, NULL
, NULL
);
341 discard_cache_entry(ce
);
345 static void write_file_in_directory(struct repository
*repo
,
346 struct strbuf
*dir
, size_t dir_len
,
347 const char *path
, const char *content
)
349 add_path(dir
, dir_len
, path
);
350 ensure_leading_directories(repo
, dir
->buf
);
352 write_file(dir
->buf
, "%s", content
);
355 /* Write the file contents for the left and right sides of the difftool
356 * dir-diff representation for submodules and symlinks. Symlinks and submodules
357 * are written as regular text files so that external diff tools can diff them
358 * as text files, resulting in behavior that is analogous to what "git diff"
359 * displays for symlink and submodule diffs.
361 static void write_standin_files(struct repository
*repo
,
362 struct pair_entry
*entry
,
363 struct strbuf
*ldir
, size_t ldir_len
,
364 struct strbuf
*rdir
, size_t rdir_len
)
367 write_file_in_directory(repo
, ldir
, ldir_len
, entry
->path
, entry
->left
);
369 write_file_in_directory(repo
, rdir
, rdir_len
, entry
->path
, entry
->right
);
372 static int run_dir_diff(struct repository
*repo
,
373 struct difftool_options
*dt_options
,
374 const char *extcmd
, const char *prefix
,
375 struct child_process
*child
)
377 struct strbuf info
= STRBUF_INIT
, lpath
= STRBUF_INIT
;
378 struct strbuf rpath
= STRBUF_INIT
, buf
= STRBUF_INIT
;
379 struct strbuf ldir
= STRBUF_INIT
, rdir
= STRBUF_INIT
;
380 struct strbuf wtdir
= STRBUF_INIT
;
381 struct strbuf tmpdir
= STRBUF_INIT
;
382 char *lbase_dir
= NULL
, *rbase_dir
= NULL
;
383 size_t ldir_len
, rdir_len
, wtdir_len
;
384 const char *workdir
, *tmp
;
388 struct hashmap working_tree_dups
= HASHMAP_INIT(working_tree_entry_cmp
,
390 struct hashmap submodules
= HASHMAP_INIT(pair_cmp
, NULL
);
391 struct hashmap symlinks2
= HASHMAP_INIT(pair_cmp
, NULL
);
392 struct hashmap_iter iter
;
393 struct pair_entry
*entry
;
394 struct index_state wtindex
= INDEX_STATE_INIT(repo
);
395 struct checkout lstate
, rstate
;
397 struct child_process cmd
= CHILD_PROCESS_INIT
;
398 struct hashmap wt_modified
= HASHMAP_INIT(path_entry_cmp
, NULL
);
399 struct hashmap tmp_modified
= HASHMAP_INIT(path_entry_cmp
, NULL
);
400 int indices_loaded
= 0;
402 workdir
= repo_get_work_tree(repo
);
404 /* Setup temp directories */
405 tmp
= getenv("TMPDIR");
406 strbuf_add_absolute_path(&tmpdir
, tmp
? tmp
: "/tmp");
407 strbuf_trim_trailing_dir_sep(&tmpdir
);
408 strbuf_addstr(&tmpdir
, "/git-difftool.XXXXXX");
409 if (!mkdtemp(tmpdir
.buf
)) {
410 ret
= error("could not create '%s'", tmpdir
.buf
);
413 strbuf_addf(&ldir
, "%s/left/", tmpdir
.buf
);
414 strbuf_addf(&rdir
, "%s/right/", tmpdir
.buf
);
415 strbuf_addstr(&wtdir
, workdir
);
416 if (!wtdir
.len
|| !is_dir_sep(wtdir
.buf
[wtdir
.len
- 1]))
417 strbuf_addch(&wtdir
, '/');
418 mkdir(ldir
.buf
, 0700);
419 mkdir(rdir
.buf
, 0700);
421 memset(&lstate
, 0, sizeof(lstate
));
422 lstate
.base_dir
= lbase_dir
= xstrdup(ldir
.buf
);
423 lstate
.base_dir_len
= ldir
.len
;
425 memset(&rstate
, 0, sizeof(rstate
));
426 rstate
.base_dir
= rbase_dir
= xstrdup(rdir
.buf
);
427 rstate
.base_dir_len
= rdir
.len
;
432 wtdir_len
= wtdir
.len
;
436 child
->use_shell
= 0;
437 child
->clean_on_exit
= 1;
440 if (start_command(child
))
441 die("could not obtain raw diff");
442 fp
= xfdopen(child
->out
, "r");
444 /* Build index info for left and right sides of the diff */
446 while (!strbuf_getline_nul(&info
, fp
)) {
448 struct object_id loid
, roid
;
450 const char *src_path
, *dst_path
;
452 if (starts_with(info
.buf
, "::"))
453 die(N_("combined diff formats ('-c' and '--cc') are "
455 "directory diff mode ('-d' and '--dir-diff')."));
457 if (parse_index_info(repo
, info
.buf
, &lmode
, &rmode
, &loid
, &roid
, &status
))
459 if (strbuf_getline_nul(&lpath
, fp
))
461 src_path
= lpath
.buf
;
464 if (status
!= 'C' && status
!= 'R') {
467 if (strbuf_getline_nul(&rpath
, fp
))
469 dst_path
= rpath
.buf
;
472 if (S_ISGITLINK(lmode
) || S_ISGITLINK(rmode
)) {
474 strbuf_addf(&buf
, "Subproject commit %s",
476 add_left_or_right(&submodules
, src_path
, buf
.buf
, 0);
478 strbuf_addf(&buf
, "Subproject commit %s",
480 if (oideq(&loid
, &roid
))
481 strbuf_addstr(&buf
, "-dirty");
482 add_left_or_right(&submodules
, dst_path
, buf
.buf
, 1);
486 if (S_ISLNK(lmode
)) {
487 char *content
= get_symlink(repo
, dt_options
, &loid
, src_path
);
488 add_left_or_right(&symlinks2
, src_path
, content
, 0);
492 if (S_ISLNK(rmode
)) {
493 char *content
= get_symlink(repo
, dt_options
, &roid
, dst_path
);
494 add_left_or_right(&symlinks2
, dst_path
, content
, 1);
498 if (lmode
&& status
!= 'C') {
499 if (checkout_path(lmode
, &loid
, src_path
, &lstate
)) {
500 ret
= error("could not write '%s'", src_path
);
505 if (rmode
&& !S_ISLNK(rmode
)) {
506 struct working_tree_entry
*entry
;
508 /* Avoid duplicate working_tree entries */
509 FLEX_ALLOC_STR(entry
, path
, dst_path
);
510 hashmap_entry_init(&entry
->entry
, strhash(dst_path
));
511 if (hashmap_get(&working_tree_dups
, &entry
->entry
,
516 hashmap_add(&working_tree_dups
, &entry
->entry
);
518 if (!use_wt_file(repo
, workdir
, dst_path
, &roid
)) {
519 if (checkout_path(rmode
, &roid
, dst_path
,
521 ret
= error("could not write '%s'",
525 } else if (!is_null_oid(&roid
)) {
527 * Changes in the working tree need special
528 * treatment since they are not part of the
531 struct cache_entry
*ce2
=
532 make_cache_entry(&wtindex
, rmode
, &roid
,
535 add_index_entry(&wtindex
, ce2
,
536 ADD_CACHE_JUST_APPEND
);
538 add_path(&rdir
, rdir_len
, dst_path
);
539 if (ensure_leading_directories(repo
, rdir
.buf
)) {
540 ret
= error("could not create "
541 "directory for '%s'",
545 add_path(&wtdir
, wtdir_len
, dst_path
);
546 if (dt_options
->symlinks
) {
547 if (symlink(wtdir
.buf
, rdir
.buf
)) {
548 ret
= error_errno("could not symlink '%s' to '%s'", wtdir
.buf
, rdir
.buf
);
553 if (stat(wtdir
.buf
, &st
))
555 if (copy_file(rdir
.buf
, wtdir
.buf
,
557 ret
= error("could not copy '%s' to '%s'", wtdir
.buf
, rdir
.buf
);
567 if (finish_command(child
)) {
568 ret
= error("error occurred running diff --raw");
576 * Changes to submodules require special treatment.This loop writes a
577 * temporary file to both the left and right directories to show the
578 * change in the recorded SHA1 for the submodule.
580 hashmap_for_each_entry(&submodules
, &iter
, entry
,
581 entry
/* member name */) {
582 write_standin_files(repo
, entry
, &ldir
, ldir_len
, &rdir
, rdir_len
);
586 * Symbolic links require special treatment. The standard "git diff"
587 * shows only the link itself, not the contents of the link target.
588 * This loop replicates that behavior.
590 hashmap_for_each_entry(&symlinks2
, &iter
, entry
,
591 entry
/* member name */) {
593 write_standin_files(repo
, entry
, &ldir
, ldir_len
, &rdir
, rdir_len
);
596 strbuf_setlen(&ldir
, ldir_len
);
597 strbuf_setlen(&rdir
, rdir_len
);
600 strvec_push(&cmd
.args
, extcmd
);
602 strvec_push(&cmd
.args
, "difftool--helper");
604 setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
606 strvec_pushl(&cmd
.args
, ldir
.buf
, rdir
.buf
, NULL
);
607 ret
= run_command(&cmd
);
609 /* TODO: audit for interaction with sparse-index. */
610 ensure_full_index(&wtindex
);
613 * If the diff includes working copy files and those
614 * files were modified during the diff, then the changes
615 * should be copied back to the working tree.
616 * Do not copy back files when symlinks are used and the
617 * external tool did not replace the original link with a file.
619 * These hashes are loaded lazily since they aren't needed
620 * in the common case of --symlinks and the difftool updating
621 * files through the symlink.
623 for (i
= 0; i
< wtindex
.cache_nr
; i
++) {
624 struct hashmap_entry dummy
;
625 const char *name
= wtindex
.cache
[i
]->name
;
628 add_path(&rdir
, rdir_len
, name
);
629 if (lstat(rdir
.buf
, &st
))
632 if ((dt_options
->symlinks
&& S_ISLNK(st
.st_mode
)) || !S_ISREG(st
.st_mode
))
635 if (!indices_loaded
) {
636 struct lock_file lock
= LOCK_INIT
;
638 strbuf_addf(&buf
, "%s/wtindex", tmpdir
.buf
);
639 if (hold_lock_file_for_update(&lock
, buf
.buf
, 0) < 0 ||
640 write_locked_index(&wtindex
, &lock
, COMMIT_LOCK
)) {
641 ret
= error("could not write %s", buf
.buf
);
644 changed_files(repo
, &wt_modified
, buf
.buf
, workdir
);
645 strbuf_setlen(&rdir
, rdir_len
);
646 changed_files(repo
, &tmp_modified
, buf
.buf
, rdir
.buf
);
647 add_path(&rdir
, rdir_len
, name
);
651 hashmap_entry_init(&dummy
, strhash(name
));
652 if (hashmap_get(&tmp_modified
, &dummy
, name
)) {
653 add_path(&wtdir
, wtdir_len
, name
);
654 if (hashmap_get(&wt_modified
, &dummy
, name
)) {
655 warning(_("both files modified: '%s' and '%s'."),
656 wtdir
.buf
, rdir
.buf
);
657 warning(_("working tree file has been left."));
660 } else if (unlink(wtdir
.buf
) ||
661 copy_file(wtdir
.buf
, rdir
.buf
, st
.st_mode
))
662 warning_errno(_("could not copy '%s' to '%s'"),
663 rdir
.buf
, wtdir
.buf
);
668 warning(_("temporary files exist in '%s'."), tmpdir
.buf
);
669 warning(_("you may want to cleanup or recover these."));
672 remove_dir_recursively(&tmpdir
, 0);
674 warning(_("failed: %d"), ret
);
681 hashmap_clear_and_free(&working_tree_dups
, struct working_tree_entry
, entry
);
682 hashmap_clear_and_free(&wt_modified
, struct path_entry
, entry
);
683 hashmap_clear_and_free(&tmp_modified
, struct path_entry
, entry
);
684 hashmap_clear_and_free(&submodules
, struct pair_entry
, entry
);
685 hashmap_clear_and_free(&symlinks2
, struct pair_entry
, entry
);
686 release_index(&wtindex
);
689 strbuf_release(&info
);
690 strbuf_release(&lpath
);
691 strbuf_release(&rpath
);
692 strbuf_release(&ldir
);
693 strbuf_release(&rdir
);
694 strbuf_release(&wtdir
);
695 strbuf_release(&buf
);
696 strbuf_release(&tmpdir
);
698 return (ret
< 0) ? 1 : ret
;
701 static int run_file_diff(int prompt
, const char *prefix
,
702 struct child_process
*child
)
704 strvec_push(&child
->env
, "GIT_PAGER=");
705 strvec_push(&child
->env
, "GIT_EXTERNAL_DIFF=git-difftool--helper");
707 strvec_push(&child
->env
, "GIT_DIFFTOOL_PROMPT=true");
709 strvec_push(&child
->env
, "GIT_DIFFTOOL_NO_PROMPT=true");
714 return run_command(child
);
717 int cmd_difftool(int argc
,
720 struct repository
*repo
)
722 int use_gui_tool
= -1, dir_diff
= 0, prompt
= -1, tool_help
= 0, no_index
= 0;
723 static char *difftool_cmd
= NULL
, *extcmd
= NULL
;
724 struct difftool_options dt_options
= {
729 struct option builtin_difftool_options
[] = {
730 OPT_BOOL('g', "gui", &use_gui_tool
,
731 N_("use `diff.guitool` instead of `diff.tool`")),
732 OPT_BOOL('d', "dir-diff", &dir_diff
,
733 N_("perform a full-directory diff")),
734 OPT_SET_INT_F('y', "no-prompt", &prompt
,
735 N_("do not prompt before launching a diff tool"),
737 OPT_SET_INT_F(0, "prompt", &prompt
, NULL
,
738 1, PARSE_OPT_NONEG
| PARSE_OPT_HIDDEN
),
739 OPT_BOOL(0, "symlinks", &dt_options
.symlinks
,
740 N_("use symlinks in dir-diff mode")),
741 OPT_STRING('t', "tool", &difftool_cmd
, N_("tool"),
742 N_("use the specified diff tool")),
743 OPT_BOOL(0, "tool-help", &tool_help
,
744 N_("print a list of diff tools that may be used with "
746 OPT_BOOL(0, "trust-exit-code", &dt_options
.trust_exit_code
,
747 N_("make 'git-difftool' exit when an invoked diff "
748 "tool returns a non-zero exit code")),
749 OPT_STRING('x', "extcmd", &extcmd
, N_("command"),
750 N_("specify a custom command for viewing diffs")),
751 OPT_BOOL(0, "no-index", &no_index
, N_("passed to `diff`")),
754 struct child_process child
= CHILD_PROCESS_INIT
;
756 repo_config(repo
, difftool_config
, &dt_options
);
757 dt_options
.symlinks
= dt_options
.has_symlinks
;
759 argc
= parse_options(argc
, argv
, prefix
, builtin_difftool_options
,
760 builtin_difftool_usage
, PARSE_OPT_KEEP_UNKNOWN_OPT
|
761 PARSE_OPT_KEEP_DASHDASH
);
764 return print_tool_help();
766 if (!no_index
&& !startup_info
->have_repository
)
767 die(_("difftool requires worktree or --no-index"));
771 setenv(GIT_DIR_ENVIRONMENT
, absolute_path(repo_get_git_dir(repo
)), 1);
772 setenv(GIT_WORK_TREE_ENVIRONMENT
, absolute_path(repo_get_work_tree(repo
)), 1);
774 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
776 die_for_incompatible_opt3(use_gui_tool
== 1, "--gui",
777 !!difftool_cmd
, "--tool",
778 !!extcmd
, "--extcmd");
781 * Explicitly specified GUI option is forwarded to git-mergetool--lib.sh;
782 * empty or unset means "use the difftool.guiDefault config or default to
785 if (use_gui_tool
== 1)
786 setenv("GIT_MERGETOOL_GUI", "true", 1);
787 else if (use_gui_tool
== 0)
788 setenv("GIT_MERGETOOL_GUI", "false", 1);
792 setenv("GIT_DIFF_TOOL", difftool_cmd
, 1);
794 die(_("no <tool> given for --tool=<tool>"));
799 setenv("GIT_DIFFTOOL_EXTCMD", extcmd
, 1);
801 die(_("no <cmd> given for --extcmd=<cmd>"));
804 setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
805 dt_options
.trust_exit_code
? "true" : "false", 1);
808 * In directory diff mode, 'git-difftool--helper' is called once
809 * to compare the a / b directories. In file diff mode, 'git diff'
810 * will invoke a separate instance of 'git-difftool--helper' for
811 * each file that changed.
813 strvec_push(&child
.args
, "diff");
815 strvec_push(&child
.args
, "--no-index");
817 strvec_pushl(&child
.args
, "--raw", "--no-abbrev", "-z", NULL
);
818 strvec_pushv(&child
.args
, argv
);
821 return run_dir_diff(repo
, &dt_options
, extcmd
, prefix
, &child
);
822 return run_file_diff(prompt
, prefix
, &child
);