aba3d2f8cb114774ca316abb4ac72f7ad66bb769
   1#!/usr/bin/env perl
   2# Copyright (c) 2009, 2010 David Aguilar
   3#
   4# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
   5# git-difftool--helper script.
   6#
   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.
  10#
  11# Any arguments that are unknown to this script are forwarded to 'git diff'.
  12
  13use 5.008;
  14use strict;
  15use warnings;
  16use Getopt::Long qw(:config pass_through);
  17use Git;
  18
  19sub usage
  20{
  21        my $exitcode = shift;
  22        print << 'USAGE';
  23usage: git difftool [-t|--tool=<tool>]
  24                    [-x|--extcmd=<cmd>]
  25                    [-g|--gui] [--no-gui]
  26                    [--prompt] [-y|--no-prompt]
  27                    ['git diff' options]
  28USAGE
  29        exit($exitcode);
  30}
  31
  32# parse command-line options. all unrecognized options and arguments
  33# are passed through to the 'git diff' command.
  34my ($difftool_cmd, $extcmd, $gui, $help, $prompt);
  35GetOptions('g|gui!' => \$gui,
  36        'h' => \$help,
  37        'prompt!' => \$prompt,
  38        'y' => sub { $prompt = 0; },
  39        't|tool:s' => \$difftool_cmd,
  40        'x|extcmd:s' => \$extcmd);
  41
  42if (defined($help)) {
  43        usage(0);
  44}
  45if (defined($difftool_cmd)) {
  46        if (length($difftool_cmd) > 0) {
  47                $ENV{GIT_DIFF_TOOL} = $difftool_cmd;
  48        } else {
  49                print "No <tool> given for --tool=<tool>\n";
  50                usage(1);
  51        }
  52}
  53if (defined($extcmd)) {
  54        if (length($extcmd) > 0) {
  55                $ENV{GIT_DIFFTOOL_EXTCMD} = $extcmd;
  56        } else {
  57                print "No <cmd> given for --extcmd=<cmd>\n";
  58                usage(1);
  59        }
  60}
  61if ($gui) {
  62        my $guitool = "";
  63        $guitool = Git::config('diff.guitool');
  64        if (length($guitool) > 0) {
  65                $ENV{GIT_DIFF_TOOL} = $guitool;
  66        }
  67}
  68if (defined($prompt)) {
  69        if ($prompt) {
  70                $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
  71        } else {
  72                $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
  73        }
  74}
  75
  76$ENV{GIT_PAGER} = '';
  77$ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
  78my @command = ('git', 'diff', @ARGV);
  79
  80# ActiveState Perl for Win32 does not implement POSIX semantics of
  81# exec* system call. It just spawns the given executable and finishes
  82# the starting program, exiting with code 0.
  83# system will at least catch the errors returned by git diff,
  84# allowing the caller of git difftool better handling of failures.
  85my $rc = system(@command);
  86exit($rc | ($rc >> 8));