David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # Copyright (c) 2009 David Aguilar |
| 3 | # |
| 4 | # This is a wrapper around the GIT_EXTERNAL_DIFF-compatible |
David Aguilar | a904392 | 2009-04-07 01:21:22 -0700 | [diff] [blame] | 5 | # git-difftool--helper script. |
| 6 | # |
| 7 | # This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git. |
| 8 | # GIT_DIFFTOOL_NO_PROMPT, GIT_DIFFTOOL_PROMPT, and GIT_DIFF_TOOL |
| 9 | # are exported for use by git-difftool--helper. |
| 10 | # |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 11 | # Any arguments that are unknown to this script are forwarded to 'git diff'. |
| 12 | |
| 13 | use strict; |
| 14 | use warnings; |
| 15 | use Cwd qw(abs_path); |
| 16 | use File::Basename qw(dirname); |
| 17 | |
| 18 | my $DIR = abs_path(dirname($0)); |
| 19 | |
| 20 | |
| 21 | sub usage |
| 22 | { |
| 23 | print << 'USAGE'; |
David Aguilar | 8b73322 | 2009-04-07 01:21:19 -0700 | [diff] [blame] | 24 | usage: git difftool [--tool=<tool>] [-y|--no-prompt] ["git diff" options] |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 25 | USAGE |
| 26 | exit 1; |
| 27 | } |
| 28 | |
| 29 | sub setup_environment |
| 30 | { |
| 31 | $ENV{PATH} = "$DIR:$ENV{PATH}"; |
| 32 | $ENV{GIT_PAGER} = ''; |
David Aguilar | afcbc8e | 2009-04-07 01:21:20 -0700 | [diff] [blame] | 33 | $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper'; |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | sub exe |
| 37 | { |
| 38 | my $exe = shift; |
David Aguilar | 46ae156 | 2009-04-06 01:31:21 -0700 | [diff] [blame] | 39 | if ($^O eq 'MSWin32' || $^O eq 'msys') { |
| 40 | return "$exe.exe"; |
| 41 | } |
| 42 | return $exe; |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | sub generate_command |
| 46 | { |
| 47 | my @command = (exe('git'), 'diff'); |
| 48 | my $skip_next = 0; |
| 49 | my $idx = -1; |
| 50 | for my $arg (@ARGV) { |
| 51 | $idx++; |
| 52 | if ($skip_next) { |
| 53 | $skip_next = 0; |
| 54 | next; |
| 55 | } |
David Aguilar | 46ae156 | 2009-04-06 01:31:21 -0700 | [diff] [blame] | 56 | if ($arg eq '-t' || $arg eq '--tool') { |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 57 | usage() if $#ARGV <= $idx; |
David Aguilar | 2464456 | 2009-03-09 02:12:36 -0700 | [diff] [blame] | 58 | $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1]; |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 59 | $skip_next = 1; |
| 60 | next; |
| 61 | } |
| 62 | if ($arg =~ /^--tool=/) { |
David Aguilar | 2464456 | 2009-03-09 02:12:36 -0700 | [diff] [blame] | 63 | $ENV{GIT_DIFF_TOOL} = substr($arg, 7); |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 64 | next; |
| 65 | } |
David Aguilar | 8b73322 | 2009-04-07 01:21:19 -0700 | [diff] [blame] | 66 | if ($arg eq '-y' || $arg eq '--no-prompt') { |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 67 | $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true'; |
David Aguilar | a904392 | 2009-04-07 01:21:22 -0700 | [diff] [blame] | 68 | delete $ENV{GIT_DIFFTOOL_PROMPT}; |
| 69 | next; |
| 70 | } |
| 71 | if ($arg eq '--prompt') { |
| 72 | $ENV{GIT_DIFFTOOL_PROMPT} = 'true'; |
| 73 | delete $ENV{GIT_DIFFTOOL_NO_PROMPT}; |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 74 | next; |
| 75 | } |
David Aguilar | 8b73322 | 2009-04-07 01:21:19 -0700 | [diff] [blame] | 76 | if ($arg eq '-h' || $arg eq '--help') { |
David Aguilar | 5c38ea3 | 2009-01-16 00:00:02 -0800 | [diff] [blame] | 77 | usage(); |
| 78 | } |
| 79 | push @command, $arg; |
| 80 | } |
| 81 | return @command |
| 82 | } |
| 83 | |
| 84 | setup_environment(); |
Alex Riesen | 677fbff | 2009-04-23 21:18:09 +0200 | [diff] [blame] | 85 | |
| 86 | # ActiveState Perl for Win32 does not implement POSIX semantics of |
| 87 | # exec* system call. It just spawns the given executable and finishes |
| 88 | # the starting program, exiting with code 0. |
| 89 | # system will at least catch the errors returned by git diff, |
| 90 | # allowing the caller of git difftool better handling of failures. |
Alex Riesen | e8d1180 | 2009-04-22 09:27:22 +0200 | [diff] [blame] | 91 | my $rc = system(generate_command()); |
| 92 | exit($rc | ($rc >> 8)); |