Code

difftool: Allow specifying unconfigured commands with --extcmd
[git.git] / git-difftool.perl
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.
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'.
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
24 {
25         print << 'USAGE';
26 usage: git difftool [-g|--gui] [-t|--tool=<tool>] [-y|--no-prompt]
27                     ["git diff" options]
28 USAGE
29         exit 1;
30 }
32 sub setup_environment
33 {
34         $ENV{PATH} = "$DIR:$ENV{PATH}";
35         $ENV{GIT_PAGER} = '';
36         $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
37 }
39 sub exe
40 {
41         my $exe = shift;
42         if ($^O eq 'MSWin32' || $^O eq 'msys') {
43                 return "$exe.exe";
44         }
45         return $exe;
46 }
48 sub generate_command
49 {
50         my @command = (exe('git'), 'diff');
51         my $skip_next = 0;
52         my $idx = -1;
53         for my $arg (@ARGV) {
54                 $idx++;
55                 if ($skip_next) {
56                         $skip_next = 0;
57                         next;
58                 }
59                 if ($arg eq '-t' || $arg eq '--tool') {
60                         usage() if $#ARGV <= $idx;
61                         $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
62                         $skip_next = 1;
63                         next;
64                 }
65                 if ($arg =~ /^--extcmd=/) {
66                         $ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
67                         next;
68                 }
69                 if ($arg =~ /^--tool=/) {
70                         $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
71                         next;
72                 }
73                 if ($arg eq '-g' || $arg eq '--gui') {
74                         my $tool = Git::command_oneline('config',
75                                                         'diff.guitool');
76                         if (length($tool)) {
77                                 $ENV{GIT_DIFF_TOOL} = $tool;
78                         }
79                         next;
80                 }
81                 if ($arg eq '-y' || $arg eq '--no-prompt') {
82                         $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
83                         delete $ENV{GIT_DIFFTOOL_PROMPT};
84                         next;
85                 }
86                 if ($arg eq '--prompt') {
87                         $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
88                         delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
89                         next;
90                 }
91                 if ($arg eq '-h' || $arg eq '--help') {
92                         usage();
93                 }
94                 push @command, $arg;
95         }
96         return @command
97 }
99 setup_environment();
101 # ActiveState Perl for Win32 does not implement POSIX semantics of
102 # exec* system call. It just spawns the given executable and finishes
103 # the starting program, exiting with code 0.
104 # system will at least catch the errors returned by git diff,
105 # allowing the caller of git difftool better handling of failures.
106 my $rc = system(generate_command());
107 exit($rc | ($rc >> 8));