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