Merge branch 'ao/send-email-irt'
[git/kirr.git] / git-difftool.perl
blobe95e4ad973f21dd104950dc6ebb28485844b36f4
1 #!/usr/bin/env perl
2 # Copyright (c) 2009, 2010 David Aguilar
4 # This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
5 # git-difftool--helper script.
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.
11 # Any arguments that are unknown to this script are forwarded to 'git diff'.
13 use 5.008;
14 use strict;
15 use warnings;
16 use Cwd qw(abs_path);
17 use File::Basename qw(dirname);
19 require Git;
21 my $DIR = abs_path(dirname($0));
24 sub usage
26 print << 'USAGE';
27 usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
28 [-y|--no-prompt] [-g|--gui]
29 ['git diff' options]
30 USAGE
31 exit 1;
34 sub setup_environment
36 $ENV{PATH} = "$DIR:$ENV{PATH}";
37 $ENV{GIT_PAGER} = '';
38 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
41 sub exe
43 my $exe = shift;
44 if ($^O eq 'MSWin32' || $^O eq 'msys') {
45 return "$exe.exe";
47 return $exe;
50 sub generate_command
52 my @command = (exe('git'), 'diff');
53 my $skip_next = 0;
54 my $idx = -1;
55 for my $arg (@ARGV) {
56 $idx++;
57 if ($skip_next) {
58 $skip_next = 0;
59 next;
61 if ($arg eq '-t' || $arg eq '--tool') {
62 usage() if $#ARGV <= $idx;
63 $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
64 $skip_next = 1;
65 next;
67 if ($arg =~ /^--tool=/) {
68 $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
69 next;
71 if ($arg eq '-x' || $arg eq '--extcmd') {
72 usage() if $#ARGV <= $idx;
73 $ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
74 $skip_next = 1;
75 next;
77 if ($arg =~ /^--extcmd=/) {
78 $ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
79 next;
81 if ($arg eq '-g' || $arg eq '--gui') {
82 eval {
83 my $tool = Git::command_oneline('config',
84 'diff.guitool');
85 if (length($tool)) {
86 $ENV{GIT_DIFF_TOOL} = $tool;
89 next;
91 if ($arg eq '-y' || $arg eq '--no-prompt') {
92 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
93 delete $ENV{GIT_DIFFTOOL_PROMPT};
94 next;
96 if ($arg eq '--prompt') {
97 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
98 delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
99 next;
101 if ($arg eq '-h' || $arg eq '--help') {
102 usage();
104 push @command, $arg;
106 return @command
109 setup_environment();
111 # ActiveState Perl for Win32 does not implement POSIX semantics of
112 # exec* system call. It just spawns the given executable and finishes
113 # the starting program, exiting with code 0.
114 # system will at least catch the errors returned by git diff,
115 # allowing the caller of git difftool better handling of failures.
116 my $rc = system(generate_command());
117 exit($rc | ($rc >> 8));