threads / patch / 35941

patchdifftool: support repositories with .git-files

Subject: [PATCH] difftool: support repositories with .git-files

## tl;dr

8 messages between Feb 24, 2014 and Mar 5, 2014. Diffs are folded; open one to read it.

replies: 7people: 3as markdown or json

David Aguilar· Feb 24, 2014, 03:12 UTC · lore

Modern versions of "git submodule" use .git-files to setup the submodule directory. When run in a "git submodule"-created repository "git difftool --dir-diff" dies with the following error:

	$ git difftool -d HEAD~
	fatal: This operation must be run in a work tree
	diff --raw --no-abbrev -z HEAD~: command returned error: 128

core.worktree is relative to the .git directory but the logic in find_worktree() does not account for it.

Use `git rev-parse --show-toplevel` to find the worktree so that the dir-diff feature works inside a submodule.

Reported-by: Gábor Lipták <gabor.liptak@gmail.com>
Helped-by: Jens Lehmann <jens.lehmann@web.de>
Helped-by: John Keeping <john@keeping.me.uk>
Signed-off-by: David Aguilar <davvid@gmail.com>
---
 git-difftool.perl | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
Show changes to git-difftool.perl +2 −16
diff --git a/git-difftool.perl b/git-difftool.perl
index e57d3d1..18ca61e 100755
--- a/git-difftool.perl
+++ b/git-difftool.perl
@@ -39,24 +39,10 @@ USAGE
 
 sub find_worktree
 {
-	my ($repo) = @_;
-
 	# Git->repository->wc_path() does not honor changes to the working
 	# tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
 	# config variable.
-	my $worktree;
-	my $env_worktree = $ENV{GIT_WORK_TREE};
-	my $core_worktree = Git::config('core.worktree');
-
-	if (defined($env_worktree) and (length($env_worktree) > 0)) {
-		$worktree = $env_worktree;
-	} elsif (defined($core_worktree) and (length($core_worktree) > 0)) {
-		$worktree = $core_worktree;
-	} else {
-		$worktree = $repo->wc_path();
-	}
-
-	return $worktree;
+	return Git::command_oneline('rev-parse', '--show-toplevel');
 }
 
 sub print_tool_help
@@ -418,7 +404,7 @@ sub dir_diff
 	my $rc;
 	my $error = 0;
 	my $repo = Git->repository();
-	my $workdir = find_worktree($repo);
+	my $workdir = find_worktree();
 	my ($a, $b, $tmpdir, @worktree) =
 		setup_dir_diff($repo, $workdir, $symlinks);
 
-- 
1.9.0.1.gd20a678
Junio C Hamano· Feb 24, 2014, 17:55 UTC · re: David Aguilar · lore

Re: [PATCH] difftool: support repositories with .git-files

David Aguilar <davvid@gmail.com> writes:
Show 20 quoted lines
> Modern versions of "git submodule" use .git-files to setup the
> submodule directory.  When run in a "git submodule"-created
> repository "git difftool --dir-diff" dies with the following
> error:
>
> 	$ git difftool -d HEAD~
> 	fatal: This operation must be run in a work tree
> 	diff --raw --no-abbrev -z HEAD~: command returned error: 128
>
> core.worktree is relative to the .git directory but the logic
> in find_worktree() does not account for it.
>
> Use `git rev-parse --show-toplevel` to find the worktree so that
> the dir-diff feature works inside a submodule.
>
> Reported-by: Gábor Lipták <gabor.liptak@gmail.com>
> Helped-by: Jens Lehmann <jens.lehmann@web.de>
> Helped-by: John Keeping <john@keeping.me.uk>
> Signed-off-by: David Aguilar <davvid@gmail.com>
> ---
Looks good; thanks.
Show 41 quoted lines
>  git-difftool.perl | 18 ++----------------
>  1 file changed, 2 insertions(+), 16 deletions(-)
>
> diff --git a/git-difftool.perl b/git-difftool.perl
> index e57d3d1..18ca61e 100755
> --- a/git-difftool.perl
> +++ b/git-difftool.perl
> @@ -39,24 +39,10 @@ USAGE
>  
>  sub find_worktree
>  {
> -	my ($repo) = @_;
> -
>  	# Git->repository->wc_path() does not honor changes to the working
>  	# tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree'
>  	# config variable.
> -	my $worktree;
> -	my $env_worktree = $ENV{GIT_WORK_TREE};
> -	my $core_worktree = Git::config('core.worktree');
> -
> -	if (defined($env_worktree) and (length($env_worktree) > 0)) {
> -		$worktree = $env_worktree;
> -	} elsif (defined($core_worktree) and (length($core_worktree) > 0)) {
> -		$worktree = $core_worktree;
> -	} else {
> -		$worktree = $repo->wc_path();
> -	}
> -
> -	return $worktree;
> +	return Git::command_oneline('rev-parse', '--show-toplevel');
>  }
>  
>  sub print_tool_help
> @@ -418,7 +404,7 @@ sub dir_diff
>  	my $rc;
>  	my $error = 0;
>  	my $repo = Git->repository();
> -	my $workdir = find_worktree($repo);
> +	my $workdir = find_worktree();
>  	my ($a, $b, $tmpdir, @worktree) =
>  		setup_dir_diff($repo, $workdir, $symlinks);
Jens Lehmann· Feb 24, 2014, 21:10 UTC · re: Junio C Hamano · lore

Re: [PATCH] difftool: support repositories with .git-files

Am 24.02.2014 17:55, schrieb Junio C Hamano:
Show 24 quoted lines
> David Aguilar <davvid@gmail.com> writes:
> 
>> Modern versions of "git submodule" use .git-files to setup the
>> submodule directory.  When run in a "git submodule"-created
>> repository "git difftool --dir-diff" dies with the following
>> error:
>>
>> 	$ git difftool -d HEAD~
>> 	fatal: This operation must be run in a work tree
>> 	diff --raw --no-abbrev -z HEAD~: command returned error: 128
>>
>> core.worktree is relative to the .git directory but the logic
>> in find_worktree() does not account for it.
>>
>> Use `git rev-parse --show-toplevel` to find the worktree so that
>> the dir-diff feature works inside a submodule.
>>
>> Reported-by: Gábor Lipták <gabor.liptak@gmail.com>
>> Helped-by: Jens Lehmann <jens.lehmann@web.de>
>> Helped-by: John Keeping <john@keeping.me.uk>
>> Signed-off-by: David Aguilar <davvid@gmail.com>
>> ---
> 
> Looks good; thanks.
FWIW:
Tested-by: Jens Lehmann <jens.lehmann@web.de>
What about squashing this in to detect any future regressions?
Show changes to t/t7800-difftool.sh +8 −0
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index 2418528..d86ad68 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -434,4 +434,12 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
 	)
 '

+test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
+	git submodule add ./. submod/ule &&
+	(
+		cd submod/ule &&
+		git difftool --tool=echo  --dir-diff --cached
+	)
+'
+
 test_done
Junio C Hamano· Feb 25, 2014, 18:02 UTC · re: Jens Lehmann · lore

Re: [PATCH] difftool: support repositories with .git-files

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 45 quoted lines
> Am 24.02.2014 17:55, schrieb Junio C Hamano:
>> David Aguilar <davvid@gmail.com> writes:
>> 
>>> Modern versions of "git submodule" use .git-files to setup the
>>> submodule directory.  When run in a "git submodule"-created
>>> repository "git difftool --dir-diff" dies with the following
>>> error:
>>>
>>> 	$ git difftool -d HEAD~
>>> 	fatal: This operation must be run in a work tree
>>> 	diff --raw --no-abbrev -z HEAD~: command returned error: 128
>>>
>>> core.worktree is relative to the .git directory but the logic
>>> in find_worktree() does not account for it.
>>>
>>> Use `git rev-parse --show-toplevel` to find the worktree so that
>>> the dir-diff feature works inside a submodule.
>>>
>>> Reported-by: Gábor Lipták <gabor.liptak@gmail.com>
>>> Helped-by: Jens Lehmann <jens.lehmann@web.de>
>>> Helped-by: John Keeping <john@keeping.me.uk>
>>> Signed-off-by: David Aguilar <davvid@gmail.com>
>>> ---
>> 
>> Looks good; thanks.
>
>
> FWIW:
> Tested-by: Jens Lehmann <jens.lehmann@web.de>
>
> What about squashing this in to detect any future regressions?
>
> diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
> index 2418528..d86ad68 100755
> --- a/t/t7800-difftool.sh
> +++ b/t/t7800-difftool.sh
> @@ -434,4 +434,12 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
>  	)
>  '
>
> +test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
> +	git submodule add ./. submod/ule &&
> +	(
> +		cd submod/ule &&
> +		git difftool --tool=echo  --dir-diff --cached

In the context of this fix, finishing with 0 exit status may be all we care about, but do we also care about things like in what directory the tool is invoked in, what arguments and extra environment settings (if any) it is given, and stuff like that?

In fact, the "echo" in the above is very misleading. The test relies on the fact that immediately after the submod/ule is cloned, "diff --cached" does not have to call any tool backend---if you modify some tracked file in its working tree and dropped --cached on the command line, the command will fail with "Huh? I do not know what 'echo' diff/merge backend is", no?

> +	)
> +'
> +
>  test_done
Jens Lehmann· Feb 25, 2014, 20:34 UTC · re: Junio C Hamano · lore

Re: [PATCH] difftool: support repositories with .git-files

Am 25.02.2014 18:02, schrieb Junio C Hamano:
Show 52 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>> Am 24.02.2014 17:55, schrieb Junio C Hamano:
>>> David Aguilar <davvid@gmail.com> writes:
>>>
>>>> Modern versions of "git submodule" use .git-files to setup the
>>>> submodule directory.  When run in a "git submodule"-created
>>>> repository "git difftool --dir-diff" dies with the following
>>>> error:
>>>>
>>>> 	$ git difftool -d HEAD~
>>>> 	fatal: This operation must be run in a work tree
>>>> 	diff --raw --no-abbrev -z HEAD~: command returned error: 128
>>>>
>>>> core.worktree is relative to the .git directory but the logic
>>>> in find_worktree() does not account for it.
>>>>
>>>> Use `git rev-parse --show-toplevel` to find the worktree so that
>>>> the dir-diff feature works inside a submodule.
>>>>
>>>> Reported-by: Gábor Lipták <gabor.liptak@gmail.com>
>>>> Helped-by: Jens Lehmann <jens.lehmann@web.de>
>>>> Helped-by: John Keeping <john@keeping.me.uk>
>>>> Signed-off-by: David Aguilar <davvid@gmail.com>
>>>> ---
>>>
>>> Looks good; thanks.
>>
>>
>> FWIW:
>> Tested-by: Jens Lehmann <jens.lehmann@web.de>
>>
>> What about squashing this in to detect any future regressions?
>>
>> diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
>> index 2418528..d86ad68 100755
>> --- a/t/t7800-difftool.sh
>> +++ b/t/t7800-difftool.sh
>> @@ -434,4 +434,12 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
>>  	)
>>  '
>>
>> +test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
>> +	git submodule add ./. submod/ule &&
>> +	(
>> +		cd submod/ule &&
>> +		git difftool --tool=echo  --dir-diff --cached
> 
> In the context of this fix, finishing with 0 exit status may be all
> we care about, but do we also care about things like in what
> directory the tool is invoked in, what arguments and extra
> environment settings (if any) it is given, and stuff like that?

Sure. But I just intended to test the fix (and the test can easily be extended by people who know more about difftool than I do).

Show 6 quoted lines
> In fact, the "echo" in the above is very misleading.  The test
> relies on the fact that immediately after the submod/ule is cloned,
> "diff --cached" does not have to call any tool backend---if you
> modify some tracked file in its working tree and dropped --cached
> on the command line, the command will fail with "Huh?  I do not know
> what 'echo' diff/merge backend is", no?

Right, using echo was not the best choice here. I used it to avoid the dependency to meld in the example of the OP (maybe using "true" as tool would have indicated that the tool is not important here, but looking into this again a simple "git difftool --dir-diff" without any further arguments also shows that the fix is working).

Aas mentioned above, I'm not familiar with difftool and just wanted to share an easy way to test the fix. But I do not care too deeply about this test, so feel free to ignore it.

Junio C Hamano· Feb 25, 2014, 22:12 UTC · re: Jens Lehmann · lore

Re: [PATCH] difftool: support repositories with .git-files

Jens Lehmann <Jens.Lehmann@web.de> writes:
Show 13 quoted lines
>>> +test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
>>> +	git submodule add ./. submod/ule &&
>>> +	(
>>> +		cd submod/ule &&
>>> +		git difftool --tool=echo  --dir-diff --cached
>> 
>> In the context of this fix, finishing with 0 exit status may be all
>> we care about, but do we also care about things like in what
>> directory the tool is invoked in, what arguments and extra
>> environment settings (if any) it is given, and stuff like that?
>
> Sure. But I just intended to test the fix (and the test can easily
> be extended by people who know more about difftool than I do).

Yes, we need to start somewhere and I'd agree that it was a good starting point.

> Right, using echo was not the best choice here. I used it to avoid
> the dependency to meld...

Perhaps like this then? This is an "a monkey sees what difftool_test_setup does and then mimics" patch ;-).

 t/t7800-difftool.sh | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Show changes to t/t7800-difftool.sh +13 −0
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index 2418528..595f808 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -434,4 +434,17 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
 	)
 '
 
+test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
+	git submodule add ./. submod/ule &&
+	(
+		cd submod/ule &&
+		git config diff.tool checktrees &&
+		git config difftool.checktrees.cmd '\''
+			test -d "$LOCAL" && test -d "$REMOTE"
+		'\'' &&
+		echo further >>file &&
+		git difftool --tool=checktrees --dir-diff
+	)
+'
+
 test_done
Jens Lehmann· Feb 27, 2014, 21:12 UTC · re: Junio C Hamano · lore

Re: [PATCH] difftool: support repositories with .git-files

Am 25.02.2014 22:12, schrieb Junio C Hamano:
Show 24 quoted lines
> Jens Lehmann <Jens.Lehmann@web.de> writes:
> 
>>>> +test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
>>>> +	git submodule add ./. submod/ule &&
>>>> +	(
>>>> +		cd submod/ule &&
>>>> +		git difftool --tool=echo  --dir-diff --cached
>>>
>>> In the context of this fix, finishing with 0 exit status may be all
>>> we care about, but do we also care about things like in what
>>> directory the tool is invoked in, what arguments and extra
>>> environment settings (if any) it is given, and stuff like that?
>>
>> Sure. But I just intended to test the fix (and the test can easily
>> be extended by people who know more about difftool than I do).
> 
> Yes, we need to start somewhere and I'd agree that it was a good
> starting point.
> 
>> Right, using echo was not the best choice here. I used it to avoid
>> the dependency to meld...
> 
> Perhaps like this then?  This is an "a monkey sees what
> difftool_test_setup does and then mimics" patch ;-).
Nicely done :-)
Show 30 quoted lines
>  t/t7800-difftool.sh | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
> index 2418528..595f808 100755
> --- a/t/t7800-difftool.sh
> +++ b/t/t7800-difftool.sh
> @@ -434,4 +434,17 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
>  	)
>  '
>  
> +test_expect_success PERL 'difftool properly honours gitlink and core.worktree' '
> +	git submodule add ./. submod/ule &&
> +	(
> +		cd submod/ule &&
> +		git config diff.tool checktrees &&
> +		git config difftool.checktrees.cmd '\''
> +			test -d "$LOCAL" && test -d "$REMOTE"
> +		'\'' &&
> +		echo further >>file &&
> +		git difftool --tool=checktrees --dir-diff
> +	)
> +'
> +
>  test_done
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
David Aguilar· Mar 5, 2014, 09:23 UTC · re: Jens Lehmann · lore

[PATCH] t7800: add a difftool test for .git-files

From: Junio C Hamano <gitster@pobox.com>
Signed-off-by: David Aguilar <davvid@gmail.com>
---
This is a replacement patch for the current tip of da/difftool.
 t/t7800-difftool.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Show changes to t/t7800-difftool.sh +14 −0
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index 2418528..986b78e 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -434,4 +434,18 @@ test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
 	)
 '
 
+test_expect_success PERL 'difftool properly honors gitlink and core.worktree' '
+	git submodule add ./. submod/ule &&
+	(
+		cd submod/ule &&
+		test_config diff.tool checktrees &&
+		test_config difftool.checktrees.cmd '\''
+			test -d "$LOCAL" && test -d "$REMOTE" && echo good
+		'\'' &&
+		echo good>expect &&
+		git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
1.8.5.5.2.g42fdfc9

← back to recent threads