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

[PATCH v4 01/19] hex: introduce parse_oid_hex

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Feb 20, 2017, 00:10 UTC
Message-ID
<20170220001031.559931-2-sandals@crustytoothpaste.net>
In-Reply-To
<20170220001031.559931-1-sandals@crustytoothpaste.net>

Introduce a function, parse_oid_hex, which parses a hexadecimal object ID and if successful, sets a pointer to just beyond the last character. This allows for simpler, more robust parsing without needing to hard-code integer values throughout the codebase.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 cache.h | 9 +++++++++
 hex.c   | 8 ++++++++
 2 files changed, 17 insertions(+)
diff --git a/cache.h b/cache.h
index 61fc86e6d7..e03a672d15 100644
--- a/cache.h
+++ b/cache.h
@@ -1319,6 +1319,15 @@ extern char *oid_to_hex_r(char *out, const struct object_id *oid);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as sha1_to_hex */
 
+/*
+ * Parse a 40-character hexadecimal object ID starting from hex, updating the
+ * pointer specified by end when parsing stops.  The resulting object ID is
+ * stored in oid.  Returns 0 on success.  Parsing will stop on the first NUL or
+ * other invalid character.  end is only updated on success; otherwise, it is
+ * unmodified.
+ */
+extern int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
+
 extern int interpret_branch_name(const char *str, int len, struct strbuf *);
 extern int get_oid_mb(const char *str, struct object_id *oid);
 
diff --git a/hex.c b/hex.c
index 845b01a874..eab7b626ee 100644
--- a/hex.c
+++ b/hex.c
@@ -53,6 +53,14 @@ int get_oid_hex(const char *hex, struct object_id *oid)
 	return get_sha1_hex(hex, oid->hash);
 }
 
+int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
+{
+	int ret = get_oid_hex(hex, oid);
+	if (!ret)
+		*end = hex + GIT_SHA1_HEXSZ;
+	return ret;
+}
+
 char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
 {
 	static const char hex[] = "0123456789abcdef";
-- 
2.11.0
Previous: brian m. carlsonNext: brian m. carlson
Message 7 of 24 in “object_id part 6”
  1. 00/19 object_id part 6brian m. carlson, Feb 20, 2017
  2. 16/19 sha1_file: introduce an nth_packed_object_oid functionbrian m. carlson, Feb 20, 2017
  3. 15/19 refs: simplify parsing of reflog entriesbrian m. carlson, Feb 20, 2017
  4. 05/19 builtin/fast-export: convert to struct object_idbrian m. carlson, Feb 20, 2017
  5. 09/19 builtin/clone: convert to struct object_idbrian m. carlson, Feb 20, 2017
  6. 18/19 builtin/merge-base: convert to struct object_idbrian m. carlson, Feb 20, 2017
  7. 01/19 hex: introduce parse_oid_hexbrian m. carlson, Feb 20, 2017
  8. 12/19 builtin/replace: convert to struct object_idbrian m. carlson, Feb 20, 2017
  9. 14/19 refs: convert each_reflog_ent_fn to struct object_idbrian m. carlson, Feb 20, 2017
  10. 19/19 wt-status: convert to struct object_idbrian m. carlson, Feb 20, 2017
  11. 08/19 builtin/branch: convert to struct object_idbrian m. carlson, Feb 20, 2017
  12. 17/19 Convert object iteration callbacks to struct object_idbrian m. carlson, Feb 20, 2017
  13. 06/19 builtin/fmt-merge-message: convert to struct object_idbrian m. carlson, Feb 20, 2017
  14. 07/19 builtin/grep: convert to struct object_idbrian m. carlson, Feb 20, 2017
  15. 13/19 reflog-walk: convert struct reflog_info to struct object_idbrian m. carlson, Feb 20, 2017
  16. 11/19 Convert remaining callers of resolve_refdup to object_idbrian m. carlson, Feb 20, 2017
  17. 10/19 builtin/merge: convert to struct object_idbrian m. carlson, Feb 20, 2017
  18. 02/19 builtin/commit: convert to struct object_idbrian m. carlson, Feb 20, 2017
  19. 04/19 builtin/describe: convert to struct object_idbrian m. carlson, Feb 20, 2017
  20. 03/19 builtin/diff-tree: convert to struct object_idbrian m. carlson, Feb 20, 2017
  21. Jeff KingFeb 20, 2017
  22. brian m. carlsonFeb 21, 2017
  23. Jeff KingFeb 21, 2017
  24. brian m. carlsonFeb 21, 2017

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.