4 use warnings
$ENV{GIT_PERL_FATAL_WARNINGS
} ?
qw(FATAL all) : ();
8 *import
= \
&Exporter
::import
;
10 # Exporter 5.57 which supports this invocation was
11 # released with perl 5.8.3
12 Exporter
->import('import');
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 if ( $buf =~ s/\n$// ) {
74 die "A non-binary line MUST be terminated by an LF.\n"
79 my ( $res, $buf ) = packet_bin_read
();
80 if ( $res != -1 and $buf ne '' ) {
81 $buf = remove_final_lf_or_die
($buf);
83 return ( $res, $buf );
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
91 sub packet_key_val_read
{
93 my ( $res, $buf ) = packet_txt_read
();
94 if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
95 return ( $res, $buf );
97 die "bad $key: '$buf'";
100 sub packet_bin_write
{
102 print STDOUT
sprintf( "%04x", length($buf) + 4 );
107 sub packet_txt_write
{
108 packet_bin_write
( $_[0] . "\n" );
112 print STDOUT
sprintf( "%04x", 0 );
116 sub packet_initialize
{
117 my ($name, $version) = @_;
119 packet_compare_lists
([0, $name . "-client"], packet_txt_read
()) ||
120 die "bad initialize";
121 packet_compare_lists
([0, "version=" . $version], packet_txt_read
()) ||
123 packet_compare_lists
([1, ""], packet_bin_read
()) ||
124 die "bad version end";
126 packet_txt_write
( $name . "-server" );
127 packet_txt_write
( "version=" . $version );
131 sub packet_read_capabilities
{
134 my ( $res, $buf ) = packet_bin_read
();
136 die "unexpected EOF when reading capabilities";
138 return ( $res, @cap ) if ( $res != 0 );
139 $buf = remove_final_lf_or_die
($buf);
140 unless ( $buf =~ s/capability=// ) {
141 die "bad capability buf: '$buf'";
147 # Read remote capabilities and check them against capabilities we require
148 sub packet_read_and_check_capabilities
{
149 my @required_caps = @_;
150 my ($res, @remote_caps) = packet_read_capabilities
();
151 my %remote_caps = map { $_ => 1 } @remote_caps;
152 foreach (@required_caps) {
153 unless (exists($remote_caps{$_})) {
154 die "required '$_' capability not available from remote" ;
160 # Check our capabilities we want to advertise against the remote ones
161 # and then advertise our capabilities
162 sub packet_check_and_write_capabilities
{
163 my ($remote_caps, @our_caps) = @_;
164 foreach (@our_caps) {
165 unless (exists($remote_caps->{$_})) {
166 die "our capability '$_' is not available from remote"
168 packet_txt_write
( "capability=" . $_ );