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