Skip to contentSkip to conversation

difftool: teach difftool to handle directory diffs

PATCH Superseded Tim Henigan 4 participants 8 messages Jun 15, 2016 Documentation

v1 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

PATCH 7/9 difftool: teach difftool to handle directory diffs

Prior to this commit, the difftool utility could only open files one at a time. The new '--dir-diff' option copies all the modified files to a temporary location and runs a directory diff on them.

---

I'm not sure what conventions are used to decide when new copyright authors are added. Given the amount of change to this script, it seems appropriate to add me to the list. However, if that is change is outside norms, I have no problem taking it out.

 Documentation/git-difftool.txt |    6 ++
 git-difftool--helper.sh        |   20 ++++--
 git-difftool.perl              |  142 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 150 insertions(+), 18 deletions(-)
Documentation/git-difftool.txt +6 -0
index fe38f66..aba5e76 100644
@@ -19,6 +19,12 @@ linkgit:git-diff[1].
 
 OPTIONS
 -------
+-d::
+--dir-diff::
+	Copy the modified files to a temporary location and perform
+	a directory diff on them. This mode never prompts before
+	launching the diff tool.
+
 -y::
 --no-prompt::
 	Do not prompt before launching a diff tool.
git-difftool--helper.sh +14 -6
index e6558d1..bd0556f 100755
@@ -6,6 +6,7 @@
 # Copyright (c) 2009, 2010 David Aguilar
 
 TOOL_MODE=diff
+SUBDIRECTORY_OK=1
 . git-mergetool--lib
 
 # difftool.prompt controls the default prompt/no-prompt behavior
@@ -73,9 +74,16 @@ then
 	fi
 fi
 
-# Launch the merge tool on each path provided by 'git diff'
-while test $# -gt 6
-do
-	launch_merge_tool "$1" "$2" "$5"
-	shift 7
-done
+if test -n "$GIT_DIFFTOOL_DIRDIFF"
+then
+	LOCAL="$1"
+	REMOTE="$2"
+	run_merge_tool "$merge_tool" false
+else
+	# Launch the merge tool on each path provided by 'git diff'
+	while test $# -gt 6
+	do
+		launch_merge_tool "$1" "$2" "$5"
+		shift 7
+	done
+fi
git-difftool.perl +130 -12

176 lines not on this page · raw message

Signature · 1 line
1.7.9.1.290.gbd444
#1 lore raw
Quoted 26 lines · Tim Henigan

Prior to this commit, the difftool utility could only open files one at a time.  The new '--dir-diff' option copies all the modified files to a temporary location and runs a directory diff on them.

---

 Documentation/git-difftool.txt |    6 ++
 git-difftool--helper.sh        |   20 ++++--
 git-difftool.perl              |  142 ++++++++++++++++++++++++++++++++++++----

 3 files changed, 150 insertions(+), 18 deletions(-)
git-difftool.perl +0 -0
index 8498089..0262c7a 100755
@@ -22,15 +28,109 @@ sub usage
        print << 'USAGE';

 usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>] [-y|--no-prompt]   [-g|--gui]
+                    [-d|--dir-diff]
                    ['git diff' options]
 USAGE
       exit($exitcode);
 }

git-difftool.perl sub usage · line 45

Quoted 9 lines
git-difftool.perl +9 -0
+sub setup_dir_diff
+{
+       # Run the diff; exit immediately if no diff found
+       my $repo = Git->repository();
+       my $diffrtn = $repo->command_oneline(['diff', '--raw', '--no-abbrev', '-z', @ARGV]);
+       exit(0) if (length($diffrtn) == 0);
+
+       # Setup temp directories
+       my $tmpdir = tempdir('/tmp/git-diffall.XXXXX', CLEANUP => 1);

Is it okay to hardcode /tmp here, or should we instead do something like this?:

    my $tmp = $ENV{TMPDIR} || '/tmp';
Signature · 1 line
David
#2 lore raw
Quoted 8 lines · David Aguilar
Quoted 2 lines · Tim Henigan

+       # Setup temp directories
+       my $tmpdir = tempdir('/tmp/git-diffall.XXXXX', CLEANUP => 1);

Is it okay to hardcode /tmp here, or should we instead do something like this?:

   my $tmp = $ENV{TMPDIR} || '/tmp';

This could be done better. Based on the documentation [1], it appears that the proper way to do this is:

    my $tmpdir = tempdir('git-diffall.XXXXX', CLEANUP => 1, TMPDIR => 1);

Adding 'TMPDIR => 1' is the equivalent of commanding the tmp dir to be created in 'File::Spec->tmpdir' [2].

I will try this out and send a v2 patch.

[1]: http://perldoc.perl.org/File/Temp.html
[2]: http://perldoc.perl.org/File/Spec.html

#3 lore raw
In reply to Tim Henigan

git-difftool.perl sub usage · line 122

Quoted 10 lines · Tim Henigan
git-difftool.perl +10 -0
+	foreach my $path (keys %submodule) {
+		if (defined $submodule{$path}{left}) {
+			open(SUBMOD, ">$ldir/$path") or die $!;
+			print(SUBMOD "Subproject commit $submodule{$path}{left}");
+			close(SUBMOD);
+		}
+		if (defined $submodule{$path}{right}) {
+			open(SUBMOD, ">$rdir/$path") or die $!;
+			print(SUBMOD "Subproject commit $submodule{$path}{right}");
+			close(SUBMOD);

Could you please use modern Perl, and use lexical filehandles instead of globs, and 3-arg version of 'open', i.e.

+ open my $submod_fh, ">", "$ldir/$path" or die $!;
+ print $submod_fh "Subproject commit $submodule{$path}{left}";
+ close $submod_fh;

Quoted 13 lines
git-difftool.perl +13 -0
+if (defined($dirdiff)) {
+	my ($a, $b) = setup_dir_diff();
+	if (defined($extcmd)) {
+		system("$extcmd $a $b");
+	} else {
+		$ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
+		system("git difftool--helper $a $b");
+	}
+	# TODO: if the diff including working copy files and those
+	# files were modified during the diff, then the changes
+	# should be copied back to the working tree
+} else {
+	if (defined($prompt)) {

git-difftool.perl if (defined($gui)) { · line 193

Quoted 10 lines
git-difftool.perl +10 -0
+		$ENV{GIT_DIFFTOOL_PROMPT} = 'true';
+	}
+	elsif (defined($no_prompt)) {
+		$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
+	}
+
+	$ENV{GIT_PAGER} = '';
+	$ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
+	git_cmd_try { Git::command_noisy(('diff', @ARGV)) } 'exit code %d';
+}

Why you use 'system' (and not in list form which does not require escaping shell characters) instead of git_cmd_try for first "git difftool--helper" invocation?

Is $extcmd and resultof setup_dir_diff() to be treated as shell snippet, and used in string form of 'system' without escaping shell metacharacters (like ' ' in filename)?

Signature · 1 line
Jakub Narebski
#4 lore raw
Quoted 13 lines · Jakub Narebski
Quoted 10 lines · Tim Henigan

+     foreach my $path (keys %submodule) {
+             if (defined $submodule{$path}{left}) {
+                     open(SUBMOD, ">$ldir/$path") or die $!;
+                     print(SUBMOD "Subproject commit $submodule{$path}{left}");
+                     close(SUBMOD);
+             }
+             if (defined $submodule{$path}{right}) {
+                     open(SUBMOD, ">$rdir/$path") or die $!;
+                     print(SUBMOD "Subproject commit $submodule{$path}{right}");
+                     close(SUBMOD);

Quoted 6 lines

Could you please use modern Perl, and use lexical filehandles instead of globs, and 3-arg version of 'open', i.e.

 +                     open my $submod_fh, ">", "$ldir/$path" or die $!;
 +                     print $submod_fh "Subproject commit $submodule{$path}{left}";
 +                     close $submod_fh;

I will make this change in v2. Thank you for the review and the reminder.

Quoted 9 lines
Quoted 8 lines

+if (defined($dirdiff)) {
+     my ($a, $b) = setup_dir_diff();
+     if (defined($extcmd)) {
+             system("$extcmd $a $b");
+     } else {
+             $ENV{GIT_DIFFTOOL_DIRDIFF} = 'true';
+             system("git difftool--helper $a $b");
+     }

Quoted 3 lines

Why you use 'system' (and not in list form which does not require escaping shell characters) instead of git_cmd_try for first "git difftool--helper" invocation?

There is no good reason to use 'system' here. I will change this to 'Git::command_noisy' in v2.

Quoted 3 lines

Is $extcmd and resultof setup_dir_diff() to be treated as shell snippet, and used in string form of 'system' without escaping shell metacharacters (like ' ' in filename)?

In v2, I will change to the list form of 'system' for the call to $extcmd. I think I made some bad assumptions about the contents of $extcmd, $a and $b because of my local test setup. However, changing to list form should correct the oversight.

#5 lore raw
In reply to Tim Henigan

I have run into a problem with the way that the tmp directories are populated using 'git update-index' and 'git checkout-index'. I would appreciate any feedback that may help me understand what I am doing wrong.

This is in regard to the following section of code:

Quoted 7 lines · Tim Henigan
git-difftool.perl +6 -0
+sub setup_dir_diff
+{
+       # Run the diff; exit immediately if no diff found
+       my $repo = Git->repository();
+       my $diffrtn = $repo->command_oneline(['diff', '--raw', '--no-abbrev', '-z', @ARGV]);
+       exit(0) if (length($diffrtn) == 0);

<snip>

Quoted 22 lines

+       my @rawdiff = split('\0', $diffrtn);

git-difftool.perl +21 -0
+
+       for (my $i=0; $i<@rawdiff; $i+=2) {
+               my ($lmode, $rmode, $lsha1, $rsha1, $status) = split(' ', substr($rawdiff[$i], 1));
+               my $path = $rawdiff[$i + 1];
+
+               if (($lmode eq $submodule_mode) or ($rmode eq $submodule_mode)) {
+                       $submodule{$path}{left} = $lsha1;
+                       $submodule{$path}{right} = $rsha1;
+                       next;
+               }
+
+               if ($lmode ne $null_mode) {
+                       $lindex .= "$lmode $lsha1\t$path\0";
+               }
+
+               if ($rmode ne $null_mode) {
+                       if ($rsha1 ne $null_sha1) {
+                               $rindex .= "$rmode $rsha1\t$path\0";
+                       } else {
+                               push(@working_tree, $path);
+                       }

git-difftool.perl sub usage · line 90

Quoted 10 lines
git-difftool.perl +10 -0
+               }
+       }
+
+       # Populate the left and right directories based on each index file
+       my ($inpipe, $ctx);
+       $ENV{GIT_INDEX_FILE} = "$tmpdir/lindex";
+       ($inpipe, $ctx) = $repo->command_input_pipe(qw/update-index -z --index-info/);
+       print($inpipe $lindex);
+       $repo->command_close_pipe($inpipe, $ctx);
+       $repo->command_oneline(["checkout-index", "-a", "--prefix=$ldir/"]);

When run from the root directory of a Git repository, the 'difftool --dir-diff' command operates as expected:
- Temporary index files for each side of the diff are created using
'git update-index'.
- 'git checkout-index --all --prefix=$tmp' is used to populate the
tmp directory based on the index file.

However, when 'difftool --dir-diff' is run from a subdirectory of the Git repository, there are problems.
- The temporary index file generated by 'git update-index' appears
to be correct.
- 'git checkout-index --all --prefix=$tmp' command does not work
(output tmp dir is empty).

I have tried some combinations of setting $ENV{GIT_DIR} and $ENV{GIT_WORK_TREE}, but it has not fixed the problem. Is there anything obvious that I missed?

#6 lore raw
Quoted 6 lines · Tim Henigan

However, when 'difftool --dir-diff' is run from a subdirectory of the Git repository, there are problems.
- The temporary index file generated by 'git update-index' appears
to be correct.
- 'git checkout-index --all --prefix=$tmp' command does not work
(output tmp dir is empty).

Hrmph.

$ rm -fr /tmp/xyz
$ cd Documentation
$ git checkout-index --all --prefix=/tmp/xyz/
$ ls -F /tmp/xyz
Documentation/
$ rm -fr /tmp/xyz
$ GIT_DIR=$(pwd)/../.git GIT_WORK_TREE=$(pwd) \
  git checkout-index --all --prefix=/tmp/xyz/
$ ls -FC /tmp/xyz | head -2 
abspath.c                   git-merge-octopus.sh*       reflog-walk.c
aclocal.m4                  git-merge-one-file.sh*      reflog-walk.h

Be sure to have the trailing slash in $tmp.

#7 lore raw
Quoted 23 lines · Junio C Hamano
Quoted 6 lines · Tim Henigan

However, when 'difftool --dir-diff' is run from a subdirectory of the Git repository, there are problems.
  - The temporary index file generated by 'git update-index' appears to be correct.
  - 'git checkout-index --all --prefix=$tmp' command does not work (output tmp dir is empty).

Hrmph.

$ rm -fr /tmp/xyz
$ cd Documentation
$ git checkout-index --all --prefix=/tmp/xyz/
$ ls -F /tmp/xyz
Documentation/
$ rm -fr /tmp/xyz
$ GIT_DIR=$(pwd)/../.git GIT_WORK_TREE=$(pwd) \
 git checkout-index --all --prefix=/tmp/xyz/
$ ls -FC /tmp/xyz | head -2
abspath.c                   git-merge-octopus.sh*       reflog-walk.c
aclocal.m4                  git-merge-one-file.sh*      reflog-walk.h
Quoted 1 line

Be sure to have the trailing slash in $tmp.

It turns out that 'Git::command_oneline()' does not honor environment variables set prior to calling the function.

This change fixed the problem:

- $repo->command_oneline(["checkout-index", "-a", "--prefix=$ldir/"]);
+ $ENV{GIT_DIR} = $repo->repo_path();
+ system(('git', 'checkout-index', '-a', "--prefix=$ldir/"));

I'm still testing, but will submit an updated patch soon.