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