8 *import = \&Exporter::import;
10 # Exporter 5.57 which supports this invocation was
11 # released with perl 5.8.3
12 Exporter->import('import');
20 packet_required_key_val_read
25 packet_read_capabilities
26 packet_read_and_check_capabilities
27 packet_check_and_write_capabilities
29 our @EXPORT_OK = @EXPORT;
31 sub packet_compare_lists {
32 my ($expect, @result) = @_;
34 if (scalar @$expect != scalar @result) {
37 for ($ix = 0; $ix < $#result; $ix++) {
38 if ($expect->[$ix] ne $result[$ix]) {
47 my $bytes_read = read STDIN, $buffer, 4;
48 if ( $bytes_read == 0 ) {
49 # EOF - Git stopped talking to us!
51 } elsif ( $bytes_read != 4 ) {
52 die "invalid packet: '$buffer'";
54 my $pkt_size = hex($buffer);
55 if ( $pkt_size == 0 ) {
57 } elsif ( $pkt_size > 4 ) {
58 my $content_size = $pkt_size - 4;
59 $bytes_read = read STDIN, $buffer, $content_size;
60 if ( $bytes_read != $content_size ) {
61 die "invalid packet ($content_size bytes expected; $bytes_read bytes read)";
63 return ( 0, $buffer );
65 die "invalid packet size: $pkt_size";
69 sub remove_final_lf_or_die {
71 unless ( $buf =~ s/\n$// ) {
72 die "A non-binary line MUST be terminated by an LF.\n"
79 my ( $res, $buf ) = packet_bin_read();
80 unless ( $res == -1 or $buf eq '' ) {
81 $buf = remove_final_lf_or_die($buf);
83 return ( $res, $buf );
86 sub packet_required_key_val_read {
88 my ( $res, $buf ) = packet_txt_read();
89 unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
90 die "bad $key: '$buf'";
92 return ( $res, $buf );
95 sub packet_bin_write {
97 print STDOUT sprintf( "%04x", length($buf) + 4 );
102 sub packet_txt_write {
103 packet_bin_write( $_[0] . "\n" );
107 print STDOUT sprintf( "%04x", 0 );
111 sub packet_initialize {
112 my ($name, $version) = @_;
114 packet_compare_lists([0, $name . "-client"], packet_txt_read()) ||
115 die "bad initialize";
116 packet_compare_lists([0, "version=" . $version], packet_txt_read()) ||
118 packet_compare_lists([1, ""], packet_bin_read()) ||
119 die "bad version end";
121 packet_txt_write( $name . "-server" );
122 packet_txt_write( "version=" . $version );
126 sub packet_read_capabilities {
129 my ( $res, $buf ) = packet_bin_read();
131 die "unexpected EOF when reading capabilities";
133 return ( $res, @cap ) if ( $res != 0 );
134 $buf = remove_final_lf_or_die($buf);
135 unless ( $buf =~ s/capability=// ) {
136 die "bad capability buf: '$buf'";
142 # Read remote capabilities and check them against capabilities we require
143 sub packet_read_and_check_capabilities {
144 my @required_caps = @_;
145 my ($res, @remote_caps) = packet_read_capabilities();
146 my %remote_caps = map { $_ => 1 } @remote_caps;
147 foreach (@required_caps) {
148 unless (exists($remote_caps{$_})) {
149 die "required '$_' capability not available from remote" ;
155 # Check our capabilities we want to advertise against the remote ones
156 # and then advertise our capabilities
157 sub packet_check_and_write_capabilities {
158 my ($remote_caps, @our_caps) = @_;
159 foreach (@our_caps) {
160 unless (exists($remote_caps->{$_})) {
161 die "our capability '$_' is not available from remote"
163 packet_txt_write( "capability=" . $_ );