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