fcm_graphic_diff 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env perl
  2. #-------------------------------------------------------------------------------
  3. # (C) Crown copyright Met Office. All rights reserved.
  4. # For further details please refer to the file COPYRIGHT.txt
  5. # which you should have received as part of this distribution.
  6. #-------------------------------------------------------------------------------
  7. use strict;
  8. use warnings;
  9. use Getopt::Long qw{GetOptions};
  10. # ------------------------------------------------------------------------------
  11. my ($u, @label);
  12. GetOptions ('u' => \$u, 'L=s' => \@label);
  13. # Check existence of files
  14. for my $i (0 .. 1) {
  15. die $ARGV[$i], ': not found, abort' unless $ARGV[$i] and -f $ARGV[$i];
  16. }
  17. my ($old, $new) = @ARGV;
  18. if ($old =~ m#.svn/empty-file$#) {
  19. print 'Skipping new file', "\n\n";
  20. } elsif ($new =~ m#.svn/empty-file$#) {
  21. print 'Skipping deleted file', "\n\n";
  22. } elsif (-z $old) {
  23. print 'Skipping as old file is empty (or does not exist)', "\n\n";
  24. } elsif (-z $new) {
  25. print 'Skipping as new file is empty (or deleted)', "\n\n";
  26. } elsif (-B $new) {
  27. print 'Skipping binary file', "\n\n";
  28. } else {
  29. # Print descriptions of files
  30. if (@label >= 2) {
  31. print '--- ', $label[0], "\n", '+++ ', $label[1], "\n\n";
  32. }
  33. # FCM_GRAPHIC_DIFF is the graphical diff tool command
  34. my $cmd = (exists $ENV{FCM_GRAPHIC_DIFF} ? $ENV{FCM_GRAPHIC_DIFF} : 'xxdiff');
  35. if ($cmd) {
  36. my @options = ();
  37. # Set options for labels if appropriate
  38. if (@label >= 2) {
  39. if ($cmd eq 'tkdiff') {
  40. # Use tkdiff
  41. @options = ('-L', $label[0], '-L', $label[1]);
  42. } elsif ($cmd eq 'xxdiff') {
  43. # Use xxdiff
  44. @options = ('--title1', $label[0], '--title2', $label[1]);
  45. }
  46. }
  47. # Execute the command
  48. my @command = ($cmd, @options, $old, $new);
  49. exec (@command) or die 'Cannot execute: ', join (' ', @command);
  50. }
  51. exit;
  52. }
  53. __END__
  54. =head1 NAME
  55. fcm_graphic_diff
  56. =head1 SYNOPSIS
  57. fcm_graphic_diff [-u] [-L OLD_DESC] [-L NEW_DESC] OLD NEW
  58. =head1 DESCRIPTION
  59. Wrapper script which invokes a graphical diff tool. Its interface is
  60. compatible with the "svn diff" command and can be used in combination with
  61. its "--diff-cmd" option. The command prints the OLD_DESC and NEW_DESC if
  62. they are both set. The two arguments OLD and NEW must be set and are the
  63. files to compare. The graphical diff tool invoked depends on the value of
  64. the FCM_GRAPHIC_DIFF environment variable. The command exits if the
  65. environment variable is not set.
  66. =head1 COPYRIGHT
  67. (C) Crown copyright Met Office. All rights reserved.
  68. =cut