From a1bea2c1fc5e5243e873919248940dd5fff593a4 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 5 Jul 2011 10:54:44 -0700 Subject: ref namespaces: infrastructure Add support for dividing the refs of a single repository into multiple namespaces, each of which can have its own branches, tags, and HEAD. Git can expose each namespace as an independent repository to pull from and push to, while sharing the object store, and exposing all the refs to operations such as git-gc. Storing multiple repositories as namespaces of a single repository avoids storing duplicate copies of the same objects, such as when storing multiple branches of the same source. The alternates mechanism provides similar support for avoiding duplicates, but alternates do not prevent duplication between new objects added to the repositories without ongoing maintenance, while namespaces do. To specify a namespace, set the GIT_NAMESPACE environment variable to the namespace. For each ref namespace, git stores the corresponding refs in a directory under refs/namespaces/. For example, GIT_NAMESPACE=foo will store refs under refs/namespaces/foo/. You can also specify namespaces via the --namespace option to git. Note that namespaces which include a / will expand to a hierarchy of namespaces; for example, GIT_NAMESPACE=foo/bar will store refs under refs/namespaces/foo/refs/namespaces/bar/. This makes paths in GIT_NAMESPACE behave hierarchically, so that cloning with GIT_NAMESPACE=foo/bar produces the same result as cloning with GIT_NAMESPACE=foo and cloning from that repo with GIT_NAMESPACE=bar. It also avoids ambiguity with strange namespace paths such as foo/refs/heads/, which could otherwise generate directory/file conflicts within the refs directory. Add the infrastructure for ref namespaces: handle the GIT_NAMESPACE environment variable and --namespace option, and support iterating over refs in a namespace. Signed-off-by: Josh Triplett Signed-off-by: Jamey Sharp Signed-off-by: Junio C Hamano --- environment.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'environment.c') diff --git a/environment.c b/environment.c index 94d58fd24..aad274b37 100644 --- a/environment.c +++ b/environment.c @@ -8,6 +8,7 @@ * are. */ #include "cache.h" +#include "refs.h" char git_default_email[MAX_GITNAME]; char git_default_name[MAX_GITNAME]; @@ -65,6 +66,9 @@ int core_preload_index = 0; char *git_work_tree_cfg; static char *work_tree; +static const char *namespace; +static size_t namespace_len; + static const char *git_dir; static char *git_object_dir, *git_index_file, *git_graft_file; @@ -86,6 +90,27 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = { NULL }; +static char *expand_namespace(const char *raw_namespace) +{ + struct strbuf buf = STRBUF_INIT; + struct strbuf **components, **c; + + if (!raw_namespace || !*raw_namespace) + return xstrdup(""); + + strbuf_addstr(&buf, raw_namespace); + components = strbuf_split(&buf, '/'); + strbuf_reset(&buf); + for (c = components; *c; c++) + if (strcmp((*c)->buf, "/") != 0) + strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf); + strbuf_list_free(components); + if (check_ref_format(buf.buf) != CHECK_REF_FORMAT_OK) + die("bad git namespace path \"%s\"", raw_namespace); + strbuf_addch(&buf, '/'); + return strbuf_detach(&buf, NULL); +} + static void setup_git_env(void) { git_dir = getenv(GIT_DIR_ENVIRONMENT); @@ -111,6 +136,8 @@ static void setup_git_env(void) git_graft_file = git_pathdup("info/grafts"); if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT)) read_replace_refs = 0; + namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT)); + namespace_len = strlen(namespace); } int is_bare_repository(void) @@ -131,6 +158,20 @@ const char *get_git_dir(void) return git_dir; } +const char *get_git_namespace(void) +{ + if (!namespace) + setup_git_env(); + return namespace; +} + +const char *strip_namespace(const char *namespaced_ref) +{ + if (prefixcmp(namespaced_ref, get_git_namespace()) != 0) + return NULL; + return namespaced_ref + namespace_len; +} + static int git_work_tree_initialized; /* -- cgit v1.2.1