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

[PATCH v6 12/25] refs: move check_refname_format to the common code

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Nov 3, 2015, 07:39 UTC
Message-ID
<4dad486935e0752408c29ab20ec4acd74a35d1e4.1446534991.git.mhagger@alum.mit.edu>
In-Reply-To
<cover.1446534991.git.mhagger@alum.mit.edu>
From: Ronnie Sahlberg <sahlberg@google.com>

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

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs/files-backend.c | 109 ---------------------------------------------------
 refs/refs.c          | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 109 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index f84fa9e..545075f 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.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/refs.c b/refs/refs.c
index 56dee05..bc7e159 100644
--- a/refs/refs.c
+++ b/refs/refs.c
@@ -622,3 +622,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.6.2
Previous: Michael HaggertyNext: Michael Haggerty
Message 45 of 67 in “refs backend pre-vtable”
  1. 00/26 refs backend pre-vtableDavid Turner, Oct 28, 2015
  2. 01/26 refs.c: create a public version of verify_refname_availableDavid Turner, Oct 28, 2015
  3. 02/26 refs: make is_branch publicDavid Turner, Oct 28, 2015
  4. 03/26 refs-be-files.c: rename refs to refs-be-filesDavid Turner, Oct 28, 2015
  5. 04/26 refs.c: add a new refs.c file to hold all common refs codeDavid Turner, Oct 28, 2015
  6. 05/26 refs.c: move update_ref to refs.cDavid Turner, Oct 28, 2015
  7. 06/26 refs.c: move delete_pseudoref and delete_ref to the common codeDavid Turner, Oct 28, 2015
  8. 07/26 refs.c: move read_ref_at to the common refs fileDavid Turner, Oct 28, 2015
  9. 08/26 refs.c: move the hidden refs functions to the common codeDavid Turner, Oct 28, 2015
  10. 09/26 refs.c: move dwim and friend functions to the common refs codeDavid Turner, Oct 28, 2015
  11. 10/26 refs.c: move warn_if_dangling_symref* to the common codeDavid Turner, Oct 28, 2015
  12. 11/26 refs.c: move read_ref, read_ref_full and ref_exists to the common codeDavid Turner, Oct 28, 2015
  13. 12/26 refs.c: move resolve_refdup to commonDavid Turner, Oct 28, 2015
  14. 13/26 refs.c: move check_refname_format to the common codeDavid Turner, Oct 28, 2015
  15. 14/26 refs.c: move is_branch to the common codeDavid Turner, Oct 28, 2015
  16. 15/26 refs.c: move prettify_refname to the common codeDavid Turner, Oct 28, 2015
  17. 16/26 refs.c: move ref iterators to the common codeDavid Turner, Oct 28, 2015
  18. Michael HaggertyNov 1, 2015
  19. 17/26 refs.c: move head_ref_namespaced to the common codeDavid Turner, Oct 28, 2015
  20. 18/26 refs: move transaction functions into common codeDavid Turner, Oct 28, 2015
  21. Michael HaggertyNov 1, 2015
  22. David TurnerNov 2, 2015
  23. 19/26 refs.c: move refname_is_safe to the common codeDavid Turner, Oct 28, 2015
  24. 20/26 refs.c: move copy_msg to the common codeDavid Turner, Oct 28, 2015
  25. 21/26 refs.c: move peel_object to the common codeDavid Turner, Oct 28, 2015
  26. 22/26 refs.c: move should_autocreate_reflog to common codeDavid Turner, Oct 28, 2015
  27. 23/26 initdb: move safe_create_dir into common codeDavid Turner, Oct 28, 2015
  28. 24/26 refs: make files_log_ref_write functions publicDavid Turner, Oct 28, 2015
  29. 25/26 refs: break out ref conflict checksDavid Turner, Oct 28, 2015
  30. Michael HaggertyNov 2, 2015
  31. 26/26 introduce "extensions" form of core.repositoryformatversionDavid Turner, Oct 28, 2015
  32. Michael HaggertyNov 3, 2015
  33. 00/25 refs backend pre-vtableMichael Haggerty, Nov 3, 2015
  34. 01/25 refs: make is_branch publicMichael Haggerty, Nov 3, 2015
  35. 02/25 refs/files-backend.c: new file, renamed from refs.cMichael Haggerty, Nov 3, 2015
  36. 03/25 refs: add a new file, refs/refs.c, to hold common refs codeMichael Haggerty, Nov 3, 2015
  37. 04/25 refs: move update_ref to refs/refs.cMichael Haggerty, Nov 3, 2015
  38. 05/25 refs: move delete_pseudoref and delete_ref to the common codeMichael Haggerty, Nov 3, 2015
  39. 06/25 refs: move read_ref_at to the common refs fileMichael Haggerty, Nov 3, 2015
  40. 07/25 refs: move the hidden refs functions to the common codeMichael Haggerty, Nov 3, 2015
  41. 08/25 refs: move dwim and friend functions to the common refs codeMichael Haggerty, Nov 3, 2015
  42. 09/25 refs: move warn_if_dangling_symref* to the common codeMichael Haggerty, Nov 3, 2015
  43. 10/25 refs: move read_ref, read_ref_full and ref_exists to the common codeMichael Haggerty, Nov 3, 2015
  44. 11/25 refs: move resolve_refdup to commonMichael Haggerty, Nov 3, 2015
  45. 12/25 refs: move check_refname_format to the common codeMichael Haggerty, Nov 3, 2015
  46. 13/25 refs: move is_branch to the common codeMichael Haggerty, Nov 3, 2015
  47. 14/25 refs: move prettify_refname to the common codeMichael Haggerty, Nov 3, 2015
  48. 15/25 refs: move ref iterators to the common codeMichael Haggerty, Nov 3, 2015
  49. 16/25 refs: move head_ref_namespaced to the common codeMichael Haggerty, Nov 3, 2015
  50. 17/25 refs: move transaction functions to the common codeMichael Haggerty, Nov 3, 2015
  51. 18/25 refs: move refname_is_safe to the common codeMichael Haggerty, Nov 3, 2015
  52. 19/25 refs: move copy_msg to the common codeMichael Haggerty, Nov 3, 2015
  53. 20/25 refs: move peel_object to the common codeMichael Haggerty, Nov 3, 2015
  54. 21/25 refs: move should_autocreate_reflog to common codeMichael Haggerty, Nov 3, 2015
  55. 22/25 initdb: make safe_create_dir publicMichael Haggerty, Nov 3, 2015
  56. 23/25 files_log_ref_write: new functionMichael Haggerty, Nov 3, 2015
  57. 24/25 refs: create a shared version of verify_refname_availableMichael Haggerty, Nov 3, 2015
  58. 25/25 refs: break out ref conflict checksMichael Haggerty, Nov 3, 2015
  59. David TurnerNov 4, 2015
  60. Michael HaggertyNov 5, 2015
  61. David TurnerNov 5, 2015
  62. Michael HaggertyNov 6, 2015
  63. Junio C HamanoNov 6, 2015
  64. Junio C HamanoNov 6, 2015
  65. Michael HaggertyNov 8, 2015
  66. Michael HaggertyNov 8, 2015
  67. Junio C HamanoNov 8, 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.