Skip to content

Commit 82e5676

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e814c39 commit 82e5676

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

fetch-pack.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,23 @@ static enum ack_type get_ack(int fd, unsigned char *result_sha1)
189189
{
190190
int len;
191191
char *line = packet_read_line(fd, &len);
192+
const char *arg;
192193

193194
if (!len)
194195
die("git fetch-pack: expected ACK/NAK, got EOF");
195196
if (!strcmp(line, "NAK"))
196197
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)
200203
return ACK;
201-
if (strstr(line+45, "continue"))
204+
if (strstr(arg, "continue"))
202205
return ACK_continue;
203-
if (strstr(line+45, "common"))
206+
if (strstr(arg, "common"))
204207
return ACK_common;
205-
if (strstr(line+45, "ready"))
208+
if (strstr(arg, "ready"))
206209
return ACK_ready;
207210
return ACK;
208211
}

0 commit comments

Comments
 (0)