1package Git::Packet;2use 5.008;3use strict;4use warnings;5BEGIN {6require Exporter;7if ($] < 5.008003) {8*import = \&Exporter::import;9} else {10# Exporter 5.57 which supports this invocation was11# released with perl 5.8.312Exporter->import('import');13}14}1516our @EXPORT = qw(17packet_compare_lists18packet_bin_read19packet_txt_read20packet_key_val_read21packet_bin_write22packet_txt_write23packet_flush24packet_initialize25packet_read_capabilities26packet_read_and_check_capabilities27packet_check_and_write_capabilities28);29our @EXPORT_OK = @EXPORT;3031sub packet_compare_lists {32my ($expect, @result) = @_;33my $ix;34if (scalar @$expect != scalar @result) {35return undef;36}37for ($ix = 0; $ix < $#result; $ix++) {38if ($expect->[$ix] ne $result[$ix]) {39return undef;40}41}42return 1;43}4445sub packet_bin_read {46my $buffer;47my $bytes_read = read STDIN, $buffer, 4;48if ( $bytes_read == 0 ) {49# EOF - Git stopped talking to us!50return ( -1, "" );51} elsif ( $bytes_read != 4 ) {52die "invalid packet: '$buffer'";53}54my $pkt_size = hex($buffer);55if ( $pkt_size == 0 ) {56return ( 1, "" );57} elsif ( $pkt_size > 4 ) {58my $content_size = $pkt_size - 4;59$bytes_read = read STDIN, $buffer, $content_size;60if ( $bytes_read != $content_size ) {61die "invalid packet ($content_size bytes expected; $bytes_read bytes read)";62}63return ( 0, $buffer );64} else {65die "invalid packet size: $pkt_size";66}67}6869sub remove_final_lf_or_die {70my $buf = shift;71unless ( $buf =~ s/\n$// ) {72die "A non-binary line MUST be terminated by an LF.\n"73. "Received: '$buf'";74}75return $buf;76}7778sub packet_txt_read {79my ( $res, $buf ) = packet_bin_read();80unless ( $res == -1 or $buf eq '' ) {81$buf = remove_final_lf_or_die($buf);82}83return ( $res, $buf );84}8586# Read a text packet, expecting that it is in the form "key=value" for87# the given $key. An EOF does not trigger any error and is reported88# 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 part90# is empty.91sub packet_key_val_read {92my ( $key ) = @_;93my ( $res, $buf ) = packet_txt_read();94unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {95die "bad $key: '$buf'";96}97return ( $res, $buf );98}99100sub packet_bin_write {101my $buf = shift;102print STDOUT sprintf( "%04x", length($buf) + 4 );103print STDOUT $buf;104STDOUT->flush();105}106107sub packet_txt_write {108packet_bin_write( $_[0] . "\n" );109}110111sub packet_flush {112print STDOUT sprintf( "%04x", 0 );113STDOUT->flush();114}115116sub packet_initialize {117my ($name, $version) = @_;118119packet_compare_lists([0, $name . "-client"], packet_txt_read()) ||120die "bad initialize";121packet_compare_lists([0, "version=" . $version], packet_txt_read()) ||122die "bad version";123packet_compare_lists([1, ""], packet_bin_read()) ||124die "bad version end";125126packet_txt_write( $name . "-server" );127packet_txt_write( "version=" . $version );128packet_flush();129}130131sub packet_read_capabilities {132my @cap;133while (1) {134my ( $res, $buf ) = packet_bin_read();135if ( $res == -1 ) {136die "unexpected EOF when reading capabilities";137}138return ( $res, @cap ) if ( $res != 0 );139$buf = remove_final_lf_or_die($buf);140unless ( $buf =~ s/capability=// ) {141die "bad capability buf: '$buf'";142}143push @cap, $buf;144}145}146147# Read remote capabilities and check them against capabilities we require148sub packet_read_and_check_capabilities {149my @required_caps = @_;150my ($res, @remote_caps) = packet_read_capabilities();151my %remote_caps = map { $_ => 1 } @remote_caps;152foreach (@required_caps) {153unless (exists($remote_caps{$_})) {154die "required '$_' capability not available from remote" ;155}156}157return %remote_caps;158}159160# Check our capabilities we want to advertise against the remote ones161# and then advertise our capabilities162sub packet_check_and_write_capabilities {163my ($remote_caps, @our_caps) = @_;164foreach (@our_caps) {165unless (exists($remote_caps->{$_})) {166die "our capability '$_' is not available from remote"167}168packet_txt_write( "capability=" . $_ );169}170packet_flush();171}1721731;