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