Ignore:
Timestamp:
Jul 21, 2024, 1:47:00 PM (4 months ago)
Author:
abarral
Message:

Fix r5093: ship new fcm source

File:
1 edited

Legend:

Unmodified
Added
Removed
  • LMDZ6/branches/Amaury_dev/tools/fcm/bin/fcm_graphic_diff

    r1578 r5094  
    1 #!/usr/bin/perl
    2 # ------------------------------------------------------------------------------
    3 # NAME
    4 #   fcm_graphic_diff
     1#!/usr/bin/env perl
     2#-------------------------------------------------------------------------------
     3# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
    54#
    6 # SYNOPSIS
    7 #   fcm_graphic_diff [-u] [-L OLD_DESC] [-L NEW_DESC] OLD NEW
     5# This file is part of FCM, tools for managing and building source code.
    86#
    9 # DESCRIPTION
    10 #   Wrapper script which invokes a graphical diff tool. Its interface is
    11 #   compatible with the "svn diff" command and can be used in combination with
    12 #   its "--diff-cmd" option. The command prints the OLD_DESC and NEW_DESC if
    13 #   they are both set. The two arguments OLD and NEW must be set and are the
    14 #   files to compare. The graphical diff tool invoked depends on the value of
    15 #   the FCM_GRAPHIC_DIFF environment variable. The command exits if the
    16 #   environment variable is not set.
     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.
    1711#
    18 # COPYRIGHT
    19 #   This program is part of the FCM system.
    20 #   (C) Crown copyright Met Office. All rights reserved.
    21 #   For further details please refer to the file COPYRIGHT.txt
    22 #   which you should have received as part of this distribution.
    23 # ------------------------------------------------------------------------------
     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#-------------------------------------------------------------------------------
    2420
    25 # Standard pragmas:
     21use strict;
    2622use warnings;
    27 use strict;
    2823
    29 use Getopt::Long;
     24use Getopt::Long qw{GetOptions};
     25use Pod::Usage qw{pod2usage};
    3026
    31 # ------------------------------------------------------------------------------
     27my $RE_SVN_EMPTY_FILE = qr{\.svn/empty-file}msx;
    3228
    33 my ($u, @label);
    34 GetOptions ('u' => \$u, 'L=s' => \@label);
     29my %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);
     35my %LABELS_HANDLER_FOR = (
     36    'tkdiff' => sub {map {('-L', $_)} @_},
     37    'xxdiff' => sub {('--title1', $_[0], '--title2', $_[1])},
     38    'meld'   => sub {map {('-L', $_)} @_},
     39);
    3540
    36 # Check existence of files
    37 for my $i (0 .. 1) {
    38   die $ARGV[$i], ': not found, abort' unless $ARGV[$i] and -f $ARGV[$i];
     41if (!caller()) {
     42    # svn diff expects:
     43    # 0 - no diff
     44    # 1 - diff
     45    # other return code - fatal
     46    exit main(@ARGV);
    3947}
    4048
    41 my ($old, $new) = @ARGV;
     49sub 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}
    4263
    43 if ($old =~ m#.svn/empty-file$#) {
    44   print 'Skipping new file', "\n\n";
     64sub 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}
    4585
    46 } elsif ($new =~ m#.svn/empty-file$#) {
    47   print 'Skipping deleted file', "\n\n";
    48 
    49 } elsif (-z $old) {
    50   print 'Skipping as old file is empty (or does not exist)', "\n\n";
    51 
    52 } elsif (-z $new) {
    53   print 'Skipping as new file is empty (or deleted)', "\n\n";
    54 
    55 } elsif (-B $new) {
    56   print 'Skipping binary file', "\n\n";
    57 
    58 } else {
    59   # Print descriptions of files
    60   if (@label >= 2) {
    61     print '--- ', $label[0], "\n", '+++ ', $label[1], "\n\n";
    62   }
    63 
    64   # FCM_GRAPHIC_DIFF is the graphical diff tool command
    65   my $cmd = (exists $ENV{FCM_GRAPHIC_DIFF} ? $ENV{FCM_GRAPHIC_DIFF} : 'xxdiff');
    66 
    67   if ($cmd) {
    68     my @options = ();
    69 
    70     # Set options for labels if appropriate
    71     if (@label >= 2) {
    72       if ($cmd eq 'tkdiff') {
    73         # Use tkdiff
    74         @options = ('-L', $label[0], '-L', $label[1]);
    75 
    76       } elsif ($cmd eq 'xxdiff') {
    77         # Use xxdiff
    78         @options = ('--title1', $label[0], '--title2', $label[1]);
    79       }
    80     }
    81 
    82     # Execute the command
    83     my @command = ($cmd, @options, $old, $new);
    84     exec (@command) or die 'Cannot execute: ', join (' ', @command);
    85   }
    86 
    87   exit;
     86sub message {
     87    my $format = shift();
     88    printf($S{$format} . "\n\n", @_);
     89    1;
    8890}
    8991
    9092__END__
     93
     94=head1 NAME
     95
     96fcm_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
     104Invokes L<xxdiff|xxdiff> (or the command specified in the FCM_GRAPHIC_DIFF
     105environment variable) to compare the OLD and NEW files, where possible.
     106
     107If either file does not exist or is empty, or if the NEW file is a binary, the
     108command will only print a diagnostic message.
     109
     110The -u option is not used, and is for compatibility with the L<svn diff|svn>
     111command only.
     112
     113If OLD_LABEL and NEW_LABEL are set, they are printed in the format:
     114
     115    ---- OLD_LABEL
     116    ++++ NEW_LABEL
     117
     118The command makes use of the labels when the diff command is either
     119L<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
     125The command returns 0 if the files are the same or 1 if the files differ. All
     126other return codes should be regarded as fatal errors.
     127
     128=head1 COPYRIGHT
     129
     130Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
     131
     132=cut
Note: See TracChangeset for help on using the changeset viewer.