2009-01-16 08:00:02 +00:00
|
|
|
#!/usr/bin/env perl
|
2010-01-15 22:03:43 +00:00
|
|
|
# Copyright (c) 2009, 2010 David Aguilar
|
2009-01-16 08:00:02 +00:00
|
|
|
#
|
|
|
|
# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
|
2009-04-07 08:21:22 +00:00
|
|
|
# git-difftool--helper script.
|
|
|
|
#
|
|
|
|
# This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
|
|
|
|
# GIT_DIFFTOOL_NO_PROMPT, GIT_DIFFTOOL_PROMPT, and GIT_DIFF_TOOL
|
|
|
|
# are exported for use by git-difftool--helper.
|
|
|
|
#
|
2009-01-16 08:00:02 +00:00
|
|
|
# Any arguments that are unknown to this script are forwarded to 'git diff'.
|
|
|
|
|
2010-09-24 20:00:52 +00:00
|
|
|
use 5.008;
|
2009-01-16 08:00:02 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Cwd qw(abs_path);
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
|
2009-12-23 05:27:14 +00:00
|
|
|
require Git;
|
|
|
|
|
2009-01-16 08:00:02 +00:00
|
|
|
my $DIR = abs_path(dirname($0));
|
|
|
|
|
|
|
|
|
|
|
|
sub usage
|
|
|
|
{
|
|
|
|
print << 'USAGE';
|
2010-01-15 22:03:43 +00:00
|
|
|
usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
|
|
|
|
[-y|--no-prompt] [-g|--gui]
|
|
|
|
['git diff' options]
|
2009-01-16 08:00:02 +00:00
|
|
|
USAGE
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub setup_environment
|
|
|
|
{
|
|
|
|
$ENV{PATH} = "$DIR:$ENV{PATH}";
|
|
|
|
$ENV{GIT_PAGER} = '';
|
2009-04-07 08:21:20 +00:00
|
|
|
$ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
|
2009-01-16 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub exe
|
|
|
|
{
|
|
|
|
my $exe = shift;
|
2009-04-06 08:31:21 +00:00
|
|
|
if ($^O eq 'MSWin32' || $^O eq 'msys') {
|
|
|
|
return "$exe.exe";
|
|
|
|
}
|
|
|
|
return $exe;
|
2009-01-16 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub generate_command
|
|
|
|
{
|
|
|
|
my @command = (exe('git'), 'diff');
|
|
|
|
my $skip_next = 0;
|
|
|
|
my $idx = -1;
|
|
|
|
for my $arg (@ARGV) {
|
|
|
|
$idx++;
|
|
|
|
if ($skip_next) {
|
|
|
|
$skip_next = 0;
|
|
|
|
next;
|
|
|
|
}
|
2009-04-06 08:31:21 +00:00
|
|
|
if ($arg eq '-t' || $arg eq '--tool') {
|
2009-01-16 08:00:02 +00:00
|
|
|
usage() if $#ARGV <= $idx;
|
2009-03-09 09:12:36 +00:00
|
|
|
$ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
|
2009-01-16 08:00:02 +00:00
|
|
|
$skip_next = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($arg =~ /^--tool=/) {
|
2009-03-09 09:12:36 +00:00
|
|
|
$ENV{GIT_DIFF_TOOL} = substr($arg, 7);
|
2009-01-16 08:00:02 +00:00
|
|
|
next;
|
|
|
|
}
|
2010-01-15 22:03:43 +00:00
|
|
|
if ($arg eq '-x' || $arg eq '--extcmd') {
|
|
|
|
usage() if $#ARGV <= $idx;
|
|
|
|
$ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
|
|
|
|
$skip_next = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($arg =~ /^--extcmd=/) {
|
|
|
|
$ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
|
|
|
|
next;
|
|
|
|
}
|
2009-12-23 05:27:14 +00:00
|
|
|
if ($arg eq '-g' || $arg eq '--gui') {
|
2010-03-27 21:58:09 +00:00
|
|
|
eval {
|
|
|
|
my $tool = Git::command_oneline('config',
|
|
|
|
'diff.guitool');
|
|
|
|
if (length($tool)) {
|
|
|
|
$ENV{GIT_DIFF_TOOL} = $tool;
|
|
|
|
}
|
|
|
|
};
|
2009-12-23 05:27:14 +00:00
|
|
|
next;
|
|
|
|
}
|
2009-04-07 08:21:19 +00:00
|
|
|
if ($arg eq '-y' || $arg eq '--no-prompt') {
|
2009-01-16 08:00:02 +00:00
|
|
|
$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
|
2009-04-07 08:21:22 +00:00
|
|
|
delete $ENV{GIT_DIFFTOOL_PROMPT};
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($arg eq '--prompt') {
|
|
|
|
$ENV{GIT_DIFFTOOL_PROMPT} = 'true';
|
|
|
|
delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
|
2009-01-16 08:00:02 +00:00
|
|
|
next;
|
|
|
|
}
|
2009-04-07 08:21:19 +00:00
|
|
|
if ($arg eq '-h' || $arg eq '--help') {
|
2009-01-16 08:00:02 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
push @command, $arg;
|
|
|
|
}
|
|
|
|
return @command
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_environment();
|
2009-04-23 19:18:09 +00:00
|
|
|
|
|
|
|
# ActiveState Perl for Win32 does not implement POSIX semantics of
|
|
|
|
# exec* system call. It just spawns the given executable and finishes
|
|
|
|
# the starting program, exiting with code 0.
|
|
|
|
# system will at least catch the errors returned by git diff,
|
|
|
|
# allowing the caller of git difftool better handling of failures.
|
2009-04-22 07:27:22 +00:00
|
|
|
my $rc = system(generate_command());
|
|
|
|
exit($rc | ($rc >> 8));
|