d47b7f5666a590cc09c26f71d3965f5c838f402f
   1#
   2# Example implementation for the Git filter protocol version 2
   3# See Documentation/gitattributes.txt, section "Filter Protocol"
   4#
   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).
   8#
   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
  20#     same command.
  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.
  31#
  32
  33use strict;
  34use warnings;
  35use IO::File;
  36
  37my $MAX_PACKET_CONTENT_SIZE = 65516;
  38my $log_file                = shift @ARGV;
  39my @capabilities            = @ARGV;
  40
  41open my $debug, ">>", $log_file or die "cannot open log file: $!";
  42
  43my %DELAY = (
  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 },
  50);
  51
  52sub rot13 {
  53        my $str = shift;
  54        $str =~ y/A-Za-z/N-ZA-Mn-za-m/;
  55        return $str;
  56}
  57
  58sub packet_compare_lists {
  59        my ($expect, @result) = @_;
  60        my $ix;
  61        if (scalar @$expect != scalar @result) {
  62                return undef;
  63        }
  64        for ($ix = 0; $ix < $#result; $ix++) {
  65                if ($expect->[$ix] ne $result[$ix]) {
  66                        return undef;
  67                }
  68        }
  69        return 1;
  70}
  71
  72sub packet_bin_read {
  73        my $buffer;
  74        my $bytes_read = read STDIN, $buffer, 4;
  75        if ( $bytes_read == 0 ) {
  76                # EOF - Git stopped talking to us!
  77                return ( -1, "" );
  78        } elsif ( $bytes_read != 4 ) {
  79                die "invalid packet: '$buffer'";
  80        }
  81        my $pkt_size = hex($buffer);
  82        if ( $pkt_size == 0 ) {
  83                return ( 1, "" );
  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)";
  89                }
  90                return ( 0, $buffer );
  91        } else {
  92                die "invalid packet size: $pkt_size";
  93        }
  94}
  95
  96sub remove_final_lf_or_die {
  97        my $buf = shift;
  98        unless ( $buf =~ s/\n$// ) {
  99                die "A non-binary line MUST be terminated by an LF.\n"
 100                    . "Received: '$buf'";
 101        }
 102        return $buf;
 103}
 104
 105sub packet_txt_read {
 106        my ( $res, $buf ) = packet_bin_read();
 107        unless ( $res == -1 or $buf eq '' ) {
 108                $buf = remove_final_lf_or_die($buf);
 109        }
 110        return ( $res, $buf );
 111}
 112
 113sub packet_required_key_val_read {
 114        my ( $key ) = @_;
 115        my ( $res, $buf ) = packet_txt_read();
 116        unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
 117                die "bad $key: '$buf'";
 118        }
 119        return ( $res, $buf );
 120}
 121
 122sub packet_bin_write {
 123        my $buf = shift;
 124        print STDOUT sprintf( "%04x", length($buf) + 4 );
 125        print STDOUT $buf;
 126        STDOUT->flush();
 127}
 128
 129sub packet_txt_write {
 130        packet_bin_write( $_[0] . "\n" );
 131}
 132
 133sub packet_flush {
 134        print STDOUT sprintf( "%04x", 0 );
 135        STDOUT->flush();
 136}
 137
 138sub packet_initialize {
 139        my ($name, $version) = @_;
 140
 141        packet_compare_lists([0, $name . "-client"], packet_txt_read()) ||
 142                die "bad initialize";
 143        packet_compare_lists([0, "version=" . $version], packet_txt_read()) ||
 144                die "bad version";
 145        packet_compare_lists([1, ""], packet_bin_read()) ||
 146                die "bad version end";
 147
 148        packet_txt_write( $name . "-server" );
 149        packet_txt_write( "version=" . $version );
 150        packet_flush();
 151}
 152
 153print $debug "START\n";
 154$debug->flush();
 155
 156packet_initialize("git-filter", 2);
 157
 158packet_compare_lists([0, "capability=clean"], packet_txt_read()) ||
 159        die "bad capability";
 160packet_compare_lists([0, "capability=smudge"], packet_txt_read()) ||
 161        die "bad capability";
 162packet_compare_lists([0, "capability=delay"], packet_txt_read()) ||
 163        die "bad capability";
 164packet_compare_lists([1, ""], packet_bin_read()) ||
 165        die "bad capability end";
 166
 167foreach (@capabilities) {
 168        packet_txt_write( "capability=" . $_ );
 169}
 170packet_flush();
 171print $debug "init handshake complete\n";
 172$debug->flush();
 173
 174while (1) {
 175        my ( $res, $command ) = packet_required_key_val_read("command");
 176        if ( $res == -1 ) {
 177                print $debug "STOP\n";
 178                exit();
 179        }
 180        print $debug "IN: $command";
 181        $debug->flush();
 182
 183        if ( $command eq "list_available_blobs" ) {
 184                # Flush
 185                packet_compare_lists([1, ""], packet_bin_read()) ||
 186                        die "bad list_available_blobs end";
 187
 188                foreach my $pathname ( sort keys %DELAY ) {
 189                        if ( $DELAY{$pathname}{"requested"} >= 1 ) {
 190                                $DELAY{$pathname}{"count"} = $DELAY{$pathname}{"count"} - 1;
 191                                if ( $pathname eq "invalid-delay.a" ) {
 192                                        # Send Git a pathname that was not delayed earlier
 193                                        packet_txt_write("pathname=unfiltered");
 194                                }
 195                                if ( $pathname eq "missing-delay.a" ) {
 196                                        # Do not signal Git that this file is available
 197                                } elsif ( $DELAY{$pathname}{"count"} == 0 ) {
 198                                        print $debug " $pathname";
 199                                        packet_txt_write("pathname=$pathname");
 200                                }
 201                        }
 202                }
 203
 204                packet_flush();
 205
 206                print $debug " [OK]\n";
 207                $debug->flush();
 208                packet_txt_write("status=success");
 209                packet_flush();
 210        } else {
 211                my ( $res, $pathname ) = packet_required_key_val_read("pathname");
 212                if ( $res == -1 ) {
 213                        die "unexpected EOF while expecting pathname";
 214                }
 215                print $debug " $pathname";
 216                $debug->flush();
 217
 218                # Read until flush
 219                my ( $done, $buffer ) = packet_txt_read();
 220                while ( $buffer ne '' ) {
 221                        if ( $buffer eq "can-delay=1" ) {
 222                                if ( exists $DELAY{$pathname} and $DELAY{$pathname}{"requested"} == 0 ) {
 223                                        $DELAY{$pathname}{"requested"} = 1;
 224                                }
 225                        } else {
 226                                die "Unknown message '$buffer'";
 227                        }
 228
 229                        ( $done, $buffer ) = packet_txt_read();
 230                }
 231                if ( $done == -1 ) {
 232                        die "unexpected EOF after pathname '$pathname'";
 233                }
 234
 235                my $input = "";
 236                {
 237                        binmode(STDIN);
 238                        my $buffer;
 239                        my $done = 0;
 240                        while ( !$done ) {
 241                                ( $done, $buffer ) = packet_bin_read();
 242                                $input .= $buffer;
 243                        }
 244                        if ( $done == -1 ) {
 245                                die "unexpected EOF while reading input for '$pathname'";
 246                        }                       
 247                        print $debug " " . length($input) . " [OK] -- ";
 248                        $debug->flush();
 249                }
 250
 251                my $output;
 252                if ( exists $DELAY{$pathname} and exists $DELAY{$pathname}{"output"} ) {
 253                        $output = $DELAY{$pathname}{"output"}
 254                } elsif ( $pathname eq "error.r" or $pathname eq "abort.r" ) {
 255                        $output = "";
 256                } elsif ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) {
 257                        $output = rot13($input);
 258                } elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) {
 259                        $output = rot13($input);
 260                } else {
 261                        die "bad command '$command'";
 262                }
 263
 264                if ( $pathname eq "error.r" ) {
 265                        print $debug "[ERROR]\n";
 266                        $debug->flush();
 267                        packet_txt_write("status=error");
 268                        packet_flush();
 269                } elsif ( $pathname eq "abort.r" ) {
 270                        print $debug "[ABORT]\n";
 271                        $debug->flush();
 272                        packet_txt_write("status=abort");
 273                        packet_flush();
 274                } elsif ( $command eq "smudge" and
 275                        exists $DELAY{$pathname} and
 276                        $DELAY{$pathname}{"requested"} == 1 ) {
 277                        print $debug "[DELAYED]\n";
 278                        $debug->flush();
 279                        packet_txt_write("status=delayed");
 280                        packet_flush();
 281                        $DELAY{$pathname}{"requested"} = 2;
 282                        $DELAY{$pathname}{"output"} = $output;
 283                } else {
 284                        packet_txt_write("status=success");
 285                        packet_flush();
 286
 287                        if ( $pathname eq "${command}-write-fail.r" ) {
 288                                print $debug "[WRITE FAIL]\n";
 289                                $debug->flush();
 290                                die "${command} write error";
 291                        }
 292
 293                        print $debug "OUT: " . length($output) . " ";
 294                        $debug->flush();
 295
 296                        while ( length($output) > 0 ) {
 297                                my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );
 298                                packet_bin_write($packet);
 299                                # dots represent the number of packets
 300                                print $debug ".";
 301                                if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {
 302                                        $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );
 303                                } else {
 304                                        $output = "";
 305                                }
 306                        }
 307                        packet_flush();
 308                        print $debug " [OK]\n";
 309                        $debug->flush();
 310                        packet_flush();
 311                }
 312        }
 313}