30
30
# to the "list_available_blobs" response.
31
31
#
32
32
33
+ use 5.008;
34
+ use lib (split (/ :/ , $ENV {GITPERLLIB }));
33
35
use strict;
34
36
use warnings;
35
37
use IO::File;
38
+ use Git::Packet;
36
39
37
40
my $MAX_PACKET_CONTENT_SIZE = 65516;
38
41
my $log_file = shift @ARGV ;
@@ -55,89 +58,30 @@ sub rot13 {
55
58
return $str ;
56
59
}
57
60
58
- sub packet_bin_read {
59
- my $buffer ;
60
- my $bytes_read = read STDIN , $buffer , 4;
61
- if ( $bytes_read == 0 ) {
62
- # EOF - Git stopped talking to us!
63
- print $debug " STOP\n " ;
64
- exit ();
65
- }
66
- elsif ( $bytes_read != 4 ) {
67
- die " invalid packet: '$buffer '" ;
68
- }
69
- my $pkt_size = hex ($buffer );
70
- if ( $pkt_size == 0 ) {
71
- return ( 1, " " );
72
- }
73
- elsif ( $pkt_size > 4 ) {
74
- my $content_size = $pkt_size - 4;
75
- $bytes_read = read STDIN , $buffer , $content_size ;
76
- if ( $bytes_read != $content_size ) {
77
- die " invalid packet ($content_size bytes expected; $bytes_read bytes read)" ;
78
- }
79
- return ( 0, $buffer );
80
- }
81
- else {
82
- die " invalid packet size: $pkt_size " ;
83
- }
84
- }
85
-
86
- sub packet_txt_read {
87
- my ( $res , $buf ) = packet_bin_read();
88
- unless ( $buf eq ' ' or $buf =~ s /\n $// ) {
89
- die " A non-binary line MUST be terminated by an LF." ;
90
- }
91
- return ( $res , $buf );
92
- }
93
-
94
- sub packet_bin_write {
95
- my $buf = shift ;
96
- print STDOUT sprintf ( " %04x" , length ($buf ) + 4 );
97
- print STDOUT $buf ;
98
- STDOUT -> flush();
99
- }
100
-
101
- sub packet_txt_write {
102
- packet_bin_write( $_ [0] . " \n " );
103
- }
104
-
105
- sub packet_flush {
106
- print STDOUT sprintf ( " %04x" , 0 );
107
- STDOUT -> flush();
108
- }
109
-
110
61
print $debug " START\n " ;
111
62
$debug -> flush();
112
63
113
- ( packet_txt_read() eq ( 0, " git-filter-client" ) ) || die " bad initialize" ;
114
- ( packet_txt_read() eq ( 0, " version=2" ) ) || die " bad version" ;
115
- ( packet_bin_read() eq ( 1, " " ) ) || die " bad version end" ;
64
+ packet_initialize(" git-filter" , 2);
116
65
117
- packet_txt_write(" git-filter-server" );
118
- packet_txt_write(" version=2" );
119
- packet_flush();
66
+ my %remote_caps = packet_read_and_check_capabilities(" clean" , " smudge" , " delay" );
67
+ packet_check_and_write_capabilities(\%remote_caps , @capabilities );
120
68
121
- ( packet_txt_read() eq ( 0, " capability=clean" ) ) || die " bad capability" ;
122
- ( packet_txt_read() eq ( 0, " capability=smudge" ) ) || die " bad capability" ;
123
- ( packet_txt_read() eq ( 0, " capability=delay" ) ) || die " bad capability" ;
124
- ( packet_bin_read() eq ( 1, " " ) ) || die " bad capability end" ;
125
-
126
- foreach (@capabilities ) {
127
- packet_txt_write( " capability=" . $_ );
128
- }
129
- packet_flush();
130
69
print $debug " init handshake complete\n " ;
131
70
$debug -> flush();
132
71
133
72
while (1) {
134
- my ( $command ) = packet_txt_read() =~ / ^command=(.+)$ / ;
73
+ my ( $res , $command ) = packet_required_key_val_read(" command" );
74
+ if ( $res == -1 ) {
75
+ print $debug " STOP\n " ;
76
+ exit ();
77
+ }
135
78
print $debug " IN: $command " ;
136
79
$debug -> flush();
137
80
138
81
if ( $command eq " list_available_blobs" ) {
139
82
# Flush
140
- packet_bin_read();
83
+ packet_compare_lists([1, " " ], packet_bin_read()) ||
84
+ die " bad list_available_blobs end" ;
141
85
142
86
foreach my $pathname ( sort keys %DELAY ) {
143
87
if ( $DELAY {$pathname }{" requested" } >= 1 ) {
@@ -161,16 +105,14 @@ sub packet_flush {
161
105
$debug -> flush();
162
106
packet_txt_write(" status=success" );
163
107
packet_flush();
164
- }
165
- else {
166
- my ( $pathname ) = packet_txt_read() =~ / ^pathname=(.+)$ / ;
108
+ } else {
109
+ my ( $res , $pathname ) = packet_required_key_val_read(" pathname" );
110
+ if ( $res == -1 ) {
111
+ die " unexpected EOF while expecting pathname" ;
112
+ }
167
113
print $debug " $pathname " ;
168
114
$debug -> flush();
169
115
170
- if ( $pathname eq " " ) {
171
- die " bad pathname '$pathname '" ;
172
- }
173
-
174
116
# Read until flush
175
117
my ( $done , $buffer ) = packet_txt_read();
176
118
while ( $buffer ne ' ' ) {
@@ -184,6 +126,9 @@ sub packet_flush {
184
126
185
127
( $done , $buffer ) = packet_txt_read();
186
128
}
129
+ if ( $done == -1 ) {
130
+ die " unexpected EOF after pathname '$pathname '" ;
131
+ }
187
132
188
133
my $input = " " ;
189
134
{
@@ -194,24 +139,23 @@ sub packet_flush {
194
139
( $done , $buffer ) = packet_bin_read();
195
140
$input .= $buffer ;
196
141
}
142
+ if ( $done == -1 ) {
143
+ die " unexpected EOF while reading input for '$pathname '" ;
144
+ }
197
145
print $debug " " . length ($input ) . " [OK] -- " ;
198
146
$debug -> flush();
199
147
}
200
148
201
149
my $output ;
202
150
if ( exists $DELAY {$pathname } and exists $DELAY {$pathname }{" output" } ) {
203
151
$output = $DELAY {$pathname }{" output" }
204
- }
205
- elsif ( $pathname eq " error.r" or $pathname eq " abort.r" ) {
152
+ } elsif ( $pathname eq " error.r" or $pathname eq " abort.r" ) {
206
153
$output = " " ;
207
- }
208
- elsif ( $command eq " clean" and grep ( / ^clean$ / , @capabilities ) ) {
154
+ } elsif ( $command eq " clean" and grep ( / ^clean$ / , @capabilities ) ) {
209
155
$output = rot13($input );
210
- }
211
- elsif ( $command eq " smudge" and grep ( / ^smudge$ / , @capabilities ) ) {
156
+ } elsif ( $command eq " smudge" and grep ( / ^smudge$ / , @capabilities ) ) {
212
157
$output = rot13($input );
213
- }
214
- else {
158
+ } else {
215
159
die " bad command '$command '" ;
216
160
}
217
161
@@ -220,25 +164,21 @@ sub packet_flush {
220
164
$debug -> flush();
221
165
packet_txt_write(" status=error" );
222
166
packet_flush();
223
- }
224
- elsif ( $pathname eq " abort.r" ) {
167
+ } elsif ( $pathname eq " abort.r" ) {
225
168
print $debug " [ABORT]\n " ;
226
169
$debug -> flush();
227
170
packet_txt_write(" status=abort" );
228
171
packet_flush();
229
- }
230
- elsif ( $command eq " smudge" and
172
+ } elsif ( $command eq " smudge" and
231
173
exists $DELAY {$pathname } and
232
- $DELAY {$pathname }{" requested" } == 1
233
- ) {
174
+ $DELAY {$pathname }{" requested" } == 1 ) {
234
175
print $debug " [DELAYED]\n " ;
235
176
$debug -> flush();
236
177
packet_txt_write(" status=delayed" );
237
178
packet_flush();
238
179
$DELAY {$pathname }{" requested" } = 2;
239
180
$DELAY {$pathname }{" output" } = $output ;
240
- }
241
- else {
181
+ } else {
242
182
packet_txt_write(" status=success" );
243
183
packet_flush();
244
184
@@ -258,8 +198,7 @@ sub packet_flush {
258
198
print $debug " ." ;
259
199
if ( length ($output ) > $MAX_PACKET_CONTENT_SIZE ) {
260
200
$output = substr ( $output , $MAX_PACKET_CONTENT_SIZE );
261
- }
262
- else {
201
+ } else {
263
202
$output = " " ;
264
203
}
265
204
}
0 commit comments