contrib: add 'git difftool' for launching common merge tools
[git/dscho.git] / contrib / difftool / git-difftool
blob1fc087c5fc99962cc3b62810e4df97ebf7439a0c
1 #!/usr/bin/env perl
2 # Copyright (c) 2009 David Aguilar
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'.
10 use strict;
11 use warnings;
12 use Cwd qw(abs_path);
13 use File::Basename qw(dirname);
15 my $DIR = abs_path(dirname($0));
18 sub usage
20 print << 'USAGE';
22 usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
23 USAGE
24 exit 1;
27 sub setup_environment
29 $ENV{PATH} = "$DIR:$ENV{PATH}";
30 $ENV{GIT_PAGER} = '';
31 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
34 sub exe
36 my $exe = shift;
37 return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
40 sub generate_command
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;
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;
57 if ($arg =~ /^--tool=/) {
58 $ENV{GIT_MERGE_TOOL} = substr($arg, 7);
59 next;
61 if ($arg eq '--no-prompt') {
62 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
63 next;
65 if ($arg eq '-h' or $arg eq '--help') {
66 usage();
68 push @command, $arg;
70 return @command
73 setup_environment();
74 exec(generate_command());