2 # Example implementation for the Git filter protocol version 2
3 # See Documentation/gitattributes.txt, section "Filter Protocol"
5 # The first argument defines a debug log file that the script write to.
6 # All remaining arguments define a list of supported protocol
7 # capabilities ("clean", "smudge", etc).
9 # This implementation supports special test cases:
10 # (1) If data with the pathname "clean-write-fail.r" is processed with
11 # a "clean" operation then the write operation will die.
12 # (2) If data with the pathname "smudge-write-fail.r" is processed with
13 # a "smudge" operation then the write operation will die.
14 # (3) If data with the pathname "error.r" is processed with any
15 # operation then the filter signals that it cannot or does not want
16 # to process the file.
17 # (4) If data with the pathname "abort.r" is processed with any
18 # operation then the filter signals that it cannot or does not want
19 # to process the file and any file after that is processed with the
21 # (5) If data with a pathname that is a key in the DELAY hash is
22 # requested (e.g. "test-delay10.a") then the filter responds with
23 # a "delay" status and sets the "requested" field in the DELAY hash.
24 # The filter will signal the availability of this object after
25 # "count" (field in DELAY hash) "list_available_blobs" commands.
26 # (6) If data with the pathname "missing-delay.a" is processed that the
27 # filter will drop the path from the "list_available_blobs" response.
28 # (7) If data with the pathname "invalid-delay.a" is processed that the
29 # filter will add the path "unfiltered" which was not delayed before
30 # to the "list_available_blobs" response.
37 my $MAX_PACKET_CONTENT_SIZE = 65516;
38 my $log_file = shift @ARGV;
39 my @capabilities = @ARGV;
41 open my $debug, ">>", $log_file or die "cannot open log file: $!";
44 'test-delay10.a' => { "requested" => 0, "count" => 1 },
45 'test-delay11.a' => { "requested" => 0, "count" => 1 },
46 'test-delay20.a' => { "requested" => 0, "count" => 2 },
47 'test-delay10.b' => { "requested" => 0, "count" => 1 },
48 'missing-delay.a' => { "requested" => 0, "count" => 1 },
49 'invalid-delay.a' => { "requested" => 0, "count" => 1 },
54 $str =~ y/A-Za-z/N-ZA-Mn-za-m/;
58 sub packet_compare_lists {
59 my ($expect, @result) = @_;
61 if (scalar @$expect != scalar @result) {
64 for ($ix = 0; $ix < $#result; $ix++) {
65 if ($expect->[$ix] ne $result[$ix]) {
74 my $bytes_read = read STDIN, $buffer, 4;
75 if ( $bytes_read == 0 ) {
76 # EOF - Git stopped talking to us!
78 } elsif ( $bytes_read != 4 ) {
79 die "invalid packet: '$buffer'";
81 my $pkt_size = hex($buffer);
82 if ( $pkt_size == 0 ) {
84 } elsif ( $pkt_size > 4 ) {
85 my $content_size = $pkt_size - 4;
86 $bytes_read = read STDIN, $buffer, $content_size;
87 if ( $bytes_read != $content_size ) {
88 die "invalid packet ($content_size bytes expected; $bytes_read bytes read)";
90 return ( 0, $buffer );
92 die "invalid packet size: $pkt_size";
97 my ( $res, $buf ) = packet_bin_read();
98 unless ( $res == -1 or $buf eq '' or $buf =~ s/\n$// ) {
99 die "A non-binary line MUST be terminated by an LF.\n"
100 . "Received: '$buf'";
102 return ( $res, $buf );
105 sub packet_required_key_val_read {
107 my ( $res, $buf ) = packet_txt_read();
108 unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
109 die "bad $key: '$buf'";
111 return ( $res, $buf );
114 sub packet_bin_write {
116 print STDOUT sprintf( "%04x", length($buf) + 4 );
121 sub packet_txt_write {
122 packet_bin_write( $_[0] . "\n" );
126 print STDOUT sprintf( "%04x", 0 );
130 print $debug "START\n";
133 packet_compare_lists([0, "git-filter-client"], packet_txt_read()) ||
134 die "bad initialize";
135 packet_compare_lists([0, "version=2"], packet_txt_read()) ||
137 packet_compare_lists([1, ""], packet_bin_read()) ||
138 die "bad version end";
140 packet_txt_write("git-filter-server");
141 packet_txt_write("version=2");
144 packet_compare_lists([0, "capability=clean"], packet_txt_read()) ||
145 die "bad capability";
146 packet_compare_lists([0, "capability=smudge"], packet_txt_read()) ||
147 die "bad capability";
148 packet_compare_lists([0, "capability=delay"], packet_txt_read()) ||
149 die "bad capability";
150 packet_compare_lists([1, ""], packet_bin_read()) ||
151 die "bad capability end";
153 foreach (@capabilities) {
154 packet_txt_write( "capability=" . $_ );
157 print $debug "init handshake complete\n";
161 my ( $res, $command ) = packet_required_key_val_read("command");
163 print $debug "STOP\n";
166 print $debug "IN: $command";
169 if ( $command eq "list_available_blobs" ) {
171 packet_compare_lists([1, ""], packet_bin_read()) ||
172 die "bad list_available_blobs end";
174 foreach my $pathname ( sort keys %DELAY ) {
175 if ( $DELAY{$pathname}{"requested"} >= 1 ) {
176 $DELAY{$pathname}{"count"} = $DELAY{$pathname}{"count"} - 1;
177 if ( $pathname eq "invalid-delay.a" ) {
178 # Send Git a pathname that was not delayed earlier
179 packet_txt_write("pathname=unfiltered");
181 if ( $pathname eq "missing-delay.a" ) {
182 # Do not signal Git that this file is available
183 } elsif ( $DELAY{$pathname}{"count"} == 0 ) {
184 print $debug " $pathname";
185 packet_txt_write("pathname=$pathname");
192 print $debug " [OK]\n";
194 packet_txt_write("status=success");
197 my ( $res, $pathname ) = packet_required_key_val_read("pathname");
199 die "unexpected EOF while expecting pathname";
201 print $debug " $pathname";
205 my ( $done, $buffer ) = packet_txt_read();
206 while ( $buffer ne '' ) {
207 if ( $buffer eq "can-delay=1" ) {
208 if ( exists $DELAY{$pathname} and $DELAY{$pathname}{"requested"} == 0 ) {
209 $DELAY{$pathname}{"requested"} = 1;
212 die "Unknown message '$buffer'";
215 ( $done, $buffer ) = packet_txt_read();
218 die "unexpected EOF after pathname '$pathname'";
227 ( $done, $buffer ) = packet_bin_read();
231 die "unexpected EOF while reading input for '$pathname'";
233 print $debug " " . length($input) . " [OK] -- ";
238 if ( exists $DELAY{$pathname} and exists $DELAY{$pathname}{"output"} ) {
239 $output = $DELAY{$pathname}{"output"}
240 } elsif ( $pathname eq "error.r" or $pathname eq "abort.r" ) {
242 } elsif ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) {
243 $output = rot13($input);
244 } elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) {
245 $output = rot13($input);
247 die "bad command '$command'";
250 if ( $pathname eq "error.r" ) {
251 print $debug "[ERROR]\n";
253 packet_txt_write("status=error");
255 } elsif ( $pathname eq "abort.r" ) {
256 print $debug "[ABORT]\n";
258 packet_txt_write("status=abort");
260 } elsif ( $command eq "smudge" and
261 exists $DELAY{$pathname} and
262 $DELAY{$pathname}{"requested"} == 1 ) {
263 print $debug "[DELAYED]\n";
265 packet_txt_write("status=delayed");
267 $DELAY{$pathname}{"requested"} = 2;
268 $DELAY{$pathname}{"output"} = $output;
270 packet_txt_write("status=success");
273 if ( $pathname eq "${command}-write-fail.r" ) {
274 print $debug "[WRITE FAIL]\n";
276 die "${command} write error";
279 print $debug "OUT: " . length($output) . " ";
282 while ( length($output) > 0 ) {
283 my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );
284 packet_bin_write($packet);
285 # dots represent the number of packets
287 if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {
288 $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );
294 print $debug " [OK]\n";