Skip to content

Commit 0e758ac

Browse files
committed
Merge branch 'js/difftool-builtin'
"git difftool --dir-diff" used to die a controlled death giving a "fatal" message when encountering a locally modified symbolic link, but it started segfaulting since v2.12. This has been fixed. * js/difftool-builtin: difftool: handle modified symlinks in dir-diff mode t7800: cleanup cruft left behind by tests t7800: remove whitespace before redirect
2 parents 9d77b04 + 18ec800 commit 0e758ac

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

builtin/difftool.c

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,49 @@ static int ensure_leading_directories(char *path)
254254
}
255255
}
256256

257+
/*
258+
* Unconditional writing of a plain regular file is what
259+
* "git difftool --dir-diff" wants to do for symlinks. We are preparing two
260+
* temporary directories to be fed to a Git-unaware tool that knows how to
261+
* show a diff of two directories (e.g. "diff -r A B").
262+
*
263+
* Because the tool is Git-unaware, if a symbolic link appears in either of
264+
* these temporary directories, it will try to dereference and show the
265+
* difference of the target of the symbolic link, which is not what we want,
266+
* as the goal of the dir-diff mode is to produce an output that is logically
267+
* equivalent to what "git diff" produces.
268+
*
269+
* Most importantly, we want to get textual comparison of the result of the
270+
* readlink(2). get_symlink() provides that---it returns the contents of
271+
* the symlink that gets written to a regular file to force the external tool
272+
* to compare the readlink(2) result as text, even on a filesystem that is
273+
* capable of doing a symbolic link.
274+
*/
275+
static char *get_symlink(const struct object_id *oid, const char *path)
276+
{
277+
char *data;
278+
if (is_null_oid(oid)) {
279+
/* The symlink is unknown to Git so read from the filesystem */
280+
struct strbuf link = STRBUF_INIT;
281+
if (has_symlinks) {
282+
if (strbuf_readlink(&link, path, strlen(path)))
283+
die(_("could not read symlink %s"), path);
284+
} else if (strbuf_read_file(&link, path, 128))
285+
die(_("could not read symlink file %s"), path);
286+
287+
data = strbuf_detach(&link, NULL);
288+
} else {
289+
enum object_type type;
290+
unsigned long size;
291+
data = read_sha1_file(oid->hash, &type, &size);
292+
if (!data)
293+
die(_("could not read object %s for symlink %s"),
294+
oid_to_hex(oid), path);
295+
}
296+
297+
return data;
298+
}
299+
257300
static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
258301
int argc, const char **argv)
259302
{
@@ -270,8 +313,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
270313
struct hashmap working_tree_dups, submodules, symlinks2;
271314
struct hashmap_iter iter;
272315
struct pair_entry *entry;
273-
enum object_type type;
274-
unsigned long size;
275316
struct index_state wtindex;
276317
struct checkout lstate, rstate;
277318
int rc, flags = RUN_GIT_CMD, err = 0;
@@ -377,13 +418,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
377418
}
378419

379420
if (S_ISLNK(lmode)) {
380-
char *content = read_sha1_file(loid.hash, &type, &size);
421+
char *content = get_symlink(&loid, src_path);
381422
add_left_or_right(&symlinks2, src_path, content, 0);
382423
free(content);
383424
}
384425

385426
if (S_ISLNK(rmode)) {
386-
char *content = read_sha1_file(roid.hash, &type, &size);
427+
char *content = get_symlink(&roid, dst_path);
387428
add_left_or_right(&symlinks2, dst_path, content, 1);
388429
free(content);
389430
}
@@ -397,7 +438,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
397438
return error("could not write '%s'", src_path);
398439
}
399440

400-
if (rmode) {
441+
if (rmode && !S_ISLNK(rmode)) {
401442
struct working_tree_entry *entry;
402443

403444
/* Avoid duplicate working_tree entries */

t/t7800-difftool.sh

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ run_dir_diff_test 'difftool --dir-diff branch from subdirectory' '
428428
git difftool --dir-diff $symlinks --extcmd ls branch >output &&
429429
# "sub" must only exist in "right"
430430
# "file" and "file2" must be listed in both "left" and "right"
431-
grep sub output > sub-output &&
431+
grep sub output >sub-output &&
432432
test_line_count = 1 sub-output &&
433433
grep file"$" output >file-output &&
434434
test_line_count = 2 file-output &&
@@ -591,6 +591,7 @@ test_expect_success 'difftool --no-symlinks detects conflict ' '
591591
'
592592

593593
test_expect_success 'difftool properly honors gitlink and core.worktree' '
594+
test_when_finished rm -rf submod/ule &&
594595
git submodule add ./. submod/ule &&
595596
test_config -C submod/ule diff.tool checktrees &&
596597
test_config -C submod/ule difftool.checktrees.cmd '\''
@@ -600,11 +601,13 @@ test_expect_success 'difftool properly honors gitlink and core.worktree' '
600601
cd submod/ule &&
601602
echo good >expect &&
602603
git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
603-
test_cmp expect actual
604+
test_cmp expect actual &&
605+
rm -f expect actual
604606
)
605607
'
606608

607609
test_expect_success SYMLINKS 'difftool --dir-diff symlinked directories' '
610+
test_when_finished git reset --hard &&
608611
git init dirlinks &&
609612
(
610613
cd dirlinks &&
@@ -623,4 +626,64 @@ test_expect_success SYMLINKS 'difftool --dir-diff symlinked directories' '
623626
)
624627
'
625628

629+
test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
630+
test_when_finished git reset --hard &&
631+
touch b &&
632+
ln -s b c &&
633+
git add b c &&
634+
test_tick &&
635+
git commit -m initial &&
636+
touch d &&
637+
rm c &&
638+
ln -s d c &&
639+
cat >expect <<-EOF &&
640+
b
641+
c
642+
643+
c
644+
EOF
645+
git difftool --symlinks --dir-diff --extcmd ls >output &&
646+
grep -v ^/ output >actual &&
647+
test_cmp expect actual &&
648+
649+
git difftool --no-symlinks --dir-diff --extcmd ls >output &&
650+
grep -v ^/ output >actual &&
651+
test_cmp expect actual &&
652+
653+
# The left side contains symlink "c" that points to "b"
654+
test_config difftool.cat.cmd "cat \$LOCAL/c" &&
655+
printf "%s\n" b >expect &&
656+
657+
git difftool --symlinks --dir-diff --tool cat >actual &&
658+
test_cmp expect actual &&
659+
660+
git difftool --symlinks --no-symlinks --dir-diff --tool cat >actual &&
661+
test_cmp expect actual &&
662+
663+
# The right side contains symlink "c" that points to "d"
664+
test_config difftool.cat.cmd "cat \$REMOTE/c" &&
665+
printf "%s\n" d >expect &&
666+
667+
git difftool --symlinks --dir-diff --tool cat >actual &&
668+
test_cmp expect actual &&
669+
670+
git difftool --no-symlinks --dir-diff --tool cat >actual &&
671+
test_cmp expect actual &&
672+
673+
# Deleted symlinks
674+
rm -f c &&
675+
cat >expect <<-EOF &&
676+
b
677+
c
678+
679+
EOF
680+
git difftool --symlinks --dir-diff --extcmd ls >output &&
681+
grep -v ^/ output >actual &&
682+
test_cmp expect actual &&
683+
684+
git difftool --no-symlinks --dir-diff --extcmd ls >output &&
685+
grep -v ^/ output >actual &&
686+
test_cmp expect actual
687+
'
688+
626689
test_done

0 commit comments

Comments
 (0)