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 <josh@joshtriplett.org> Signed-off-by: Jamey Sharp <jamey@minilop.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Josh Triplett committed Jul 5, 2011 at 10:54 UTC a1bea2c1fc5e5243e873919248940dd5fff593a4
6 files changed +89 -2
cache.h
+3
@@ -379,6 +379,7 @@ static inline enum object_type object_type(unsigned int mode)
379 }
380
381 #define GIT_DIR_ENVIRONMENT "GIT_DIR"
382 +#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
383 #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
384 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
385 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
@@ -419,6 +420,8 @@ extern char *get_object_directory(void);
420 extern char *get_index_file(void);
421 extern char *get_graft_file(void);
422 extern int set_git_dir(const char *path);
423 +extern const char *get_git_namespace(void);
424 +extern const char *strip_namespace(const char *namespaced_ref);
425 extern const char *get_git_work_tree(void);
426 extern const char *read_gitfile_gently(const char *path);
427 extern void set_git_work_tree(const char *tree);
contrib/completion/git-completion.bash
+1
@@ -2639,6 +2639,7 @@ _git ()
2639 --exec-path
2640 --html-path
2641 --work-tree=
2642 + --namespace=
2643 --help
2644 "
2645 ;;
environment.c
+41
@@ -8,6 +8,7 @@
8 * are.
9 */
10 #include "cache.h"
11 +#include "refs.h"
12
13 char git_default_email[MAX_GITNAME];
14 char git_default_name[MAX_GITNAME];
@@ -65,6 +66,9 @@ int core_preload_index = 0;
66 char *git_work_tree_cfg;
67 static char *work_tree;
68
69 +static const char *namespace;
70 +static size_t namespace_len;
71 +
72 static const char *git_dir;
73 static char *git_object_dir, *git_index_file, *git_graft_file;
74
@@ -86,6 +90,27 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
90 NULL
91 };
92
93 +static char *expand_namespace(const char *raw_namespace)
94 +{
95 + struct strbuf buf = STRBUF_INIT;
96 + struct strbuf **components, **c;
97 +
98 + if (!raw_namespace || !*raw_namespace)
99 + return xstrdup("");
100 +
101 + strbuf_addstr(&buf, raw_namespace);
102 + components = strbuf_split(&buf, '/');
103 + strbuf_reset(&buf);
104 + for (c = components; *c; c++)
105 + if (strcmp((*c)->buf, "/") != 0)
106 + strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
107 + strbuf_list_free(components);
108 + if (check_ref_format(buf.buf) != CHECK_REF_FORMAT_OK)
109 + die("bad git namespace path \"%s\"", raw_namespace);
110 + strbuf_addch(&buf, '/');
111 + return strbuf_detach(&buf, NULL);
112 +}
113 +
114 static void setup_git_env(void)
115 {
116 git_dir = getenv(GIT_DIR_ENVIRONMENT);
@@ -111,6 +136,8 @@ static void setup_git_env(void)
136 git_graft_file = git_pathdup("info/grafts");
137 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
138 read_replace_refs = 0;
139 + namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
140 + namespace_len = strlen(namespace);
141 }
142
143 int is_bare_repository(void)
@@ -131,6 +158,20 @@ const char *get_git_dir(void)
158 return git_dir;
159 }
160
161 +const char *get_git_namespace(void)
162 +{
163 + if (!namespace)
164 + setup_git_env();
165 + return namespace;
166 +}
167 +
168 +const char *strip_namespace(const char *namespaced_ref)
169 +{
170 + if (prefixcmp(namespaced_ref, get_git_namespace()) != 0)
171 + return NULL;
172 + return namespaced_ref + namespace_len;
173 +}
174 +
175 static int git_work_tree_initialized;
176
177 /*
git.c
+16 -2
@@ -7,8 +7,8 @@
7
8 const char git_usage_string[] =
9 "git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
10 - " [-p|--paginate|--no-pager] [--no-replace-objects]\n"
11 - " [--bare] [--git-dir=<path>] [--work-tree=<path>]\n"
10 + " [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\n"
11 + " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
12 " [-c name=value] [--help]\n"
13 " <command> [<args>]";
14
@@ -127,6 +127,20 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
127 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
128 if (envchanged)
129 *envchanged = 1;
130 + } else if (!strcmp(cmd, "--namespace")) {
131 + if (*argc < 2) {
132 + fprintf(stderr, "No namespace given for --namespace.\n" );
133 + usage(git_usage_string);
134 + }
135 + setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1);
136 + if (envchanged)
137 + *envchanged = 1;
138 + (*argv)++;
139 + (*argc)--;
140 + } else if (!prefixcmp(cmd, "--namespace=")) {
141 + setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1);
142 + if (envchanged)
143 + *envchanged = 1;
144 } else if (!strcmp(cmd, "--work-tree")) {
145 if (*argc < 2) {
146 fprintf(stderr, "No directory given for --work-tree.\n" );
refs.c
+25
@@ -782,6 +782,31 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
782 return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data);
783 }
784
785 +int head_ref_namespaced(each_ref_fn fn, void *cb_data)
786 +{
787 + struct strbuf buf = STRBUF_INIT;
788 + int ret = 0;
789 + unsigned char sha1[20];
790 + int flag;
791 +
792 + strbuf_addf(&buf, "%sHEAD", get_git_namespace());
793 + if (resolve_ref(buf.buf, sha1, 1, &flag))
794 + ret = fn(buf.buf, sha1, flag, cb_data);
795 + strbuf_release(&buf);
796 +
797 + return ret;
798 +}
799 +
800 +int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
801 +{
802 + struct strbuf buf = STRBUF_INIT;
803 + int ret;
804 + strbuf_addf(&buf, "%srefs/", get_git_namespace());
805 + ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
806 + strbuf_release(&buf);
807 + return ret;
808 +}
809 +
810 int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
811 const char *prefix, void *cb_data)
812 {
refs.h
+3
@@ -36,6 +36,9 @@ extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, voi
36 extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
37 extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
38
39 +extern int head_ref_namespaced(each_ref_fn fn, void *cb_data);
40 +extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
41 +
42 static inline const char *has_glob_specials(const char *pattern)
43 {
44 return strpbrk(pattern, "?*[");