blob: 0deda3a0e41511bb1b71d30d335b4909e8719169 [file] [log] [blame]
David Aguilar5c38ea32009-01-16 00:00:02 -08001#!/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
David Aguilar24644562009-03-09 02:12:36 -07007# GIT_DIFFTOOL_NO_PROMPT and GIT_DIFF_TOOL for use by git-difftool-helper.
David Aguilar5c38ea32009-01-16 00:00:02 -08008# 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';
David Aguilar507cfcb2009-01-18 21:27:19 -080021usage: git difftool [--tool=<tool>] [--no-prompt] ["git diff" options]
David Aguilar5c38ea32009-01-16 00:00:02 -080022USAGE
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 return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
37}
38
39sub generate_command
40{
41 my @command = (exe('git'), 'diff');
42 my $skip_next = 0;
43 my $idx = -1;
44 for my $arg (@ARGV) {
45 $idx++;
46 if ($skip_next) {
47 $skip_next = 0;
48 next;
49 }
50 if ($arg eq '-t' or $arg eq '--tool') {
51 usage() if $#ARGV <= $idx;
David Aguilar24644562009-03-09 02:12:36 -070052 $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
David Aguilar5c38ea32009-01-16 00:00:02 -080053 $skip_next = 1;
54 next;
55 }
56 if ($arg =~ /^--tool=/) {
David Aguilar24644562009-03-09 02:12:36 -070057 $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
David Aguilar5c38ea32009-01-16 00:00:02 -080058 next;
59 }
60 if ($arg eq '--no-prompt') {
61 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
62 next;
63 }
64 if ($arg eq '-h' or $arg eq '--help') {
65 usage();
66 }
67 push @command, $arg;
68 }
69 return @command
70}
71
72setup_environment();
73exec(generate_command());