Re: [PATCH] diff: don't attempt to strip prefix from absolute Windows paths

2018-10-18 Thread Junio C Hamano
Johannes Sixt  writes:

> Use is_absolute_path() to detect Windows style absolute paths.

When cd676a51 ("diff --relative: output paths as relative to the
current subdirectory", 2008-02-12) was done, neither "is_dir_sep()"
nor "has_dos_drive_prefix()" existed---the latter had to wait until
25fe217b ("Windows: Treat Windows style path names.", 2008-03-05),
but we didn't notice that the above code needs to use the Windows
aware is_absolute_path() when we applied the change.

> One might wonder whether the check for a directory separator that
> is visible in the patch context should be changed from == '/' to
> is_dir_sep() or not. It turns out not to be necessary. That code
> only ever investigates paths that have undergone pathspec
> normalization, after which there are only forward slashes even on
> Windows.

Thanks for carefully explaining.

>  static void strip_prefix(int prefix_length, const char **namep, const char 
> **otherp)
>  {
>   /* Strip the prefix but do not molest /dev/null and absolute paths */
> - if (*namep && **namep != '/') {
> + if (*namep && !is_absolute_path(*namep)) {
>   *namep += prefix_length;
>   if (**namep == '/')
>   ++*namep;
>   }
> - if (*otherp && **otherp != '/') {
> + if (*otherp && !is_absolute_path(*otherp)) {
>   *otherp += prefix_length;
>   if (**otherp == '/')
>   ++*otherp;

When I read the initial report and guessed the root cause without
looking at the code, I didn't expect the problematic area to be this
isolated and the solution to be this simple.

Nicely done.


Re: [PATCH] diff: don't attempt to strip prefix from absolute Windows paths

2018-10-18 Thread Johannes Sixt
Am 18.10.18 um 20:49 schrieb Stefan Beller:
> On Thu, Oct 18, 2018 at 11:38 AM Johannes Sixt  wrote:
> 
>> There is one peculiarity, though: [...]
> 
> The explanation makes sense, and the code looks good.
> Do we want to have a test for this niche case?
> 

Good point. That would be the following. But give me a day or two to
cross-check on Windows and whether it really catches the breakage.

diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 453e6c35eb..6e0dd6f9e5 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -127,4 +127,14 @@ test_expect_success 'diff --no-index from repo subdir 
respects config (implicit)
test_cmp expect actual.head
 '
 
+test_expect_success 'diff --no-index from repo subdir with absolute paths' '
+   cat <<-EOF >expect &&
+   1   1   $(pwd)/non/git/{a => b}
+   EOF
+   test_expect_code 1 \
+   git -C repo/sub diff --numstat \
+   "$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
+   test_cmp expect actual
+'
+
 test_done


Re: [PATCH] diff: don't attempt to strip prefix from absolute Windows paths

2018-10-18 Thread Stefan Beller
On Thu, Oct 18, 2018 at 11:38 AM Johannes Sixt  wrote:

> There is one peculiarity, though: [...]

The explanation makes sense, and the code looks good.
Do we want to have a test for this niche case?