Skip to content

Commit 725ee97

Browse files
authored
diff: add -s option (briandfoy#940)
* GNU and OpenBSD versions support -s flag which is similar to -q * -q hides the diff and shows a messages when the files are different * -s explicitly tells the user when the files are the same * Improve how -q is documented; it doesn't print anything for identical files * -q and -s are not mutually exclusive * While here, remove manual assignments of $opt_u and $opt_c which are not needed (the subsequent code refers to $Context_Lines and not these variables) %perl diff -qs a.c a.c Files a.c and a.c are identical %perl diff -qs a.c a.s Files a.c and a.s differ
1 parent 2e823de commit 725ee97

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

bin/diff

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use constant EX_SUCCESS => 0;
4444
use constant EX_DIFFERENT => 1;
4545
use constant EX_FAILURE => 2;
4646

47-
use vars qw($opt_C $opt_c $opt_e $opt_f $opt_q $opt_U $opt_u);
47+
use vars qw($opt_C $opt_c $opt_e $opt_f $opt_q $opt_s $opt_U $opt_u);
4848

4949
# GLOBAL VARIABLES ####
5050
# After we've read up to a certain point in each file, the number of items
@@ -58,25 +58,25 @@ my @Ed_Hunks = ();
5858

5959
sub usage {
6060
warn << "ENDUSAGE";
61-
Usage: $Program [-c | -C lines | -e | -f | -q | -u | -U lines] oldfile newfile
61+
Usage: $Program [-c | -C lines | -e | -f | -q | -s | -u | -U lines] oldfile newfile
6262
-c do a context diff with 3 lines of context
6363
-C do a context diff with 'lines' lines of context (implies -c)
6464
-e create a script for the ed editor to change oldfile to newfile
6565
-f like -e but in reverse order
6666
-u do a unified diff with 3 lines of context
6767
-U do a unified diff with 'lines' lines of context (implies -u)
68-
-q report only whether or not the files differ
68+
-q show a message when the files differ, instead of the difference
69+
-s show a message when the files are identical
6970
7071
ENDUSAGE
7172
exit EX_FAILURE;
7273
}
7374

7475
my $Context_Lines = 0; # lines of context to print. 0 for old-style diff
7576
my $Diff_Type = "OLD"; # by default, do standard UNIX diff
76-
getopts('C:cefqU:u') or usage();
77+
getopts('C:cefqsU:u') or usage();
7778
if (defined $opt_C) {
7879
$Context_Lines = checklen($opt_C);
79-
$opt_c = 1;
8080
set_diff_type('CONTEXT');
8181
} elsif ($opt_c) {
8282
$Context_Lines = 3;
@@ -90,7 +90,6 @@ if ($opt_f) {
9090
}
9191
if (defined $opt_U) {
9292
$Context_Lines = checklen($opt_U);
93-
$opt_u = 1;
9493
set_diff_type('UNIFIED');
9594
} elsif ($opt_u) {
9695
$Context_Lines = 3;
@@ -144,12 +143,11 @@ chomp(@f1 = <$fh1>);
144143
close $fh1;
145144
chomp(@f2 = <$fh2>);
146145
close $fh2;
147-
exit(EX_SUCCESS) if (scalar(@f1) == 0 && scalar(@f2) == 0);
146+
identical() if (scalar(@f1) == 0 && scalar(@f2) == 0);
148147

149148
# diff yields lots of pieces, each of which is basically a Block object
150149
my $diffs = Algorithm::Diff::diff(\@f1, \@f2);
151-
exit(EX_SUCCESS) unless @$diffs;
152-
150+
identical() unless @$diffs;
153151
if ($opt_q) {
154152
print "Files $file1 and $file2 differ\n";
155153
exit EX_DIFFERENT;
@@ -194,6 +192,11 @@ if ($Diff_Type eq 'ED') {
194192
exit EX_DIFFERENT;
195193
# END MAIN PROGRAM
196194

195+
sub identical {
196+
print "Files $file1 and $file2 are identical\n" if $opt_s;
197+
exit EX_SUCCESS;
198+
}
199+
197200
sub bag {
198201
my $msg = shift;
199202
warn "$Program: $msg\n";
@@ -720,7 +723,7 @@ diff - compute `intelligent' differences between two files
720723
721724
=head1 SYNOPSIS
722725
723-
diff [-c | -C lines | -e | -f | -q | -u | -U lines] file1 file2
726+
diff [-c | -C lines | -e | -f | -q | -s | -u | -U lines] file1 file2
724727
725728
=head1 DESCRIPTION
726729
@@ -765,7 +768,12 @@ Output a unified diff with NUM lines of context
765768
766769
=item -q
767770
768-
Report only whether or not the files differ
771+
Print a message if the input files differ instead of displaying the difference.
772+
773+
=item -s
774+
775+
Print a message if the input files are identical.
776+
By default no output is produced in this case.
769777
770778
=back
771779

0 commit comments

Comments
 (0)