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

[PATCH 13/43] refs.c: move check_refname_format to the common code

From
David Turner <dturner@twopensource.com>
Date
Sep 3, 2015, 01:54 UTC
Message-ID
<1441245313-11907-14-git-send-email-dturner@twopensource.com>
In-Reply-To
<1441245313-11907-1-git-send-email-dturner@twopensource.com>
From: Ronnie Sahlberg <sahlberg@google.com>

This function does not contain any backend specific code so we move it to the common code.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twitter.com>
---
 refs-be-files.c | 109 --------------------------------------------------------
 refs.c          | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 109 deletions(-)
diff --git a/refs-be-files.c b/refs-be-files.c
index 6328e75..3b06714 100644
--- a/refs-be-files.c
+++ b/refs-be-files.c
@@ -14,27 +14,6 @@ struct ref_lock {
 };
 
 /*
- * How to handle various characters in refnames:
- * 0: An acceptable character for refs
- * 1: End-of-component
- * 2: ., look for a preceding . to reject .. in refs
- * 3: {, look for a preceding @ to reject @{ in refs
- * 4: A bad character: ASCII control characters, and
- *    ":", "?", "[", "\", "^", "~", SP, or TAB
- * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
- */
-static unsigned char refname_disposition[256] = {
-	1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
-	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
-	4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
-};
-
-/*
  * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
  * refs (i.e., because the reference is about to be deleted anyway).
  */
@@ -69,94 +48,6 @@ static unsigned char refname_disposition[256] = {
  * value to ref_update::flags
  */
 
-/*
- * Try to read one refname component from the front of refname.
- * Return the length of the component found, or -1 if the component is
- * not legal.  It is legal if it is something reasonable to have under
- * ".git/refs/"; We do not like it if:
- *
- * - any path component of it begins with ".", or
- * - it has double dots "..", or
- * - it has ASCII control characters, or
- * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
- * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
- * - it ends with a "/", or
- * - it ends with ".lock", or
- * - it contains a "@{" portion
- */
-static int check_refname_component(const char *refname, int *flags)
-{
-	const char *cp;
-	char last = '\0';
-
-	for (cp = refname; ; cp++) {
-		int ch = *cp & 255;
-		unsigned char disp = refname_disposition[ch];
-		switch (disp) {
-		case 1:
-			goto out;
-		case 2:
-			if (last == '.')
-				return -1; /* Refname contains "..". */
-			break;
-		case 3:
-			if (last == '@')
-				return -1; /* Refname contains "@{". */
-			break;
-		case 4:
-			return -1;
-		case 5:
-			if (!(*flags & REFNAME_REFSPEC_PATTERN))
-				return -1; /* refspec can't be a pattern */
-
-			/*
-			 * Unset the pattern flag so that we only accept
-			 * a single asterisk for one side of refspec.
-			 */
-			*flags &= ~ REFNAME_REFSPEC_PATTERN;
-			break;
-		}
-		last = ch;
-	}
-out:
-	if (cp == refname)
-		return 0; /* Component has zero length. */
-	if (refname[0] == '.')
-		return -1; /* Component starts with '.'. */
-	if (cp - refname >= LOCK_SUFFIX_LEN &&
-	    !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
-		return -1; /* Refname ends with ".lock". */
-	return cp - refname;
-}
-
-int check_refname_format(const char *refname, int flags)
-{
-	int component_len, component_count = 0;
-
-	if (!strcmp(refname, "@"))
-		/* Refname is a single character '@'. */
-		return -1;
-
-	while (1) {
-		/* We are at the start of a path component. */
-		component_len = check_refname_component(refname, &flags);
-		if (component_len <= 0)
-			return -1;
-
-		component_count++;
-		if (refname[component_len] == '\0')
-			break;
-		/* Skip to next component. */
-		refname += component_len + 1;
-	}
-
-	if (refname[component_len - 1] == '.')
-		return -1; /* Refname ends with '.'. */
-	if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
-		return -1; /* Refname has only one component. */
-	return 0;
-}
-
 struct ref_entry;
 
 /*
diff --git a/refs.c b/refs.c
index 039e5c0..e9cc2d4 100644
--- a/refs.c
+++ b/refs.c
@@ -658,3 +658,112 @@ char *resolve_refdup(const char *refname, int resolve_flags,
 	return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
 						  sha1, flags));
 }
+
+/*
+ * How to handle various characters in refnames:
+ * 0: An acceptable character for refs
+ * 1: End-of-component
+ * 2: ., look for a preceding . to reject .. in refs
+ * 3: {, look for a preceding @ to reject @{ in refs
+ * 4: A bad character: ASCII control characters, and
+ *    ":", "?", "[", "\", "^", "~", SP, or TAB
+ * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
+ */
+static unsigned char refname_disposition[256] = {
+	1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+	4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
+};
+
+/*
+ * Try to read one refname component from the front of refname.
+ * Return the length of the component found, or -1 if the component is
+ * not legal.  It is legal if it is something reasonable to have under
+ * ".git/refs/"; We do not like it if:
+ *
+ * - any path component of it begins with ".", or
+ * - it has double dots "..", or
+ * - it has ASCII control characters, or
+ * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
+ * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
+ * - it ends with a "/", or
+ * - it ends with ".lock", or
+ * - it contains a "@{" portion
+ */
+static int check_refname_component(const char *refname, int *flags)
+{
+	const char *cp;
+	char last = '\0';
+
+	for (cp = refname; ; cp++) {
+		int ch = *cp & 255;
+		unsigned char disp = refname_disposition[ch];
+		switch (disp) {
+		case 1:
+			goto out;
+		case 2:
+			if (last == '.')
+				return -1; /* Refname contains "..". */
+			break;
+		case 3:
+			if (last == '@')
+				return -1; /* Refname contains "@{". */
+			break;
+		case 4:
+			return -1;
+		case 5:
+			if (!(*flags & REFNAME_REFSPEC_PATTERN))
+				return -1; /* refspec can't be a pattern */
+
+			/*
+			 * Unset the pattern flag so that we only accept
+			 * a single asterisk for one side of refspec.
+			 */
+			*flags &= ~ REFNAME_REFSPEC_PATTERN;
+			break;
+		}
+		last = ch;
+	}
+out:
+	if (cp == refname)
+		return 0; /* Component has zero length. */
+	if (refname[0] == '.')
+		return -1; /* Component starts with '.'. */
+	if (cp - refname >= LOCK_SUFFIX_LEN &&
+	    !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
+		return -1; /* Refname ends with ".lock". */
+	return cp - refname;
+}
+
+int check_refname_format(const char *refname, int flags)
+{
+	int component_len, component_count = 0;
+
+	if (!strcmp(refname, "@"))
+		/* Refname is a single character '@'. */
+		return -1;
+
+	while (1) {
+		/* We are at the start of a path component. */
+		component_len = check_refname_component(refname, &flags);
+		if (component_len <= 0)
+			return -1;
+
+		component_count++;
+		if (refname[component_len] == '\0')
+			break;
+		/* Skip to next component. */
+		refname += component_len + 1;
+	}
+
+	if (refname[component_len - 1] == '.')
+		return -1; /* Refname ends with '.'. */
+	if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
+		return -1; /* Refname has only one component. */
+	return 0;
+}
-- 
2.0.4.315.gad8727a-twtrsrc
Previous: David TurnerNext: David Turner
Message 15 of 54 in “refs lmdb backend”
  1. 00/43 refs lmdb backendDavid Turner, Sep 3, 2015
  2. 01/43 refs.c: create a public version of verify_refname_availableDavid Turner, Sep 3, 2015
  3. Junio C HamanoSep 3, 2015
  4. David TurnerSep 4, 2015
  5. 02/43 refs: make repack_without_refs and is_branch publicDavid Turner, Sep 3, 2015
  6. 04/43 refs.c: add a new refs.c file to hold all common refs codeDavid Turner, Sep 3, 2015
  7. 05/43 refs.c: move update_ref to refs.cDavid Turner, Sep 3, 2015
  8. 06/43 refs.c: move delete_ref and delete_refs to the common codeDavid Turner, Sep 3, 2015
  9. 07/43 refs.c: move read_ref_at to the common refs fileDavid Turner, Sep 3, 2015
  10. 08/43 refs.c: move the hidden refs functions to the common codeDavid Turner, Sep 3, 2015
  11. 09/43 refs.c: move dwim and friend functions to the common refs codeDavid Turner, Sep 3, 2015
  12. 10/43 refs.c: move warn_if_dangling_symref* to the common codeDavid Turner, Sep 3, 2015
  13. 11/43 refs.c: move read_ref, read_ref_full and ref_exists to the common codeDavid Turner, Sep 3, 2015
  14. 12/43 refs.c: move resolve_refdup to commonDavid Turner, Sep 3, 2015
  15. 13/43 refs.c: move check_refname_format to the common codeDavid Turner, Sep 3, 2015
  16. 14/43 refs.c: move is_branch to the common codeDavid Turner, Sep 3, 2015
  17. 15/43 refs.c: move prettify_refname to the common codeDavid Turner, Sep 3, 2015
  18. 16/43 refs.c: move ref iterators to the common codeDavid Turner, Sep 3, 2015
  19. 17/43 refs.c: move head_ref_namespaced to the common codeDavid Turner, Sep 3, 2015
  20. 18/43 refs-be-files.c: add a backend method structure with transaction functionsDavid Turner, Sep 3, 2015
  21. 19/43 refs-be-files.c: add methods for misc ref operationsDavid Turner, Sep 3, 2015
  22. 20/43 refs-be-files.c: add methods for the ref iteratorsDavid Turner, Sep 3, 2015
  23. Junio C HamanoSep 25, 2015
  24. David TurnerSep 25, 2015
  25. Junio C HamanoSep 25, 2015
  26. 21/43 refs-be-files.c: add method for for_each_reftype_...David Turner, Sep 3, 2015
  27. 22/43 refs-be-files.c: add do_for_each_per_worktree_refDavid Turner, Sep 3, 2015
  28. 23/43 refs.c: move refname_is_safe to the common codeDavid Turner, Sep 3, 2015
  29. 24/43 refs.h: document make refname_is_safe and add it to headerDavid Turner, Sep 3, 2015
  30. David TurnerSep 3, 2015
  31. 25/43 refs.c: move copy_msg to the common codeDavid Turner, Sep 3, 2015
  32. 26/43 refs.c: move peel_object to the common codeDavid Turner, Sep 3, 2015
  33. 27/43 refs.c: move should_autocreate_reflog to common codeDavid Turner, Sep 3, 2015
  34. 28/43 refs.c: add ref backend init functionDavid Turner, Sep 3, 2015
  35. 29/43 refs.c: add methods for reflogDavid Turner, Sep 3, 2015
  36. 30/43 refs.c: add method for initial ref transaction commitDavid Turner, Sep 3, 2015
  37. 31/43 initdb: move safe_create_dir into common codeDavid Turner, Sep 3, 2015
  38. 32/43 refs.c: add method for initializing refs dbDavid Turner, Sep 3, 2015
  39. 33/43 refs.c: make struct ref_transaction genericDavid Turner, Sep 3, 2015
  40. 34/43 refs-be-files.c: add method to rename refsDavid Turner, Sep 3, 2015
  41. 35/43 refs-be-files.c: add method to expire reflogsDavid Turner, Sep 3, 2015
  42. 36/43 run-command: track total number of commands runDavid Turner, Sep 3, 2015
  43. 37/43 refs: move some defines from refs-be-files.c to refs.hDavid Turner, Sep 3, 2015
  44. 38/43 refs: make some files backend functions publicDavid Turner, Sep 3, 2015
  45. 39/43 refs: break out a ref conflict checkDavid Turner, Sep 3, 2015
  46. 40/43 refs: allow ref backend to be set for cloneDavid Turner, Sep 3, 2015
  47. 41/43 refs: add register_refs_backendDavid Turner, Sep 3, 2015
  48. 42/43 refs: add LMDB refs backendDavid Turner, Sep 3, 2015
  49. Junio C HamanoSep 3, 2015
  50. 43/43 refs: tests for db backendDavid Turner, Sep 3, 2015
  51. Junio C HamanoSep 3, 2015
  52. David TurnerSep 4, 2015
  53. David TurnerSep 17, 2015
  54. Junio C HamanoSep 17, 2015

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.