contrib/difftool: use a separate config namespace for difftool commands
[git.git] / contrib / difftool / git-difftool
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
10 use strict;
11 use warnings;
12 use Cwd qw(abs_path);
13 use File::Basename qw(dirname);
14
15 my $DIR = abs_path(dirname($0));
16
17
18 sub usage
19 {
20         print << 'USAGE';
21 usage: git difftool [--tool=<tool>] [--no-prompt] ["git diff" options]
22 USAGE
23         exit 1;
24 }
25
26 sub setup_environment
27 {
28         $ENV{PATH} = "$DIR:$ENV{PATH}";
29         $ENV{GIT_PAGER} = '';
30         $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
31 }
32
33 sub exe
34 {
35         my $exe = shift;
36         return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
37 }
38
39 sub 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;
52                         $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
53                         $skip_next = 1;
54                         next;
55                 }
56                 if ($arg =~ /^--tool=/) {
57                         $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
58                         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
72 setup_environment();
73 exec(generate_command());