3 # Example implementation for the Git filter protocol version 2
4 # See Documentation/gitattributes.txt, section "Filter Protocol"
6 # Please note, this pass-thru filter is a minimal skeleton. No proper
7 # error handling was implemented.
13 my $MAX_PACKET_CONTENT_SIZE = 65516;
17 my $bytes_read = read STDIN, $buffer, 4;
18 if ( $bytes_read == 0 ) {
20 # EOF - Git stopped talking to us!
23 elsif ( $bytes_read != 4 ) {
24 die "invalid packet: '$buffer'";
26 my $pkt_size = hex($buffer);
27 if ( $pkt_size == 0 ) {
30 elsif ( $pkt_size > 4 ) {
31 my $content_size = $pkt_size - 4;
32 $bytes_read = read STDIN, $buffer, $content_size;
33 if ( $bytes_read != $content_size ) {
34 die "invalid packet ($content_size bytes expected; $bytes_read bytes read)";
36 return ( 0, $buffer );
39 die "invalid packet size: $pkt_size";
44 my ( $res, $buf ) = packet_bin_read();
45 unless ( $buf =~ s/\n$// ) {
46 die "A non-binary line MUST be terminated by an LF.";
48 return ( $res, $buf );
51 sub packet_bin_write {
53 print STDOUT sprintf( "%04x", length($buf) + 4 );
58 sub packet_txt_write {
59 packet_bin_write( $_[0] . "\n" );
63 print STDOUT sprintf( "%04x", 0 );
67 ( packet_txt_read() eq ( 0, "git-filter-client" ) ) || die "bad initialize";
68 ( packet_txt_read() eq ( 0, "version=2" ) ) || die "bad version";
69 ( packet_bin_read() eq ( 1, "" ) ) || die "bad version end";
71 packet_txt_write("git-filter-server");
72 packet_txt_write("version=2");
75 ( packet_txt_read() eq ( 0, "capability=clean" ) ) || die "bad capability";
76 ( packet_txt_read() eq ( 0, "capability=smudge" ) ) || die "bad capability";
77 ( packet_bin_read() eq ( 1, "" ) ) || die "bad capability end";
79 packet_txt_write("capability=clean");
80 packet_txt_write("capability=smudge");
84 my ($command) = packet_txt_read() =~ /^command=(.+)$/;
85 my ($pathname) = packet_txt_read() =~ /^pathname=(.+)$/;
87 if ( $pathname eq "" ) {
88 die "bad pathname '$pathname'";
99 ( $done, $buffer ) = packet_bin_read();
105 if ( $command eq "clean" ) {
106 ### Perform clean here ###
109 elsif ( $command eq "smudge" ) {
110 ### Perform smudge here ###
114 die "bad command '$command'";
117 packet_txt_write("status=success");
119 while ( length($output) > 0 ) {
120 my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );
121 packet_bin_write($packet);
122 if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {
123 $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );
129 packet_flush(); # flush content!
130 packet_flush(); # empty list, keep "status=success" unchanged!