[5129] | 1 | #!/usr/bin/env perl |
---|
| 2 | #------------------------------------------------------------------------------- |
---|
| 3 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
| 4 | # |
---|
| 5 | # This file is part of FCM, tools for managing and building source code. |
---|
| 6 | # |
---|
| 7 | # FCM is free software: you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # FCM is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | #------------------------------------------------------------------------------- |
---|
| 20 | |
---|
| 21 | use strict; |
---|
| 22 | use warnings; |
---|
| 23 | |
---|
| 24 | use Getopt::Long qw{GetOptions}; |
---|
| 25 | use Pod::Usage qw{pod2usage}; |
---|
| 26 | |
---|
| 27 | my $RE_SVN_EMPTY_FILE = qr{\.svn/empty-file}msx; |
---|
| 28 | |
---|
| 29 | my %S = ( |
---|
| 30 | 'LABEL' => "--- %s\n+++ %s", |
---|
| 31 | 'SKIP_ADD' => "Skipping since file has been added (or old file is empty)", |
---|
| 32 | 'SKIP_DEL' => "Skipping since file has been deleted (or new file is empty)", |
---|
| 33 | 'SKIP_BIN' => "Skipping binary file", |
---|
| 34 | ); |
---|
| 35 | my %LABELS_HANDLER_FOR = ( |
---|
| 36 | 'tkdiff' => sub {map {('-L', $_)} @_}, |
---|
| 37 | 'xxdiff' => sub {('--title1', $_[0], '--title2', $_[1])}, |
---|
| 38 | 'meld' => sub {map {('-L', $_)} @_}, |
---|
| 39 | ); |
---|
| 40 | |
---|
| 41 | if (!caller()) { |
---|
| 42 | # svn diff expects: |
---|
| 43 | # 0 - no diff |
---|
| 44 | # 1 - diff |
---|
| 45 | # other return code - fatal |
---|
| 46 | exit main(@ARGV); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | sub main { |
---|
| 50 | local(@ARGV) = @_; |
---|
| 51 | my %option; |
---|
| 52 | my $rc = GetOptions(\%option, 'u', 'L=s@'); |
---|
| 53 | if (!$rc || @ARGV != 2 || grep {!-f $_} @ARGV) { |
---|
| 54 | pod2usage(1); |
---|
| 55 | } |
---|
| 56 | my ($old, $new) = @ARGV; |
---|
| 57 | ( $old =~ $RE_SVN_EMPTY_FILE || -z $old ? message('SKIP_ADD') |
---|
| 58 | : $new =~ $RE_SVN_EMPTY_FILE || -z $new ? message('SKIP_DEL') |
---|
| 59 | : -B $new ? message('SKIP_BIN') |
---|
| 60 | : command(\%option, @ARGV) |
---|
| 61 | ); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | sub command { |
---|
| 65 | my ($option_hash_ref, $old, $new) = @_; |
---|
| 66 | my @labels; |
---|
| 67 | if ($option_hash_ref->{'L'} && @{$option_hash_ref->{'L'}} >= 2) { |
---|
| 68 | @labels = @{$option_hash_ref->{'L'}}; |
---|
| 69 | message('LABEL', @labels); |
---|
| 70 | } |
---|
| 71 | my $diff_command |
---|
| 72 | = exists($ENV{FCM_GRAPHIC_DIFF}) ? $ENV{FCM_GRAPHIC_DIFF} : 'xxdiff'; |
---|
| 73 | if (!$diff_command) { |
---|
| 74 | return; |
---|
| 75 | } |
---|
| 76 | my @command = ( |
---|
| 77 | $diff_command, |
---|
| 78 | ( @labels && exists($LABELS_HANDLER_FOR{$diff_command}) |
---|
| 79 | ? $LABELS_HANDLER_FOR{$diff_command}->(@labels) : () |
---|
| 80 | ), |
---|
| 81 | $old, $new, |
---|
| 82 | ); |
---|
| 83 | system(@command); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | sub message { |
---|
| 87 | my $format = shift(); |
---|
| 88 | printf($S{$format} . "\n\n", @_); |
---|
| 89 | 1; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | __END__ |
---|
| 93 | |
---|
| 94 | =head1 NAME |
---|
| 95 | |
---|
| 96 | fcm_graphic_diff |
---|
| 97 | |
---|
| 98 | =head1 SYNOPSIS |
---|
| 99 | |
---|
| 100 | fcm_graphic_diff [-u] [-L OLD_LABEL] [-L NEW_LABEL] OLD NEW |
---|
| 101 | |
---|
| 102 | =head1 DESCRIPTION |
---|
| 103 | |
---|
| 104 | Invokes L<xxdiff|xxdiff> (or the command specified in the FCM_GRAPHIC_DIFF |
---|
| 105 | environment variable) to compare the OLD and NEW files, where possible. |
---|
| 106 | |
---|
| 107 | If either file does not exist or is empty, or if the NEW file is a binary, the |
---|
| 108 | command will only print a diagnostic message. |
---|
| 109 | |
---|
| 110 | The -u option is not used, and is for compatibility with the L<svn diff|svn> |
---|
| 111 | command only. |
---|
| 112 | |
---|
| 113 | If OLD_LABEL and NEW_LABEL are set, they are printed in the format: |
---|
| 114 | |
---|
| 115 | ---- OLD_LABEL |
---|
| 116 | ++++ NEW_LABEL |
---|
| 117 | |
---|
| 118 | The command makes use of the labels when the diff command is either |
---|
| 119 | L<xxdiff|xxdiff>, L<tkdiff|tkdiff>, or L<meld|meld>: |
---|
| 120 | |
---|
| 121 | xxdiff --title1 OLD_LABEL --title2 NEW_LABEL OLD NEW |
---|
| 122 | tkdiff -L OLD_LABEL -L NEW_LABEL OLD NEW |
---|
| 123 | meld -L OLD_LABEL -L NEW_LABEL OLD NEW |
---|
| 124 | |
---|
| 125 | The command returns 0 if the files are the same or 1 if the files differ. All |
---|
| 126 | other return codes should be regarded as fatal errors. |
---|
| 127 | |
---|
| 128 | =head1 COPYRIGHT |
---|
| 129 | |
---|
| 130 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
| 131 | |
---|
| 132 | =cut |
---|