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