]> git.ipfire.org Git - thirdparty/git.git/blame - git-difftool.perl
Makefile: add CC to TRACK_CFLAGS
[thirdparty/git.git] / git-difftool.perl
CommitLineData
5c38ea31 1#!/usr/bin/env perl
f47f1e2c 2# Copyright (c) 2009, 2010 David Aguilar
5c38ea31
DA
3#
4# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
a904392e
DA
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#
5c38ea31
DA
11# Any arguments that are unknown to this script are forwarded to 'git diff'.
12
13use strict;
14use warnings;
15use Cwd qw(abs_path);
16use File::Basename qw(dirname);
17
4cefa495
DA
18require Git;
19
5c38ea31
DA
20my $DIR = abs_path(dirname($0));
21
22
23sub usage
24{
25 print << 'USAGE';
f47f1e2c
DA
26usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
27 [-y|--no-prompt] [-g|--gui]
28 ['git diff' options]
5c38ea31
DA
29USAGE
30 exit 1;
31}
32
33sub setup_environment
34{
35 $ENV{PATH} = "$DIR:$ENV{PATH}";
36 $ENV{GIT_PAGER} = '';
afcbc8e7 37 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
5c38ea31
DA
38}
39
40sub exe
41{
42 my $exe = shift;
46ae156d
DA
43 if ($^O eq 'MSWin32' || $^O eq 'msys') {
44 return "$exe.exe";
45 }
46 return $exe;
5c38ea31
DA
47}
48
49sub generate_command
50{
51 my @command = (exe('git'), 'diff');
52 my $skip_next = 0;
53 my $idx = -1;
54 for my $arg (@ARGV) {
55 $idx++;
56 if ($skip_next) {
57 $skip_next = 0;
58 next;
59 }
46ae156d 60 if ($arg eq '-t' || $arg eq '--tool') {
5c38ea31 61 usage() if $#ARGV <= $idx;
2464456a 62 $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
5c38ea31
DA
63 $skip_next = 1;
64 next;
65 }
66 if ($arg =~ /^--tool=/) {
2464456a 67 $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
5c38ea31
DA
68 next;
69 }
f47f1e2c
DA
70 if ($arg eq '-x' || $arg eq '--extcmd') {
71 usage() if $#ARGV <= $idx;
72 $ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
73 $skip_next = 1;
74 next;
75 }
76 if ($arg =~ /^--extcmd=/) {
77 $ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
78 next;
79 }
4cefa495 80 if ($arg eq '-g' || $arg eq '--gui') {
42accaec
DA
81 eval {
82 my $tool = Git::command_oneline('config',
83 'diff.guitool');
84 if (length($tool)) {
85 $ENV{GIT_DIFF_TOOL} = $tool;
86 }
87 };
4cefa495
DA
88 next;
89 }
8b733222 90 if ($arg eq '-y' || $arg eq '--no-prompt') {
5c38ea31 91 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
a904392e
DA
92 delete $ENV{GIT_DIFFTOOL_PROMPT};
93 next;
94 }
95 if ($arg eq '--prompt') {
96 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
97 delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
5c38ea31
DA
98 next;
99 }
8b733222 100 if ($arg eq '-h' || $arg eq '--help') {
5c38ea31
DA
101 usage();
102 }
103 push @command, $arg;
104 }
105 return @command
106}
107
108setup_environment();
677fbff8
AR
109
110# ActiveState Perl for Win32 does not implement POSIX semantics of
111# exec* system call. It just spawns the given executable and finishes
112# the starting program, exiting with code 0.
113# system will at least catch the errors returned by git diff,
114# allowing the caller of git difftool better handling of failures.
e8d11804
AR
115my $rc = system(generate_command());
116exit($rc | ($rc >> 8));