git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] refs: split off reading loose ref data in separate function

From
Han-Wen Nienhuys via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 19, 2020, 14:27 UTC
Message-ID
<557727064d5e060e164984887652aa956b9d111c.1597847278.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.705.v2.git.1597847278.gitgitgadget@gmail.com>
From: Han-Wen Nienhuys <hanwen@google.com>

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>
---
 refs/files-backend.c | 34 +++++++++++++++++++---------------
 refs/refs-internal.h |  6 ++++++
 2 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 985631f33e..3a3573986f 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -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 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	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 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 		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)
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 357359a0be..24d79fb5c1 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -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.
-- 
gitgitgadget
Previous: Han-Wen Nienhuys via GitGitGadgetNext: Junio C Hamano
Message 11 of 12 in “Handle FETCH_HEAD generically”
  1. 0/4 Handle FETCH_HEAD genericallyHan-Wen Nienhuys via GitGitGadget, Aug 17, 2020
  2. 1/4 Split off reading loose ref data in separate functionHan-Wen Nienhuys via GitGitGadget, Aug 17, 2020
  3. 2/4 refs: fix comment about submodule ref_storesHan-Wen Nienhuys via GitGitGadget, Aug 17, 2020
  4. 3/4 refs: move gitdir into base ref_storeHan-Wen Nienhuys via GitGitGadget, Aug 17, 2020
  5. 4/4 refs: read FETCH_HEAD genericallyHan-Wen Nienhuys via GitGitGadget, Aug 17, 2020
  6. Junio C HamanoAug 17, 2020
  7. 0/4 Handle FETCH_HEAD and MERGE_HEAD genericallyHan-Wen Nienhuys via GitGitGadget, Aug 19, 2020
  8. 2/4 refs: fix comment about submodule ref_storesHan-Wen Nienhuys via GitGitGadget, Aug 19, 2020
  9. 3/4 refs: move gitdir into base ref_storeHan-Wen Nienhuys via GitGitGadget, Aug 19, 2020
  10. 4/4 refs: read FETCH_HEAD and MERGE_HEAD genericallyHan-Wen Nienhuys via GitGitGadget, Aug 19, 2020
  11. 1/4 refs: split off reading loose ref data in separate functionHan-Wen Nienhuys via GitGitGadget, Aug 19, 2020
  12. Junio C HamanoAug 19, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.