fetch-pack: refactor parsing in get_ack

There are several uses of the magic number "line+45" when parsing ACK lines from the server, and it's rather unclear why 45 is the correct number. We can make this more clear by keeping a running pointer as we parse, using skip_prefix to jump past the first "ACK ", then adding 40 to jump past get_sha1_hex (which is still magical, but hopefully 40 is less magical to readers of git code). Note that this actually puts us at line+44. The original required some character between the sha1 and further ACK flags (it is supposed to be a space, but we never enforced that). We start our search for flags at line+44, which meanas we are slightly more liberal than the old code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 18, 2014 at 15:56 UTC 82e56767aa9334bb0c4cfe81964a576778b93d6e
1 file changed +9 -6
fetch-pack.c
+9 -6
@@ -189,20 +189,23 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
189 {
190 int len;
191 char *line = packet_read_line(fd, &len);
192 + const char *arg;
193
194 if (!len)
195 die("git fetch-pack: expected ACK/NAK, got EOF");
196 if (!strcmp(line, "NAK"))
197 return NAK;
197 - if (starts_with(line, "ACK ")) {
198 - if (!get_sha1_hex(line+4, result_sha1)) {
199 - if (len < 45)
198 + if (skip_prefix(line, "ACK ", &arg)) {
199 + if (!get_sha1_hex(arg, result_sha1)) {
200 + arg += 40;
201 + len -= arg - line;
202 + if (len < 1)
203 return ACK;
201 - if (strstr(line+45, "continue"))
204 + if (strstr(arg, "continue"))
205 return ACK_continue;
203 - if (strstr(line+45, "common"))
206 + if (strstr(arg, "common"))
207 return ACK_common;
205 - if (strstr(line+45, "ready"))
208 + if (strstr(arg, "ready"))
209 return ACK_ready;
210 return ACK;
211 }