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_NO_PROMPT and GIT_MERGE_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';
21
22usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
23USAGE
24 exit 1;
25}
26
27sub setup_environment
28{
29 $ENV{PATH} = "$DIR:$ENV{PATH}";
30 $ENV{GIT_PAGER} = '';
31 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
32}
33
34sub exe
35{
36 my $exe = shift;
37 return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
38}
39
40sub generate_command
41{
42 my @command = (exe('git'), 'diff');
43 my $skip_next = 0;
44 my $idx = -1;
45 for my $arg (@ARGV) {
46 $idx++;
47 if ($skip_next) {
48 $skip_next = 0;
49 next;
50 }
51 if ($arg eq '-t' or $arg eq '--tool') {
52 usage() if $#ARGV <= $idx;
53 $ENV{GIT_MERGE_TOOL} = $ARGV[$idx + 1];
54 $skip_next = 1;
55 next;
56 }
57 if ($arg =~ /^--tool=/) {
58 $ENV{GIT_MERGE_TOOL} = substr($arg, 7);
59 next;
60 }
61 if ($arg eq '--no-prompt') {
62 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
63 next;
64 }
65 if ($arg eq '-h' or $arg eq '--help') {
66 usage();
67 }
68 push @command, $arg;
69 }
70 return @command
71}
72
73setup_environment();
74exec(generate_command());