Skip to contentSkip to conversation

difftool: fix dir-diff when file does not exist in working tree

PATCH In review John Keeping 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 difftool: fix dir-diff when file does not exist in working tree

Commit 02c5631 (difftool --dir-diff: symlink all files matching the working tree, 2013-03-14) does not handle the case where a file that is being compared does not exist in the working tree. Fix this by checking for existence explicitly before running git-hash-object.

---
This fixes a regression in 1.8.3-rc0.

 git-difftool.perl   | 9 ++++++++-
 t/t7800-difftool.sh | 7 +++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
git-difftool.perl +8 -1
index 6780292..0a1cb0a 100755
@@ -92,7 +92,14 @@ sub use_wt_file
 		return 0;
 	}
 
-	my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	my $wt_sha1;
+	if (-e "$workdir/$file") {
+		$wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	} else {
+		# If the file doesn't exist in the working tree, use something
+		# that cannot match a SHA-1.
+		$wt_sha1 = '';
+	};
 	my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
 	return ($use, $wt_sha1);
 }
t/t7800-difftool.sh +7 -0
index a6bd99e..d46f041 100755
@@ -356,6 +356,13 @@ run_dir_diff_test 'difftool --dir-diff from subdirectory' '
 	)
 '
 
+run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
+	test_when_finished git reset --hard &&
+	rm file2 &&
+	git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
+	grep file2 output
+'
+
 write_script .git/CHECK_SYMLINKS <<\EOF
 for f in file file2 sub/sub
 do
Signature · 1 line
1.8.3.rc2.285.gfc18c2c
#1 lore raw
Quoted 20 lines · John Keeping

Commit 02c5631 (difftool --dir-diff: symlink all files matching the working tree, 2013-03-14) does not handle the case where a file that is being compared does not exist in the working tree. Fix this by checking for existence explicitly before running git-hash-object.

---
This fixes a regression in 1.8.3-rc0.

 git-difftool.perl   | 9 ++++++++-
 t/t7800-difftool.sh | 7 +++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
git-difftool.perl +0 -0
index 6780292..0a1cb0a 100755
@@ -92,7 +92,14 @@ sub use_wt_file
 		return 0;

git-difftool.perl sub use_wt_file · line 101

Quoted 10 lines
git-difftool.perl +7 -1
 	}
 
-	my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	my $wt_sha1;
+	if (-e "$workdir/$file") {
+		$wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	} else {
+		# If the file doesn't exist in the working tree, use something
+		# that cannot match a SHA-1.
+		$wt_sha1 = '';

Yuck.

"that cannot match" might be a good justification to say "this does not break the next line to set $use and forces it to false", but "when we return false in $use, the value of $wt_sha1 is not used" needs to be said to convince why this is a safe change.

But if $sha1 is $null_sha1, we do end up setting $use to true and the caller would stuff the empty $wt_sha1 to form:

	$wtindex .= "$rmode \$dst_path\0";

Is that what we want to do here, or is it a "will never happen" condition? If the latter, the reason need to be described in this comment (and in the log).

Thanks.

Quoted 22 lines
git-difftool.perl +1 -0
+	};
 	my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
 	return ($use, $wt_sha1);
 }
t/t7800-difftool.sh +7 -0
index a6bd99e..d46f041 100755
@@ -356,6 +356,13 @@ run_dir_diff_test 'difftool --dir-diff from subdirectory' '
 	)
 '
 
+run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
+	test_when_finished git reset --hard &&
+	rm file2 &&
+	git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
+	grep file2 output
+'
+
 write_script .git/CHECK_SYMLINKS <<\EOF
 for f in file file2 sub/sub
 do
#2 lore raw

PATCH v2 1/1 difftool: fix dir-diff when file does not exist in working tree

Commit 02c5631 (difftool --dir-diff: symlink all files matching the working tree, 2013-03-14) does not handle the case where a file that is being compared does not exist in the working tree. Fix this by checking for existence explicitly before running git-hash-object.

---

Quoted 40 lines · Junio C Hamano
Quoted 20 lines · John Keeping

Commit 02c5631 (difftool --dir-diff: symlink all files matching the working tree, 2013-03-14) does not handle the case where a file that is being compared does not exist in the working tree. Fix this by checking for existence explicitly before running git-hash-object.

---
This fixes a regression in 1.8.3-rc0.

 git-difftool.perl   | 9 ++++++++-
 t/t7800-difftool.sh | 7 +++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
git-difftool.perl +0 -0
index 6780292..0a1cb0a 100755
@@ -92,7 +92,14 @@ sub use_wt_file
 		return 0;

git-difftool.perl sub use_wt_file · line 101

Quoted 10 lines
git-difftool.perl +7 -1
 	}
 
-	my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	my $wt_sha1;
+	if (-e "$workdir/$file") {
+		$wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
+	} else {
+		# If the file doesn't exist in the working tree, use something
+		# that cannot match a SHA-1.
+		$wt_sha1 = '';

Yuck.

"that cannot match" might be a good justification to say "this does not break the next line to set $use and forces it to false", but "when we return false in $use, the value of $wt_sha1 is not used" needs to be said to convince why this is a safe change.

Quoted 8 lines

But if $sha1 is $null_sha1, we do end up setting $use to true and the caller would stuff the empty $wt_sha1 to form:

	$wtindex .= "$rmode \$dst_path\0";

Is that what we want to do here, or is it a "will never happen" condition? If the latter, the reason need to be described in this comment (and in the log).

It can't ever happen because we only call this if the mode is non-zero in which case the SHA-1 is only null if there are unstaged changes.

However, I think this revised version is much clearer.

 git-difftool.perl   | 6 ++++++
 t/t7800-difftool.sh | 7 +++++++
 2 files changed, 13 insertions(+)
git-difftool.perl +6 -0
index 6780292..8a75205 100755
@@ -92,6 +92,12 @@ sub use_wt_file
 		return 0;
 	}
 
+	if (! -e "$workdir/$file") {
+		# If the file doesn't exist in the working tree, we cannot
+		# use it.
+		return (0, $null_sha1);
+	}
+
 	my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
 	my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
 	return ($use, $wt_sha1);
t/t7800-difftool.sh +7 -0
index a6bd99e..d46f041 100755
@@ -356,6 +356,13 @@ run_dir_diff_test 'difftool --dir-diff from subdirectory' '
 	)
 '
 
+run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
+	test_when_finished git reset --hard &&
+	rm file2 &&
+	git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
+	grep file2 output
+'
+
 write_script .git/CHECK_SYMLINKS <<\EOF
 for f in file file2 sub/sub
 do
Signature · 1 line
1.8.3.rc2.285.gfc18c2c
#3 lore raw
Quoted 4 lines · John Keeping

It can't ever happen because we only call this if the mode is non-zero in which case the SHA-1 is only null if there are unstaged changes.

However, I think this revised version is much clearer.

Yes, that makes the intention crystal clear. I like it.

Quoted 40 lines
 git-difftool.perl   | 6 ++++++
 t/t7800-difftool.sh | 7 +++++++
 2 files changed, 13 insertions(+)
git-difftool.perl +6 -0
index 6780292..8a75205 100755
@@ -92,6 +92,12 @@ sub use_wt_file
 		return 0;
 	}
 
+	if (! -e "$workdir/$file") {
+		# If the file doesn't exist in the working tree, we cannot
+		# use it.
+		return (0, $null_sha1);
+	}
+
 	my $wt_sha1 = $repo->command_oneline('hash-object', "$workdir/$file");
 	my $use = ($sha1 eq $null_sha1) || ($sha1 eq $wt_sha1);
 	return ($use, $wt_sha1);
t/t7800-difftool.sh +7 -0
index a6bd99e..d46f041 100755
@@ -356,6 +356,13 @@ run_dir_diff_test 'difftool --dir-diff from subdirectory' '
 	)
 '
 
+run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
+	test_when_finished git reset --hard &&
+	rm file2 &&
+	git difftool --dir-diff $symlinks --extcmd ls branch master >output &&
+	grep file2 output
+'
+
 write_script .git/CHECK_SYMLINKS <<\EOF
 for f in file file2 sub/sub
 do