]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: split off reading loose ref data in separate function
authorHan-Wen Nienhuys <hanwen@google.com>
Wed, 19 Aug 2020 14:27:55 +0000 (14:27 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Aug 2020 21:08:03 +0000 (14:08 -0700)
This prepares for handling FETCH_HEAD (which is not a regular ref)
separately from the ref backend.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/files-backend.c
refs/refs-internal.h

index 985631f33edf92afc706061b0d208bd35fab4bdc..3a3573986f166d51db625f501e32f36e583fd24b 100644 (file)
@@ -360,7 +360,6 @@ static int files_read_raw_ref(struct ref_store *ref_store,
        struct strbuf sb_path = STRBUF_INIT;
        const char *path;
        const char *buf;
-       const char *p;
        struct stat st;
        int fd;
        int ret = -1;
@@ -465,6 +464,21 @@ stat_ref:
        close(fd);
        strbuf_rtrim(&sb_contents);
        buf = sb_contents.buf;
+
+       ret = parse_loose_ref_contents(buf, oid, referent, type);
+
+out:
+       save_errno = errno;
+       strbuf_release(&sb_path);
+       strbuf_release(&sb_contents);
+       errno = save_errno;
+       return ret;
+}
+
+int parse_loose_ref_contents(const char *buf, struct object_id *oid,
+                            struct strbuf *referent, unsigned int *type)
+{
+       const char *p;
        if (skip_prefix(buf, "ref:", &buf)) {
                while (isspace(*buf))
                        buf++;
@@ -472,29 +486,19 @@ stat_ref:
                strbuf_reset(referent);
                strbuf_addstr(referent, buf);
                *type |= REF_ISSYMREF;
-               ret = 0;
-               goto out;
+               return 0;
        }
 
        /*
-        * Please note that FETCH_HEAD has additional
-        * data after the sha.
+        * FETCH_HEAD has additional data after the sha.
         */
        if (parse_oid_hex(buf, oid, &p) ||
            (*p != '\0' && !isspace(*p))) {
                *type |= REF_ISBROKEN;
                errno = EINVAL;
-               goto out;
+               return -1;
        }
-
-       ret = 0;
-
-out:
-       save_errno = errno;
-       strbuf_release(&sb_path);
-       strbuf_release(&sb_contents);
-       errno = save_errno;
-       return ret;
+       return 0;
 }
 
 static void unlock_ref(struct ref_lock *lock)
index 357359a0be4a5bc4e360dd0e13d33ae6c04c4c70..24d79fb5c146420c2f1cda5644a959362118e024 100644 (file)
@@ -674,6 +674,12 @@ struct ref_store {
        const struct ref_storage_be *be;
 };
 
+/*
+ * Parse contents of a loose ref file.
+ */
+int parse_loose_ref_contents(const char *buf, struct object_id *oid,
+                            struct strbuf *referent, unsigned int *type);
+
 /*
  * Fill in the generic part of refs and add it to our collection of
  * reference stores.