send-pack: prefer prefixcmp over memcmp in receive_status
authorJeff King <peff@peff.net>
Wed, 20 Feb 2013 20:00:43 +0000 (15:00 -0500)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Feb 2013 21:42:21 +0000 (13:42 -0800)
This code predates prefixcmp, so it used memcmp along with
static sizes. Replacing these memcmps with prefixcmp makes
the code much more readable, and the lack of static sizes
will make refactoring it in future patches simpler.

Note that we used to be unnecessarily liberal in parsing the
"unpack" status line, and would accept "unpack ok\njunk". No
version of git has ever produced that, and it violates the
BNF in Documentation/technical/pack-protocol.txt. Let's take
this opportunity to tighten the check by converting the
prefix comparison into a strcmp.

While we're in the area, let's also fix a vague error
message that does not follow our usual conventions (it
writes directly to stderr and does not use the "error:"
prefix).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
send-pack.c
index 97ab336097bfb45c63aec739a6eef2df26cc4790..e91cbe2c957003c98fcefee82f546101cdd13a9f 100644 (file)
@@ -109,9 +109,9 @@ static int receive_status(int in, struct ref *refs)
        char line[1000];
        int ret = 0;
        int len = packet_read_line(in, line, sizeof(line));
-       if (len < 10 || memcmp(line, "unpack ", 7))
+       if (prefixcmp(line, "unpack "))
                return error("did not receive remote status");
-       if (memcmp(line, "unpack ok\n", 10)) {
+       if (strcmp(line, "unpack ok\n")) {
                char *p = line + strlen(line) - 1;
                if (*p == '\n')
                        *p = '\0';
@@ -125,9 +125,8 @@ static int receive_status(int in, struct ref *refs)
                len = packet_read_line(in, line, sizeof(line));
                if (!len)
                        break;
-               if (len < 3 ||
-                   (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
-                       fprintf(stderr, "protocol error: %s\n", line);
+               if (prefixcmp(line, "ok ") && prefixcmp(line, "ng ")) {
+                       error("invalid ref status from remote: %s", line);
                        ret = -1;
                        break;
                }