Skip to content

Commit f11c8ce

Browse files
chriscoolgitster
authored andcommitted
t0021/rot13-filter: add capability functions
These function help read and write capabilities. To make them more generic and make it easy to reuse them, the following changes are made: - we don't require capabilities to come in a fixed order, - we allow duplicates, - we check that the remote supports the capabilities we advertise, - we don't check if the remote declares any capability we don't know about. The reason behind the last change is that the protocol should work using only the capabilities that both ends support, and it should not stop working if one end starts to advertise a new capability. Despite those changes, we can still require a set of capabilities, and die if one of them is not supported. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4a9ef1b commit f11c8ce

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

t/t0021/rot13-filter.pl

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,56 @@ sub packet_initialize {
150150
packet_flush();
151151
}
152152

153+
sub packet_read_capabilities {
154+
my @cap;
155+
while (1) {
156+
my ( $res, $buf ) = packet_bin_read();
157+
if ( $res == -1 ) {
158+
die "unexpected EOF when reading capabilities";
159+
}
160+
return ( $res, @cap ) if ( $res != 0 );
161+
$buf = remove_final_lf_or_die($buf);
162+
unless ( $buf =~ s/capability=// ) {
163+
die "bad capability buf: '$buf'";
164+
}
165+
push @cap, $buf;
166+
}
167+
}
168+
169+
# Read remote capabilities and check them against capabilities we require
170+
sub packet_read_and_check_capabilities {
171+
my @required_caps = @_;
172+
my ($res, @remote_caps) = packet_read_capabilities();
173+
my %remote_caps = map { $_ => 1 } @remote_caps;
174+
foreach (@required_caps) {
175+
unless (exists($remote_caps{$_})) {
176+
die "required '$_' capability not available from remote" ;
177+
}
178+
}
179+
return %remote_caps;
180+
}
181+
182+
# Check our capabilities we want to advertise against the remote ones
183+
# and then advertise our capabilities
184+
sub packet_check_and_write_capabilities {
185+
my ($remote_caps, @our_caps) = @_;
186+
foreach (@our_caps) {
187+
unless (exists($remote_caps->{$_})) {
188+
die "our capability '$_' is not available from remote"
189+
}
190+
packet_txt_write( "capability=" . $_ );
191+
}
192+
packet_flush();
193+
}
194+
153195
print $debug "START\n";
154196
$debug->flush();
155197

156198
packet_initialize("git-filter", 2);
157199

158-
packet_compare_lists([0, "capability=clean"], packet_txt_read()) ||
159-
die "bad capability";
160-
packet_compare_lists([0, "capability=smudge"], packet_txt_read()) ||
161-
die "bad capability";
162-
packet_compare_lists([0, "capability=delay"], packet_txt_read()) ||
163-
die "bad capability";
164-
packet_compare_lists([1, ""], packet_bin_read()) ||
165-
die "bad capability end";
166-
167-
foreach (@capabilities) {
168-
packet_txt_write( "capability=" . $_ );
169-
}
170-
packet_flush();
200+
my %remote_caps = packet_read_and_check_capabilities("clean", "smudge", "delay");
201+
packet_check_and_write_capabilities(\%remote_caps, @capabilities);
202+
171203
print $debug "init handshake complete\n";
172204
$debug->flush();
173205

0 commit comments

Comments
 (0)