trace.c, git.c: remove unnecessary parameter to trace_repo_setup()
[git/debian.git] / builtin / difftool.c
blobdbbfb19f1921028f78b499af0ad35a67c9595d59
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 "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 struct child_process cmd = CHILD_PROCESS_INIT;
49 cmd.git_cmd = 1;
50 strvec_pushl(&cmd.args, "mergetool", "--tool-help=diff", NULL);
51 return run_command(&cmd);
54 static int parse_index_info(char *p, int *mode1, int *mode2,
55 struct object_id *oid1, struct object_id *oid2,
56 char *status)
58 if (*p != ':')
59 return error("expected ':', got '%c'", *p);
60 *mode1 = (int)strtol(p + 1, &p, 8);
61 if (*p != ' ')
62 return error("expected ' ', got '%c'", *p);
63 *mode2 = (int)strtol(p + 1, &p, 8);
64 if (*p != ' ')
65 return error("expected ' ', got '%c'", *p);
66 if (parse_oid_hex(++p, oid1, (const char **)&p))
67 return error("expected object ID, got '%s'", p);
68 if (*p != ' ')
69 return error("expected ' ', got '%c'", *p);
70 if (parse_oid_hex(++p, oid2, (const char **)&p))
71 return error("expected object ID, got '%s'", p);
72 if (*p != ' ')
73 return error("expected ' ', got '%c'", *p);
74 *status = *++p;
75 if (!*status)
76 return error("missing status");
77 if (p[1] && !isdigit(p[1]))
78 return error("unexpected trailer: '%s'", p + 1);
79 return 0;
83 * Remove any trailing slash from $workdir
84 * before starting to avoid double slashes in symlink targets.
86 static void add_path(struct strbuf *buf, size_t base_len, const char *path)
88 strbuf_setlen(buf, base_len);
89 if (buf->len && buf->buf[buf->len - 1] != '/')
90 strbuf_addch(buf, '/');
91 strbuf_addstr(buf, path);
95 * Determine whether we can simply reuse the file in the worktree.
97 static int use_wt_file(const char *workdir, const char *name,
98 struct object_id *oid)
100 struct strbuf buf = STRBUF_INIT;
101 struct stat st;
102 int use = 0;
104 strbuf_addstr(&buf, workdir);
105 add_path(&buf, buf.len, name);
107 if (!lstat(buf.buf, &st) && !S_ISLNK(st.st_mode)) {
108 struct object_id wt_oid;
109 int fd = open(buf.buf, O_RDONLY);
111 if (fd >= 0 &&
112 !index_fd(&the_index, &wt_oid, fd, &st, OBJ_BLOB, name, 0)) {
113 if (is_null_oid(oid)) {
114 oidcpy(oid, &wt_oid);
115 use = 1;
116 } else if (oideq(oid, &wt_oid))
117 use = 1;
121 strbuf_release(&buf);
123 return use;
126 struct working_tree_entry {
127 struct hashmap_entry entry;
128 char path[FLEX_ARRAY];
131 static int working_tree_entry_cmp(const void *cmp_data UNUSED,
132 const struct hashmap_entry *eptr,
133 const struct hashmap_entry *entry_or_key,
134 const void *keydata UNUSED)
136 const struct working_tree_entry *a, *b;
138 a = container_of(eptr, const struct working_tree_entry, entry);
139 b = container_of(entry_or_key, const struct working_tree_entry, entry);
141 return strcmp(a->path, b->path);
145 * The `left` and `right` entries hold paths for the symlinks hashmap,
146 * and a SHA-1 surrounded by brief text for submodules.
148 struct pair_entry {
149 struct hashmap_entry entry;
150 char left[PATH_MAX], right[PATH_MAX];
151 const char path[FLEX_ARRAY];
154 static int pair_cmp(const void *cmp_data UNUSED,
155 const struct hashmap_entry *eptr,
156 const struct hashmap_entry *entry_or_key,
157 const void *keydata UNUSED)
159 const struct pair_entry *a, *b;
161 a = container_of(eptr, const struct pair_entry, entry);
162 b = container_of(entry_or_key, const struct pair_entry, entry);
164 return strcmp(a->path, b->path);
167 static void add_left_or_right(struct hashmap *map, const char *path,
168 const char *content, int is_right)
170 struct pair_entry *e, *existing;
172 FLEX_ALLOC_STR(e, path, path);
173 hashmap_entry_init(&e->entry, strhash(path));
174 existing = hashmap_get_entry(map, e, entry, NULL);
175 if (existing) {
176 free(e);
177 e = existing;
178 } else {
179 e->left[0] = e->right[0] = '\0';
180 hashmap_add(map, &e->entry);
182 strlcpy(is_right ? e->right : e->left, content, PATH_MAX);
185 struct path_entry {
186 struct hashmap_entry entry;
187 char path[FLEX_ARRAY];
190 static int path_entry_cmp(const void *cmp_data UNUSED,
191 const struct hashmap_entry *eptr,
192 const struct hashmap_entry *entry_or_key,
193 const void *key)
195 const struct path_entry *a, *b;
197 a = container_of(eptr, const struct path_entry, entry);
198 b = container_of(entry_or_key, const struct path_entry, entry);
200 return strcmp(a->path, key ? key : b->path);
203 static void changed_files(struct hashmap *result, const char *index_path,
204 const char *workdir)
206 struct child_process update_index = CHILD_PROCESS_INIT;
207 struct child_process diff_files = CHILD_PROCESS_INIT;
208 struct strbuf buf = STRBUF_INIT;
209 const char *git_dir = absolute_path(get_git_dir());
210 FILE *fp;
212 strvec_pushl(&update_index.args,
213 "--git-dir", git_dir, "--work-tree", workdir,
214 "update-index", "--really-refresh", "-q",
215 "--unmerged", NULL);
216 update_index.no_stdin = 1;
217 update_index.no_stdout = 1;
218 update_index.no_stderr = 1;
219 update_index.git_cmd = 1;
220 update_index.use_shell = 0;
221 update_index.clean_on_exit = 1;
222 update_index.dir = workdir;
223 strvec_pushf(&update_index.env, "GIT_INDEX_FILE=%s", index_path);
224 /* Ignore any errors of update-index */
225 run_command(&update_index);
227 strvec_pushl(&diff_files.args,
228 "--git-dir", git_dir, "--work-tree", workdir,
229 "diff-files", "--name-only", "-z", NULL);
230 diff_files.no_stdin = 1;
231 diff_files.git_cmd = 1;
232 diff_files.use_shell = 0;
233 diff_files.clean_on_exit = 1;
234 diff_files.out = -1;
235 diff_files.dir = workdir;
236 strvec_pushf(&diff_files.env, "GIT_INDEX_FILE=%s", index_path);
237 if (start_command(&diff_files))
238 die("could not obtain raw diff");
239 fp = xfdopen(diff_files.out, "r");
240 while (!strbuf_getline_nul(&buf, fp)) {
241 struct path_entry *entry;
242 FLEX_ALLOC_STR(entry, path, buf.buf);
243 hashmap_entry_init(&entry->entry, strhash(buf.buf));
244 hashmap_add(result, &entry->entry);
246 fclose(fp);
247 if (finish_command(&diff_files))
248 die("diff-files did not exit properly");
249 strbuf_release(&buf);
252 static int ensure_leading_directories(char *path)
254 switch (safe_create_leading_directories(path)) {
255 case SCLD_OK:
256 case SCLD_EXISTS:
257 return 0;
258 default:
259 return error(_("could not create leading directories "
260 "of '%s'"), path);
265 * Unconditional writing of a plain regular file is what
266 * "git difftool --dir-diff" wants to do for symlinks. We are preparing two
267 * temporary directories to be fed to a Git-unaware tool that knows how to
268 * show a diff of two directories (e.g. "diff -r A B").
270 * Because the tool is Git-unaware, if a symbolic link appears in either of
271 * these temporary directories, it will try to dereference and show the
272 * difference of the target of the symbolic link, which is not what we want,
273 * as the goal of the dir-diff mode is to produce an output that is logically
274 * equivalent to what "git diff" produces.
276 * Most importantly, we want to get textual comparison of the result of the
277 * readlink(2). get_symlink() provides that---it returns the contents of
278 * the symlink that gets written to a regular file to force the external tool
279 * to compare the readlink(2) result as text, even on a filesystem that is
280 * capable of doing a symbolic link.
282 static char *get_symlink(const struct object_id *oid, const char *path)
284 char *data;
285 if (is_null_oid(oid)) {
286 /* The symlink is unknown to Git so read from the filesystem */
287 struct strbuf link = STRBUF_INIT;
288 if (has_symlinks) {
289 if (strbuf_readlink(&link, path, strlen(path)))
290 die(_("could not read symlink %s"), path);
291 } else if (strbuf_read_file(&link, path, 128))
292 die(_("could not read symlink file %s"), path);
294 data = strbuf_detach(&link, NULL);
295 } else {
296 enum object_type type;
297 unsigned long size;
298 data = read_object_file(oid, &type, &size);
299 if (!data)
300 die(_("could not read object %s for symlink %s"),
301 oid_to_hex(oid), path);
304 return data;
307 static int checkout_path(unsigned mode, struct object_id *oid,
308 const char *path, const struct checkout *state)
310 struct cache_entry *ce;
311 int ret;
313 ce = make_transient_cache_entry(mode, oid, path, 0, NULL);
314 ret = checkout_entry(ce, state, NULL, NULL);
316 discard_cache_entry(ce);
317 return ret;
320 static void write_file_in_directory(struct strbuf *dir, size_t dir_len,
321 const char *path, const char *content)
323 add_path(dir, dir_len, path);
324 ensure_leading_directories(dir->buf);
325 unlink(dir->buf);
326 write_file(dir->buf, "%s", content);
329 /* Write the file contents for the left and right sides of the difftool
330 * dir-diff representation for submodules and symlinks. Symlinks and submodules
331 * are written as regular text files so that external diff tools can diff them
332 * as text files, resulting in behavior that is analogous to to what "git diff"
333 * displays for symlink and submodule diffs.
335 static void write_standin_files(struct pair_entry *entry,
336 struct strbuf *ldir, size_t ldir_len,
337 struct strbuf *rdir, size_t rdir_len)
339 if (*entry->left)
340 write_file_in_directory(ldir, ldir_len, entry->path, entry->left);
341 if (*entry->right)
342 write_file_in_directory(rdir, rdir_len, entry->path, entry->right);
345 static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
346 struct child_process *child)
348 struct strbuf info = STRBUF_INIT, lpath = STRBUF_INIT;
349 struct strbuf rpath = STRBUF_INIT, buf = STRBUF_INIT;
350 struct strbuf ldir = STRBUF_INIT, rdir = STRBUF_INIT;
351 struct strbuf wtdir = STRBUF_INIT;
352 struct strbuf tmpdir = STRBUF_INIT;
353 char *lbase_dir = NULL, *rbase_dir = NULL;
354 size_t ldir_len, rdir_len, wtdir_len;
355 const char *workdir, *tmp;
356 int ret = 0, i;
357 FILE *fp = NULL;
358 struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
359 NULL);
360 struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
361 struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
362 struct hashmap_iter iter;
363 struct pair_entry *entry;
364 struct index_state wtindex = INDEX_STATE_INIT(the_repository);
365 struct checkout lstate, rstate;
366 int err = 0;
367 struct child_process cmd = CHILD_PROCESS_INIT;
368 struct hashmap wt_modified, tmp_modified;
369 int indices_loaded = 0;
371 workdir = get_git_work_tree();
373 /* Setup temp directories */
374 tmp = getenv("TMPDIR");
375 strbuf_add_absolute_path(&tmpdir, tmp ? tmp : "/tmp");
376 strbuf_trim_trailing_dir_sep(&tmpdir);
377 strbuf_addstr(&tmpdir, "/git-difftool.XXXXXX");
378 if (!mkdtemp(tmpdir.buf)) {
379 ret = error("could not create '%s'", tmpdir.buf);
380 goto finish;
382 strbuf_addf(&ldir, "%s/left/", tmpdir.buf);
383 strbuf_addf(&rdir, "%s/right/", tmpdir.buf);
384 strbuf_addstr(&wtdir, workdir);
385 if (!wtdir.len || !is_dir_sep(wtdir.buf[wtdir.len - 1]))
386 strbuf_addch(&wtdir, '/');
387 mkdir(ldir.buf, 0700);
388 mkdir(rdir.buf, 0700);
390 memset(&lstate, 0, sizeof(lstate));
391 lstate.base_dir = lbase_dir = xstrdup(ldir.buf);
392 lstate.base_dir_len = ldir.len;
393 lstate.force = 1;
394 memset(&rstate, 0, sizeof(rstate));
395 rstate.base_dir = rbase_dir = xstrdup(rdir.buf);
396 rstate.base_dir_len = rdir.len;
397 rstate.force = 1;
399 ldir_len = ldir.len;
400 rdir_len = rdir.len;
401 wtdir_len = wtdir.len;
403 child->no_stdin = 1;
404 child->git_cmd = 1;
405 child->use_shell = 0;
406 child->clean_on_exit = 1;
407 child->dir = prefix;
408 child->out = -1;
409 if (start_command(child))
410 die("could not obtain raw diff");
411 fp = xfdopen(child->out, "r");
413 /* Build index info for left and right sides of the diff */
414 i = 0;
415 while (!strbuf_getline_nul(&info, fp)) {
416 int lmode, rmode;
417 struct object_id loid, roid;
418 char status;
419 const char *src_path, *dst_path;
421 if (starts_with(info.buf, "::"))
422 die(N_("combined diff formats ('-c' and '--cc') are "
423 "not supported in\n"
424 "directory diff mode ('-d' and '--dir-diff')."));
426 if (parse_index_info(info.buf, &lmode, &rmode, &loid, &roid,
427 &status))
428 break;
429 if (strbuf_getline_nul(&lpath, fp))
430 break;
431 src_path = lpath.buf;
433 i++;
434 if (status != 'C' && status != 'R') {
435 dst_path = src_path;
436 } else {
437 if (strbuf_getline_nul(&rpath, fp))
438 break;
439 dst_path = rpath.buf;
442 if (S_ISGITLINK(lmode) || S_ISGITLINK(rmode)) {
443 strbuf_reset(&buf);
444 strbuf_addf(&buf, "Subproject commit %s",
445 oid_to_hex(&loid));
446 add_left_or_right(&submodules, src_path, buf.buf, 0);
447 strbuf_reset(&buf);
448 strbuf_addf(&buf, "Subproject commit %s",
449 oid_to_hex(&roid));
450 if (oideq(&loid, &roid))
451 strbuf_addstr(&buf, "-dirty");
452 add_left_or_right(&submodules, dst_path, buf.buf, 1);
453 continue;
456 if (S_ISLNK(lmode)) {
457 char *content = get_symlink(&loid, src_path);
458 add_left_or_right(&symlinks2, src_path, content, 0);
459 free(content);
462 if (S_ISLNK(rmode)) {
463 char *content = get_symlink(&roid, dst_path);
464 add_left_or_right(&symlinks2, dst_path, content, 1);
465 free(content);
468 if (lmode && status != 'C') {
469 if (checkout_path(lmode, &loid, src_path, &lstate)) {
470 ret = error("could not write '%s'", src_path);
471 goto finish;
475 if (rmode && !S_ISLNK(rmode)) {
476 struct working_tree_entry *entry;
478 /* Avoid duplicate working_tree entries */
479 FLEX_ALLOC_STR(entry, path, dst_path);
480 hashmap_entry_init(&entry->entry, strhash(dst_path));
481 if (hashmap_get(&working_tree_dups, &entry->entry,
482 NULL)) {
483 free(entry);
484 continue;
486 hashmap_add(&working_tree_dups, &entry->entry);
488 if (!use_wt_file(workdir, dst_path, &roid)) {
489 if (checkout_path(rmode, &roid, dst_path,
490 &rstate)) {
491 ret = error("could not write '%s'",
492 dst_path);
493 goto finish;
495 } else if (!is_null_oid(&roid)) {
497 * Changes in the working tree need special
498 * treatment since they are not part of the
499 * index.
501 struct cache_entry *ce2 =
502 make_cache_entry(&wtindex, rmode, &roid,
503 dst_path, 0, 0);
505 add_index_entry(&wtindex, ce2,
506 ADD_CACHE_JUST_APPEND);
508 add_path(&rdir, rdir_len, dst_path);
509 if (ensure_leading_directories(rdir.buf)) {
510 ret = error("could not create "
511 "directory for '%s'",
512 dst_path);
513 goto finish;
515 add_path(&wtdir, wtdir_len, dst_path);
516 if (symlinks) {
517 if (symlink(wtdir.buf, rdir.buf)) {
518 ret = error_errno("could not symlink '%s' to '%s'", wtdir.buf, rdir.buf);
519 goto finish;
521 } else {
522 struct stat st;
523 if (stat(wtdir.buf, &st))
524 st.st_mode = 0644;
525 if (copy_file(rdir.buf, wtdir.buf,
526 st.st_mode)) {
527 ret = error("could not copy '%s' to '%s'", wtdir.buf, rdir.buf);
528 goto finish;
535 fclose(fp);
536 fp = NULL;
537 if (finish_command(child)) {
538 ret = error("error occurred running diff --raw");
539 goto finish;
542 if (!i)
543 goto finish;
546 * Changes to submodules require special treatment.This loop writes a
547 * temporary file to both the left and right directories to show the
548 * change in the recorded SHA1 for the submodule.
550 hashmap_for_each_entry(&submodules, &iter, entry,
551 entry /* member name */) {
552 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
556 * Symbolic links require special treatment. The standard "git diff"
557 * shows only the link itself, not the contents of the link target.
558 * This loop replicates that behavior.
560 hashmap_for_each_entry(&symlinks2, &iter, entry,
561 entry /* member name */) {
563 write_standin_files(entry, &ldir, ldir_len, &rdir, rdir_len);
566 strbuf_setlen(&ldir, ldir_len);
567 strbuf_setlen(&rdir, rdir_len);
569 if (extcmd) {
570 strvec_push(&cmd.args, extcmd);
571 } else {
572 strvec_push(&cmd.args, "difftool--helper");
573 cmd.git_cmd = 1;
574 setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
576 strvec_pushl(&cmd.args, ldir.buf, rdir.buf, NULL);
577 ret = run_command(&cmd);
579 /* TODO: audit for interaction with sparse-index. */
580 ensure_full_index(&wtindex);
583 * If the diff includes working copy files and those
584 * files were modified during the diff, then the changes
585 * should be copied back to the working tree.
586 * Do not copy back files when symlinks are used and the
587 * external tool did not replace the original link with a file.
589 * These hashes are loaded lazily since they aren't needed
590 * in the common case of --symlinks and the difftool updating
591 * files through the symlink.
593 hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
594 hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
596 for (i = 0; i < wtindex.cache_nr; i++) {
597 struct hashmap_entry dummy;
598 const char *name = wtindex.cache[i]->name;
599 struct stat st;
601 add_path(&rdir, rdir_len, name);
602 if (lstat(rdir.buf, &st))
603 continue;
605 if ((symlinks && S_ISLNK(st.st_mode)) || !S_ISREG(st.st_mode))
606 continue;
608 if (!indices_loaded) {
609 struct lock_file lock = LOCK_INIT;
610 strbuf_reset(&buf);
611 strbuf_addf(&buf, "%s/wtindex", tmpdir.buf);
612 if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||
613 write_locked_index(&wtindex, &lock, COMMIT_LOCK)) {
614 ret = error("could not write %s", buf.buf);
615 goto finish;
617 changed_files(&wt_modified, buf.buf, workdir);
618 strbuf_setlen(&rdir, rdir_len);
619 changed_files(&tmp_modified, buf.buf, rdir.buf);
620 add_path(&rdir, rdir_len, name);
621 indices_loaded = 1;
624 hashmap_entry_init(&dummy, strhash(name));
625 if (hashmap_get(&tmp_modified, &dummy, name)) {
626 add_path(&wtdir, wtdir_len, name);
627 if (hashmap_get(&wt_modified, &dummy, name)) {
628 warning(_("both files modified: '%s' and '%s'."),
629 wtdir.buf, rdir.buf);
630 warning(_("working tree file has been left."));
631 warning("%s", "");
632 err = 1;
633 } else if (unlink(wtdir.buf) ||
634 copy_file(wtdir.buf, rdir.buf, st.st_mode))
635 warning_errno(_("could not copy '%s' to '%s'"),
636 rdir.buf, wtdir.buf);
640 if (err) {
641 warning(_("temporary files exist in '%s'."), tmpdir.buf);
642 warning(_("you may want to cleanup or recover these."));
643 ret = 1;
644 } else {
645 remove_dir_recursively(&tmpdir, 0);
646 if (ret)
647 warning(_("failed: %d"), ret);
650 finish:
651 if (fp)
652 fclose(fp);
654 free(lbase_dir);
655 free(rbase_dir);
656 strbuf_release(&ldir);
657 strbuf_release(&rdir);
658 strbuf_release(&wtdir);
659 strbuf_release(&buf);
660 strbuf_release(&tmpdir);
662 return (ret < 0) ? 1 : ret;
665 static int run_file_diff(int prompt, const char *prefix,
666 struct child_process *child)
668 const char *env[] = {
669 "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
670 NULL
673 if (prompt > 0)
674 env[2] = "GIT_DIFFTOOL_PROMPT=true";
675 else if (!prompt)
676 env[2] = "GIT_DIFFTOOL_NO_PROMPT=true";
678 child->git_cmd = 1;
679 child->dir = prefix;
680 strvec_pushv(&child->env, env);
682 return run_command(child);
685 int cmd_difftool(int argc, const char **argv, const char *prefix)
687 int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
688 tool_help = 0, no_index = 0;
689 static char *difftool_cmd = NULL, *extcmd = NULL;
690 struct option builtin_difftool_options[] = {
691 OPT_BOOL('g', "gui", &use_gui_tool,
692 N_("use `diff.guitool` instead of `diff.tool`")),
693 OPT_BOOL('d', "dir-diff", &dir_diff,
694 N_("perform a full-directory diff")),
695 OPT_SET_INT_F('y', "no-prompt", &prompt,
696 N_("do not prompt before launching a diff tool"),
697 0, PARSE_OPT_NONEG),
698 OPT_SET_INT_F(0, "prompt", &prompt, NULL,
699 1, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
700 OPT_BOOL(0, "symlinks", &symlinks,
701 N_("use symlinks in dir-diff mode")),
702 OPT_STRING('t', "tool", &difftool_cmd, N_("tool"),
703 N_("use the specified diff tool")),
704 OPT_BOOL(0, "tool-help", &tool_help,
705 N_("print a list of diff tools that may be used with "
706 "`--tool`")),
707 OPT_BOOL(0, "trust-exit-code", &trust_exit_code,
708 N_("make 'git-difftool' exit when an invoked diff "
709 "tool returns a non-zero exit code")),
710 OPT_STRING('x', "extcmd", &extcmd, N_("command"),
711 N_("specify a custom command for viewing diffs")),
712 OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
713 OPT_END()
715 struct child_process child = CHILD_PROCESS_INIT;
717 git_config(difftool_config, NULL);
718 symlinks = has_symlinks;
720 argc = parse_options(argc, argv, prefix, builtin_difftool_options,
721 builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN_OPT |
722 PARSE_OPT_KEEP_DASHDASH);
724 if (tool_help)
725 return print_tool_help();
727 if (!no_index && !startup_info->have_repository)
728 die(_("difftool requires worktree or --no-index"));
730 if (!no_index){
731 setup_work_tree();
732 setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
733 setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
734 } else if (dir_diff)
735 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
737 die_for_incompatible_opt3(use_gui_tool, "--gui",
738 !!difftool_cmd, "--tool",
739 !!extcmd, "--extcmd");
741 if (use_gui_tool)
742 setenv("GIT_MERGETOOL_GUI", "true", 1);
743 else if (difftool_cmd) {
744 if (*difftool_cmd)
745 setenv("GIT_DIFF_TOOL", difftool_cmd, 1);
746 else
747 die(_("no <tool> given for --tool=<tool>"));
750 if (extcmd) {
751 if (*extcmd)
752 setenv("GIT_DIFFTOOL_EXTCMD", extcmd, 1);
753 else
754 die(_("no <cmd> given for --extcmd=<cmd>"));
757 setenv("GIT_DIFFTOOL_TRUST_EXIT_CODE",
758 trust_exit_code ? "true" : "false", 1);
761 * In directory diff mode, 'git-difftool--helper' is called once
762 * to compare the a / b directories. In file diff mode, 'git diff'
763 * will invoke a separate instance of 'git-difftool--helper' for
764 * each file that changed.
766 strvec_push(&child.args, "diff");
767 if (no_index)
768 strvec_push(&child.args, "--no-index");
769 if (dir_diff)
770 strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
771 strvec_pushv(&child.args, argv);
773 if (dir_diff)
774 return run_dir_diff(extcmd, symlinks, prefix, &child);
775 return run_file_diff(prompt, prefix, &child);