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
This commit is contained in:
Junio C Hamano 2017-12-06 09:23:37 -08:00
commit b16488eb3c
2 changed files with 17 additions and 12 deletions

View file

@ -17,7 +17,7 @@ BEGIN
packet_compare_lists
packet_bin_read
packet_txt_read
packet_required_key_val_read
packet_key_val_read
packet_bin_write
packet_txt_write
packet_flush
@ -68,28 +68,33 @@ sub packet_bin_read {
sub remove_final_lf_or_die {
my $buf = shift;
unless ( $buf =~ s/\n$// ) {
die "A non-binary line MUST be terminated by an LF.\n"
. "Received: '$buf'";
if ( $buf =~ s/\n$// ) {
return $buf;
}
return $buf;
die "A non-binary line MUST be terminated by an LF.\n"
. "Received: '$buf'";
}
sub packet_txt_read {
my ( $res, $buf ) = packet_bin_read();
unless ( $res == -1 or $buf eq '' ) {
if ( $res != -1 and $buf ne '' ) {
$buf = remove_final_lf_or_die($buf);
}
return ( $res, $buf );
}
sub packet_required_key_val_read {
# Read a text packet, expecting that it is in the form "key=value" for
# the given $key. An EOF does not trigger any error and is reported
# back to the caller (like packet_txt_read() does). Die if the "key"
# part of "key=value" does not match the given $key, or the value part
# is empty.
sub packet_key_val_read {
my ( $key ) = @_;
my ( $res, $buf ) = packet_txt_read();
unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
die "bad $key: '$buf'";
if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
return ( $res, $buf );
}
return ( $res, $buf );
die "bad $key: '$buf'";
}
sub packet_bin_write {

View file

@ -70,7 +70,7 @@ sub rot13 {
$debug->flush();
while (1) {
my ( $res, $command ) = packet_required_key_val_read("command");
my ( $res, $command ) = packet_key_val_read("command");
if ( $res == -1 ) {
print $debug "STOP\n";
exit();
@ -106,7 +106,7 @@ sub rot13 {
packet_txt_write("status=success");
packet_flush();
} else {
my ( $res, $pathname ) = packet_required_key_val_read("pathname");
my ( $res, $pathname ) = packet_key_val_read("pathname");
if ( $res == -1 ) {
die "unexpected EOF while expecting pathname";
}