checkout: check_linked_checkout: simplify symref parsing
authorEric Sunshine <sunshine@sunshineco.com>
Fri, 17 Jul 2015 23:00:02 +0000 (19:00 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 Jul 2015 18:29:24 +0000 (11:29 -0700)
check_linked_checkout() only understands symref-style HEAD (i.e. "ref:
refs/heads/master"), however, HEAD may also be a an actual symbolic link
(on platforms which support it), thus it will need to check that style
HEAD, as well (via readlink()). As a preparatory step, simplify parsing
of symref-style HEAD so the actual branch check can be re-used easily
for symbolic links (in an upcoming patch).

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
index de6619f4c503792ae727e3862a36712ca0e06e6a..6f4e49232adc5d89850d6211b7cdf3c926821e7a 100644 (file)
@@ -878,7 +878,6 @@ static void check_linked_checkout(const char *branch, const char *id)
        struct strbuf sb = STRBUF_INIT;
        struct strbuf path = STRBUF_INIT;
        struct strbuf gitdir = STRBUF_INIT;
-       const char *start, *end;
 
        /*
         * $GIT_COMMON_DIR/HEAD is practically outside
@@ -890,15 +889,13 @@ static void check_linked_checkout(const char *branch, const char *id)
        else
                strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
 
-       if (strbuf_read_file(&sb, path.buf, 0) < 0 ||
-           !skip_prefix(sb.buf, "ref:", &start))
+       if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
+           starts_with(sb.buf, "ref:")) {
+               strbuf_remove(&sb, 0, strlen("ref:"));
+               strbuf_trim(&sb);
+       } else
                goto done;
-       while (isspace(*start))
-               start++;
-       end = start;
-       while (*end && !isspace(*end))
-               end++;
-       if (strncmp(start, branch, end - start) || branch[end - start] != '\0')
+       if (strcmp(sb.buf, branch))
                goto done;
        if (id) {
                strbuf_reset(&path);