difftool: Check all return codes from compare()
[gitweb.git] / git-difftool.perl
index 41ba9323f8356cf7e9b3df97d24e76d0da89e7e6..92f4907bbc1ba4886a5cfcf59b760457a9191a1f 100755 (executable)
 use 5.008;
 use strict;
 use warnings;
-use File::Basename qw(basename dirname);
+use File::Basename qw(dirname);
 use File::Copy;
 use File::Compare;
+use File::Find;
 use File::stat;
 use File::Path qw(mkpath);
 use File::Temp qw(tempdir);
@@ -58,13 +59,27 @@ sub find_worktree
        return $worktree;
 }
 
+sub filter_tool_scripts
+{
+       my ($tools) = @_;
+       if (-d $_) {
+               if ($_ ne ".") {
+                       # Ignore files in subdirectories
+                       $File::Find::prune = 1;
+               }
+       } else {
+               if ((-f $_) && ($_ ne "defaults")) {
+                       push(@$tools, $_);
+               }
+       }
+}
+
 sub print_tool_help
 {
-       my ($cmd, @found, @notfound);
+       my ($cmd, @found, @notfound, @tools);
        my $gitpath = Git::exec_path();
 
-       my @files = map { basename($_) } glob("$gitpath/mergetools/*");
-       my @tools = sort(grep { !m{^defaults$} } @files);
+       find(sub { filter_tool_scripts(\@tools) }, "$gitpath/mergetools");
 
        foreach my $tool (@tools) {
                $cmd  = "TOOL_MODE=diff";
@@ -79,10 +94,10 @@ sub print_tool_help
        }
 
        print "'git difftool --tool=<tool>' may be set to one of the following:\n";
-       print "\t$_\n" for (@found);
+       print "\t$_\n" for (sort(@found));
 
        print "\nThe following tools are valid, but not currently available:\n";
-       print "\t$_\n" for (@notfound);
+       print "\t$_\n" for (sort(@notfound));
 
        print "\nNOTE: Some of the tools listed above only work in a windowed\n";
        print "environment. If run in a terminal-only session, they will fail.\n";
@@ -92,7 +107,7 @@ sub print_tool_help
 
 sub setup_dir_diff
 {
-       my ($repo, $workdir) = @_;
+       my ($repo, $workdir, $symlinks) = @_;
 
        # Run the diff; exit immediately if no diff found
        # 'Repository' and 'WorkingCopy' must be explicitly set to insure that
@@ -104,7 +119,7 @@ sub setup_dir_diff
        exit(0) if (length($diffrtn) == 0);
 
        # Setup temp directories
-       my $tmpdir = tempdir('git-diffall.XXXXX', CLEANUP => 1, TMPDIR => 1);
+       my $tmpdir = tempdir('git-difftool.XXXXX', CLEANUP => 1, TMPDIR => 1);
        my $ldir = "$tmpdir/left";
        my $rdir = "$tmpdir/right";
        mkpath($ldir) or die $!;
@@ -209,8 +224,13 @@ sub setup_dir_diff
                unless (-d "$rdir/$dir") {
                        mkpath("$rdir/$dir") or die $!;
                }
-               copy("$workdir/$file", "$rdir/$file") or die $!;
-               chmod(stat("$workdir/$file")->mode, "$rdir/$file") or die $!;
+               if ($symlinks) {
+                       symlink("$workdir/$file", "$rdir/$file") or die $!;
+               } else {
+                       copy("$workdir/$file", "$rdir/$file") or die $!;
+                       my $mode = stat("$workdir/$file")->mode;
+                       chmod($mode, "$rdir/$file") or die $!;
+               }
        }
 
        # Changes to submodules require special treatment. This loop writes a
@@ -264,41 +284,51 @@ sub main
 {
        # parse command-line options. all unrecognized options and arguments
        # are passed through to the 'git diff' command.
-       my ($difftool_cmd, $dirdiff, $extcmd, $gui, $help, $prompt, $tool_help);
-       GetOptions('g|gui!' => \$gui,
-               'd|dir-diff' => \$dirdiff,
-               'h' => \$help,
-               'prompt!' => \$prompt,
-               'y' => sub { $prompt = 0; },
-               't|tool:s' => \$difftool_cmd,
-               'tool-help' => \$tool_help,
-               'x|extcmd:s' => \$extcmd);
-
-       if (defined($help)) {
+       my %opts = (
+               difftool_cmd => undef,
+               dirdiff => undef,
+               extcmd => undef,
+               gui => undef,
+               help => undef,
+               prompt => undef,
+               symlinks => $^O ne 'MSWin32' && $^O ne 'msys',
+               tool_help => undef,
+       );
+       GetOptions('g|gui!' => \$opts{gui},
+               'd|dir-diff' => \$opts{dirdiff},
+               'h' => \$opts{help},
+               'prompt!' => \$opts{prompt},
+               'y' => sub { $opts{prompt} = 0; },
+               'symlinks' => \$opts{symlinks},
+               'no-symlinks' => sub { $opts{symlinks} = 0; },
+               't|tool:s' => \$opts{difftool_cmd},
+               'tool-help' => \$opts{tool_help},
+               'x|extcmd:s' => \$opts{extcmd});
+
+       if (defined($opts{help})) {
                usage(0);
        }
-       if (defined($tool_help)) {
+       if (defined($opts{tool_help})) {
                print_tool_help();
        }
-       if (defined($difftool_cmd)) {
-               if (length($difftool_cmd) > 0) {
-                       $ENV{GIT_DIFF_TOOL} = $difftool_cmd;
+       if (defined($opts{difftool_cmd})) {
+               if (length($opts{difftool_cmd}) > 0) {
+                       $ENV{GIT_DIFF_TOOL} = $opts{difftool_cmd};
                } else {
                        print "No <tool> given for --tool=<tool>\n";
                        usage(1);
                }
        }
-       if (defined($extcmd)) {
-               if (length($extcmd) > 0) {
-                       $ENV{GIT_DIFFTOOL_EXTCMD} = $extcmd;
+       if (defined($opts{extcmd})) {
+               if (length($opts{extcmd}) > 0) {
+                       $ENV{GIT_DIFFTOOL_EXTCMD} = $opts{extcmd};
                } else {
                        print "No <cmd> given for --extcmd=<cmd>\n";
                        usage(1);
                }
        }
-       if ($gui) {
-               my $guitool = '';
-               $guitool = Git::config('diff.guitool');
+       if ($opts{gui}) {
+               my $guitool = Git::config('diff.guitool');
                if (length($guitool) > 0) {
                        $ENV{GIT_DIFF_TOOL} = $guitool;
                }
@@ -308,22 +338,22 @@ sub main
        # to compare the a/b directories.  In file diff mode, 'git diff'
        # will invoke a separate instance of 'git-difftool--helper' for
        # each file that changed.
-       if (defined($dirdiff)) {
-               dir_diff($extcmd);
+       if (defined($opts{dirdiff})) {
+               dir_diff($opts{extcmd}, $opts{symlinks});
        } else {
-               file_diff($prompt);
+               file_diff($opts{prompt});
        }
 }
 
 sub dir_diff
 {
-       my ($extcmd) = @_;
+       my ($extcmd, $symlinks) = @_;
 
        my $rc;
        my $repo = Git->repository();
 
        my $workdir = find_worktree($repo);
-       my ($a, $b, @working_tree) = setup_dir_diff($repo, $workdir);
+       my ($a, $b, @worktree) = setup_dir_diff($repo, $workdir, $symlinks);
        if (defined($extcmd)) {
                $rc = system($extcmd, $a, $b);
        } else {
@@ -335,13 +365,27 @@ sub dir_diff
 
        # 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
-       for my $file (@working_tree) {
-               if (-e "$b/$file" && compare("$b/$file", "$workdir/$file")) {
+       # should be copied back to the working tree.
+       # Do not copy back files when symlinks are used and the
+       # external tool did not replace the original link with a file.
+       for my $file (@worktree) {
+               next if $symlinks && -l "$b/$file";
+               next if ! -f "$b/$file";
+
+               my $diff = compare("$b/$file", "$workdir/$file");
+               if ($diff == 0) {
+                       next;
+               } elsif ($diff == -1) {
+                       my $errmsg = "warning: Could not compare ";
+                       $errmsg += "'$b/$file' with '$workdir/$file'\n";
+                       warn $errmsg;
+               } elsif ($diff == 1) {
                        copy("$b/$file", "$workdir/$file") or die $!;
-                       chmod(stat("$b/$file")->mode, "$workdir/$file") or die $!;
+                       my $mode = stat("$b/$file")->mode;
+                       chmod($mode, "$workdir/$file") or die $!;
                }
        }
+       exit(0);
 }
 
 sub file_diff