150
packet_flush();
151
}
152
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
+
195
print $debug "START\n";
196
$debug->flush();
197
198
packet_initialize("git-filter", 2);
199
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
+
203
print $debug "init handshake complete\n";
204
$debug->flush();
205