Skip to content

Commit b16488e

Browse files
committed
Merge branch 'cc/git-packet-pm'
Code clean-up. * cc/git-packet-pm: Git/Packet.pm: use 'if' instead of 'unless' Git/Packet: clarify that packet_required_key_val_read allows EOF
2 parents 00bcc35 + 4a54370 commit b16488e

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

perl/Git/Packet.pm

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ our @EXPORT = qw(
1717
packet_compare_lists
1818
packet_bin_read
1919
packet_txt_read
20-
packet_required_key_val_read
20+
packet_key_val_read
2121
packet_bin_write
2222
packet_txt_write
2323
packet_flush
@@ -68,28 +68,33 @@ sub packet_bin_read {
6868

6969
sub remove_final_lf_or_die {
7070
my $buf = shift;
71-
unless ( $buf =~ s/\n$// ) {
72-
die "A non-binary line MUST be terminated by an LF.\n"
73-
. "Received: '$buf'";
71+
if ( $buf =~ s/\n$// ) {
72+
return $buf;
7473
}
75-
return $buf;
74+
die "A non-binary line MUST be terminated by an LF.\n"
75+
. "Received: '$buf'";
7676
}
7777

7878
sub packet_txt_read {
7979
my ( $res, $buf ) = packet_bin_read();
80-
unless ( $res == -1 or $buf eq '' ) {
80+
if ( $res != -1 and $buf ne '' ) {
8181
$buf = remove_final_lf_or_die($buf);
8282
}
8383
return ( $res, $buf );
8484
}
8585

86-
sub packet_required_key_val_read {
86+
# Read a text packet, expecting that it is in the form "key=value" for
87+
# the given $key. An EOF does not trigger any error and is reported
88+
# back to the caller (like packet_txt_read() does). Die if the "key"
89+
# part of "key=value" does not match the given $key, or the value part
90+
# is empty.
91+
sub packet_key_val_read {
8792
my ( $key ) = @_;
8893
my ( $res, $buf ) = packet_txt_read();
89-
unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
90-
die "bad $key: '$buf'";
94+
if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
95+
return ( $res, $buf );
9196
}
92-
return ( $res, $buf );
97+
die "bad $key: '$buf'";
9398
}
9499

95100
sub packet_bin_write {

t/t0021/rot13-filter.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ sub rot13 {
7070
$debug->flush();
7171

7272
while (1) {
73-
my ( $res, $command ) = packet_required_key_val_read("command");
73+
my ( $res, $command ) = packet_key_val_read("command");
7474
if ( $res == -1 ) {
7575
print $debug "STOP\n";
7676
exit();
@@ -106,7 +106,7 @@ sub rot13 {
106106
packet_txt_write("status=success");
107107
packet_flush();
108108
} else {
109-
my ( $res, $pathname ) = packet_required_key_val_read("pathname");
109+
my ( $res, $pathname ) = packet_key_val_read("pathname");
110110
if ( $res == -1 ) {
111111
die "unexpected EOF while expecting pathname";
112112
}

0 commit comments

Comments
 (0)