Makefile: Fix occasional GIT-CFLAGS breakage
[alt-git.git] / git-difftool.perl
blobd975d072dbf0fab36266c3f3a71a69875206e4d8
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 strict;
14 use warnings;
15 use Cwd qw(abs_path);
16 use File::Basename qw(dirname);
18 require Git;
20 my $DIR = abs_path(dirname($0));
23 sub usage
25 print << 'USAGE';
26 usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
27 [-y|--no-prompt] [-g|--gui]
28 ['git diff' options]
29 USAGE
30 exit 1;
33 sub setup_environment
35 $ENV{PATH} = "$DIR:$ENV{PATH}";
36 $ENV{GIT_PAGER} = '';
37 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
40 sub exe
42 my $exe = shift;
43 if ($^O eq 'MSWin32' || $^O eq 'msys') {
44 return "$exe.exe";
46 return $exe;
49 sub generate_command
51 my @command = (exe('git'), 'diff');
52 my $skip_next = 0;
53 my $idx = -1;
54 for my $arg (@ARGV) {
55 $idx++;
56 if ($skip_next) {
57 $skip_next = 0;
58 next;
60 if ($arg eq '-t' || $arg eq '--tool') {
61 usage() if $#ARGV <= $idx;
62 $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
63 $skip_next = 1;
64 next;
66 if ($arg =~ /^--tool=/) {
67 $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
68 next;
70 if ($arg eq '-x' || $arg eq '--extcmd') {
71 usage() if $#ARGV <= $idx;
72 $ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
73 $skip_next = 1;
74 next;
76 if ($arg =~ /^--extcmd=/) {
77 $ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
78 next;
80 if ($arg eq '-g' || $arg eq '--gui') {
81 my $tool = Git::command_oneline('config',
82 'diff.guitool');
83 if (length($tool)) {
84 $ENV{GIT_DIFF_TOOL} = $tool;
86 next;
88 if ($arg eq '-y' || $arg eq '--no-prompt') {
89 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
90 delete $ENV{GIT_DIFFTOOL_PROMPT};
91 next;
93 if ($arg eq '--prompt') {
94 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
95 delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
96 next;
98 if ($arg eq '-h' || $arg eq '--help') {
99 usage();
101 push @command, $arg;
103 return @command
106 setup_environment();
108 # ActiveState Perl for Win32 does not implement POSIX semantics of
109 # exec* system call. It just spawns the given executable and finishes
110 # the starting program, exiting with code 0.
111 # system will at least catch the errors returned by git diff,
112 # allowing the caller of git difftool better handling of failures.
113 my $rc = system(generate_command());
114 exit($rc | ($rc >> 8));