checkout: check_linked_checkout: simplify symref parsing

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>

Eric Sunshine committed Jul 17, 2015 at 19:00 UTC 33aef83666900df03c39bcf7e391e2f8195dd13c
1 file changed +6 -9
builtin/checkout.c
+6 -9
@@ -878,7 +878,6 @@ static void check_linked_checkout(const char *branch, const char *id)
878 struct strbuf sb = STRBUF_INIT;
879 struct strbuf path = STRBUF_INIT;
880 struct strbuf gitdir = STRBUF_INIT;
881 - const char *start, *end;
881
882 /*
883 * $GIT_COMMON_DIR/HEAD is practically outside
@@ -890,15 +889,13 @@ static void check_linked_checkout(const char *branch, const char *id)
889 else
890 strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
891
893 - if (strbuf_read_file(&sb, path.buf, 0) < 0 ||
894 - !skip_prefix(sb.buf, "ref:", &start))
892 + if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
893 + starts_with(sb.buf, "ref:")) {
894 + strbuf_remove(&sb, 0, strlen("ref:"));
895 + strbuf_trim(&sb);
896 + } else
897 goto done;
896 - while (isspace(*start))
897 - start++;
898 - end = start;
899 - while (*end && !isspace(*end))
900 - end++;
901 - if (strncmp(start, branch, end - start) || branch[end - start] != '\0')
898 + if (strcmp(sb.buf, branch))
899 goto done;
900 if (id) {
901 strbuf_reset(&path);