1fc087c5fc99962cc3b62810e4df97ebf7439a0c
[git/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_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
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
22 usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
23 USAGE
24 exit 1;
25 }
26
27 sub setup_environment
28 {
29 $ENV{PATH} = "$DIR:$ENV{PATH}";
30 $ENV{GIT_PAGER} = '';
31 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
32 }
33
34 sub exe
35 {
36 my $exe = shift;
37 return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
38 }
39
40 sub 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
73 setup_environment();
74 exec(generate_command());