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