1#!/usr/bin/env perl
2# Copyright (c) 2009 David Aguilar
3#
4# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
5# git-difftool-helper script. This script exports
6# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
7# GIT_DIFFTOOL_NO_PROMPT and GIT_DIFF_TOOL for use by git-difftool-helper.
8# Any arguments that are unknown to this script are forwarded to 'git diff'.
9
10use strict;
11use warnings;
12use Cwd qw(abs_path);
13use File::Basename qw(dirname);
14
15my $DIR = abs_path(dirname($0));
16
17
18sub usage
19{
20 print << 'USAGE';
21usage: git difftool [--tool=<tool>] [-y|--no-prompt] ["git diff" options]
22USAGE
23 exit 1;
24}
25
26sub setup_environment
27{
28 $ENV{PATH} = "$DIR:$ENV{PATH}";
29 $ENV{GIT_PAGER} = '';
30 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
31}
32
33sub exe
34{
35 my $exe = shift;
36 if ($^O eq 'MSWin32' || $^O eq 'msys') {
37 return "$exe.exe";
38 }
39 return $exe;
40}
41
42sub generate_command
43{
44 my @command = (exe('git'), 'diff');
45 my $skip_next = 0;
46 my $idx = -1;
47 for my $arg (@ARGV) {
48 $idx++;
49 if ($skip_next) {
50 $skip_next = 0;
51 next;
52 }
53 if ($arg eq '-t' || $arg eq '--tool') {
54 usage() if $#ARGV <= $idx;
55 $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
56 $skip_next = 1;
57 next;
58 }
59 if ($arg =~ /^--tool=/) {
60 $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
61 next;
62 }
63 if ($arg eq '-y' || $arg eq '--no-prompt') {
64 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
65 next;
66 }
67 if ($arg eq '-h' || $arg eq '--help') {
68 usage();
69 }
70 push @command, $arg;
71 }
72 return @command
73}
74
75setup_environment();
76exec(generate_command());