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