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

[PATCH v2 03/10] refs: add array of ref namespaces

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 29, 2022, 19:29 UTC
Message-ID
<d7486390d57849971ee6a4ac27a0a784c41623ee.1659122979.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1301.v2.git.1659122979.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

Git interprets different meanings to different refs based on their names. Some meanings are cosmetic, like how refs in 'refs/remotes/*' are colored differently from refs in 'refs/heads/*'. Others are more critical, such as how replace refs are interpreted.

Before making behavior changes based on ref namespaces, collect all known ref namespaces into a array of ref_namespace_info structs. This array is indexed by the new ref_namespace enum for quick access.

As of this change, this array is purely documentation. Future changes will add dependencies on this array.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 environment.c |  2 ++
 notes.c       |  1 +
 refs.c        | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 refs.h        | 46 ++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
diff --git a/environment.c b/environment.c
index b3296ce7d15..9eb22f975c7 100644
--- a/environment.c
+++ b/environment.c
@@ -185,6 +185,8 @@ void setup_git_env(const char *git_dir)
 	free(git_replace_ref_base);
 	git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
 							  : "refs/replace/");
+	update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
+
 	free(git_namespace);
 	git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
 	shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
diff --git a/notes.c b/notes.c
index 7452e71cc8d..7bade6d8f69 100644
--- a/notes.c
+++ b/notes.c
@@ -1005,6 +1005,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
 
 	if (!notes_ref)
 		notes_ref = default_notes_ref();
+	update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
 
 	if (!combine_notes)
 		combine_notes = combine_notes_concatenate;
diff --git a/refs.c b/refs.c
index ec3134e4842..8e87cc7efb0 100644
--- a/refs.c
+++ b/refs.c
@@ -20,6 +20,7 @@
 #include "repository.h"
 #include "sigchain.h"
 #include "date.h"
+#include "commit.h"
 
 /*
  * List of all available backends
@@ -56,6 +57,88 @@ static unsigned char refname_disposition[256] = {
 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
 };
 
+struct ref_namespace_info ref_namespaces[] = {
+	[NAMESPACE_HEAD] = {
+		.ref = "HEAD",
+		.decoration = DECORATION_REF_HEAD,
+		.exact = 1,
+	},
+	[NAMESPACE_BRANCHES] = {
+		.ref = "refs/heads/",
+		.decoration = DECORATION_REF_LOCAL,
+	},
+	[NAMESPACE_TAGS] = {
+		.ref = "refs/tags/",
+		.decoration = DECORATION_REF_TAG,
+	},
+	[NAMESPACE_REMOTE_REFS] = {
+		/*
+		 * The default refspec for new remotes copies refs from
+		 * refs/heads/ on the remote into refs/remotes/<remote>/.
+		 * As such, "refs/remotes/" has special handling.
+		 */
+		.ref = "refs/remotes/",
+		.decoration = DECORATION_REF_REMOTE,
+	},
+	[NAMESPACE_STASH] = {
+		/*
+		 * The single ref "refs/stash" stores the latest stash.
+		 * Older stashes can be found in the reflog.
+		 */
+		.ref = "refs/stash",
+		.exact = 1,
+		.decoration = DECORATION_REF_STASH,
+	},
+	[NAMESPACE_REPLACE] = {
+		/*
+		 * This namespace allows Git to act as if one object ID
+		 * points to the content of another. Unlike the other
+		 * ref namespaces, this one can be changed by the
+		 * GIT_REPLACE_REF_BASE environment variable. This
+		 * .namespace value will be overwritten in setup_git_env().
+		 */
+		.ref = "refs/replace/",
+		.decoration = DECORATION_GRAFTED,
+	},
+	[NAMESPACE_NOTES] = {
+		/*
+		 * The refs/notes/commit ref points to the tip of a
+		 * parallel commit history that adds metadata to commits
+		 * in the normal history. This branch can be overwritten
+		 * by the core.notesRef config variable or the
+		 * GIT_NOTES_REFS environment variable.
+		 */
+		.ref = "refs/notes/commit",
+		.exact = 1,
+	},
+	[NAMESPACE_PREFETCH] = {
+		/*
+		 * Prefetch refs are written by the background 'fetch'
+		 * maintenance task. It allows faster foreground fetches
+		 * by advertising these previously-downloaded tips without
+		 * updating refs/remotes/ without user intervention.
+		 */
+		.ref = "refs/prefetch/",
+	},
+	[NAMESPACE_REWRITTEN] = {
+		/*
+		 * Rewritten refs are used by the 'label' command in the
+		 * sequencer. These are particularly useful during an
+		 * interactive rebase that uses the 'merge' command.
+		 */
+		.ref = "refs/rewritten/",
+	},
+};
+
+void update_ref_namespace(enum ref_namespace namespace, char *ref)
+{
+	struct ref_namespace_info *info = &ref_namespaces[namespace];
+	if (info->ref_updated)
+		free(info->ref);
+	info->ref = ref;
+	info->ref_updated = 1;
+}
+
 /*
  * Try to read one refname component from the front of refname.
  * Return the length of the component found, or -1 if the component is
diff --git a/refs.h b/refs.h
index 47cb9edbaa8..94e8dedf939 100644
--- a/refs.h
+++ b/refs.h
@@ -2,6 +2,7 @@
 #define REFS_H
 
 #include "cache.h"
+#include "commit.h"
 
 struct object_id;
 struct ref_store;
@@ -930,4 +931,49 @@ struct ref_store *get_main_ref_store(struct repository *r);
 struct ref_store *get_submodule_ref_store(const char *submodule);
 struct ref_store *get_worktree_ref_store(const struct worktree *wt);
 
+/*
+ * Some of the names specified by refs have special meaning to Git.
+ * Organize these namespaces in a comon 'ref_namespaces' array for
+ * reference from multiple places in the codebase.
+ */
+
+struct ref_namespace_info {
+	char *ref;
+	enum decoration_type decoration;
+
+	/*
+	 * If 'exact' is true, then we must match the 'ref' exactly.
+	 * Otherwise, use a prefix match.
+	 *
+	 * 'orig_ref' is for internal use. It represents whether the
+	 * 'ref' value was replaced from its original literal version.
+	 */
+	unsigned exact:1,
+		 ref_updated:1;
+};
+
+enum ref_namespace {
+	NAMESPACE_HEAD,
+	NAMESPACE_BRANCHES,
+	NAMESPACE_TAGS,
+	NAMESPACE_REMOTE_REFS,
+	NAMESPACE_STASH,
+	NAMESPACE_REPLACE,
+	NAMESPACE_NOTES,
+	NAMESPACE_PREFETCH,
+	NAMESPACE_REWRITTEN,
+
+	/* Must be last */
+	NAMESPACE__COUNT
+};
+
+/* See refs.c for the contents of this array. */
+extern struct ref_namespace_info ref_namespaces[];
+
+/*
+ * Some ref namespaces can be modified by config values or environment
+ * variables. Modify a namespace as specified by its ref_namespace key.
+ */
+void update_ref_namespace(enum ref_namespace namespace, char *ref);
+
 #endif /* REFS_H */
-- 
gitgitgadget
Previous: Derrick StoleeNext: Junio C Hamano
Message 31 of 79 in “log: create tighter default decoration filter”
  1. 0/3 log: create tighter default decoration filterDerrick Stolee via GitGitGadget, Jul 26, 2022
  2. 1/3 refs: allow "HEAD" as decoration filterDerrick Stolee via GitGitGadget, Jul 26, 2022
  3. Ævar Arnfjörð BjarmasonJul 26, 2022
  4. Derrick StoleeJul 29, 2022
  5. 2/3 log: add default decoration filterDerrick Stolee via GitGitGadget, Jul 26, 2022
  6. Ævar Arnfjörð BjarmasonJul 26, 2022
  7. Derrick StoleeJul 29, 2022
  8. Ævar Arnfjörð BjarmasonJul 26, 2022
  9. Junio C HamanoJul 27, 2022
  10. Derrick StoleeJul 27, 2022
  11. Junio C HamanoJul 27, 2022
  12. Jeff KingJul 29, 2022
  13. 3/3 maintenance: stop writing log.excludeDecorationDerrick Stolee via GitGitGadget, Jul 26, 2022
  14. Junio C HamanoJul 27, 2022
  15. Ævar Arnfjörð BjarmasonJul 26, 2022
  16. Derrick StoleeJul 26, 2022
  17. Ævar Arnfjörð BjarmasonJul 26, 2022
  18. Derrick StoleeJul 27, 2022
  19. Ævar Arnfjörð BjarmasonJul 27, 2022
  20. 00/10 log: create tighter default decoration filterDerrick Stolee via GitGitGadget, Jul 29, 2022
  21. 01/10 refs: allow "HEAD" as decoration filterDerrick Stolee via GitGitGadget, Jul 29, 2022
  22. Junio C HamanoAug 3, 2022
  23. Derrick StoleeAug 4, 2022
  24. 02/10 t4207: test coloring of grafted decorationsDerrick Stolee via GitGitGadget, Jul 29, 2022
  25. Ævar Arnfjörð BjarmasonAug 3, 2022
  26. Eric SunshineAug 3, 2022
  27. Ævar Arnfjörð BjarmasonAug 3, 2022
  28. Eric SunshineAug 4, 2022
  29. Junio C HamanoAug 4, 2022
  30. Derrick StoleeAug 5, 2022
  31. 03/10 refs: add array of ref namespacesDerrick Stolee via GitGitGadget, Jul 29, 2022
  32. Junio C HamanoAug 3, 2022
  33. Derrick StoleeAug 4, 2022
  34. Junio C HamanoAug 4, 2022
  35. Josh SteadmonAug 3, 2022
  36. Derrick StoleeAug 4, 2022
  37. 04/10 refs: use ref_namespaces for replace refs baseDerrick Stolee via GitGitGadget, Jul 29, 2022
  38. Josh SteadmonAug 3, 2022
  39. Derrick StoleeAug 4, 2022
  40. 05/10 log-tree: use ref_namespaces instead of if/else-ifDerrick Stolee via GitGitGadget, Jul 29, 2022
  41. Junio C HamanoAug 3, 2022
  42. Derrick StoleeAug 4, 2022
  43. Derrick StoleeAug 4, 2022
  44. Derrick StoleeAug 4, 2022
  45. Junio C HamanoAug 4, 2022
  46. Derrick StoleeAug 4, 2022
  47. 06/10 log: add default decoration filterDerrick Stolee via GitGitGadget, Jul 29, 2022
  48. Ævar Arnfjörð BjarmasonAug 4, 2022
  49. Derrick StoleeAug 5, 2022
  50. Ævar Arnfjörð BjarmasonAug 5, 2022
  51. Derrick StoleeAug 5, 2022
  52. Ævar Arnfjörð BjarmasonAug 11, 2022
  53. Derrick StoleeAug 12, 2022
  54. 08/10 log: create log.decorateFilter=allDerrick Stolee via GitGitGadget, Jul 29, 2022
  55. Josh SteadmonAug 3, 2022
  56. Derrick StoleeAug 4, 2022
  57. 10/10 fetch: use ref_namespaces during prefetchDerrick Stolee via GitGitGadget, Jul 29, 2022
  58. 07/10 log: add --decorate-all optionDerrick Stolee via GitGitGadget, Jul 29, 2022
  59. Junio C HamanoAug 4, 2022
  60. 09/10 maintenance: stop writing log.excludeDecorationDerrick Stolee via GitGitGadget, Jul 29, 2022
  61. Junio C HamanoAug 3, 2022
  62. 00/11 log: create tighter default decoration filterDerrick Stolee via GitGitGadget, Aug 5, 2022
  63. 01/11 refs: allow "HEAD" as decoration filterDerrick Stolee via GitGitGadget, Aug 5, 2022
  64. 02/11 t4207: modernize testDerrick Stolee via GitGitGadget, Aug 5, 2022
  65. 03/11 t4207: test coloring of grafted decorationsDerrick Stolee via GitGitGadget, Aug 5, 2022
  66. 04/11 refs: add array of ref namespacesDerrick Stolee via GitGitGadget, Aug 5, 2022
  67. 05/11 refs: use ref_namespaces for replace refs baseDerrick Stolee via GitGitGadget, Aug 5, 2022
  68. 06/11 log-tree: use ref_namespaces instead of if/else-ifDerrick Stolee via GitGitGadget, Aug 5, 2022
  69. 07/11 log: add default decoration filterDerrick Stolee via GitGitGadget, Aug 5, 2022
  70. Glen ChooSep 8, 2022
  71. Derrick StoleeSep 9, 2022
  72. Junio C HamanoSep 9, 2022
  73. Derrick StoleeSep 9, 2022
  74. Junio C HamanoSep 9, 2022
  75. Glen ChooSep 9, 2022
  76. 08/11 log: add --clear-decorations optionDerrick Stolee via GitGitGadget, Aug 5, 2022
  77. 10/11 maintenance: stop writing log.excludeDecorationDerrick Stolee via GitGitGadget, Aug 5, 2022
  78. 09/11 log: create log.initialDecorationSet=allDerrick Stolee via GitGitGadget, Aug 5, 2022
  79. 11/11 fetch: use ref_namespaces during prefetchDerrick Stolee via GitGitGadget, Aug 5, 2022

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.