Skip to content

Commit a1bea2c

Browse files
joshtriplettgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jamey Sharp <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b3cfc40 commit a1bea2c

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ static inline enum object_type object_type(unsigned int mode)
379379
}
380380

381381
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
382+
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
382383
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
383384
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
384385
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
@@ -419,6 +420,8 @@ extern char *get_object_directory(void);
419420
extern char *get_index_file(void);
420421
extern char *get_graft_file(void);
421422
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);
422425
extern const char *get_git_work_tree(void);
423426
extern const char *read_gitfile_gently(const char *path);
424427
extern void set_git_work_tree(const char *tree);

contrib/completion/git-completion.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,7 @@ _git ()
26392639
--exec-path
26402640
--html-path
26412641
--work-tree=
2642+
--namespace=
26422643
--help
26432644
"
26442645
;;

environment.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* are.
99
*/
1010
#include "cache.h"
11+
#include "refs.h"
1112

1213
char git_default_email[MAX_GITNAME];
1314
char git_default_name[MAX_GITNAME];
@@ -65,6 +66,9 @@ int core_preload_index = 0;
6566
char *git_work_tree_cfg;
6667
static char *work_tree;
6768

69+
static const char *namespace;
70+
static size_t namespace_len;
71+
6872
static const char *git_dir;
6973
static char *git_object_dir, *git_index_file, *git_graft_file;
7074

@@ -86,6 +90,27 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
8690
NULL
8791
};
8892

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+
89114
static void setup_git_env(void)
90115
{
91116
git_dir = getenv(GIT_DIR_ENVIRONMENT);
@@ -111,6 +136,8 @@ static void setup_git_env(void)
111136
git_graft_file = git_pathdup("info/grafts");
112137
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
113138
read_replace_refs = 0;
139+
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
140+
namespace_len = strlen(namespace);
114141
}
115142

116143
int is_bare_repository(void)
@@ -131,6 +158,20 @@ const char *get_git_dir(void)
131158
return git_dir;
132159
}
133160

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+
134175
static int git_work_tree_initialized;
135176

136177
/*

git.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
const char git_usage_string[] =
99
"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"
1212
" [-c name=value] [--help]\n"
1313
" <command> [<args>]";
1414

@@ -127,6 +127,20 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
127127
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
128128
if (envchanged)
129129
*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;
130144
} else if (!strcmp(cmd, "--work-tree")) {
131145
if (*argc < 2) {
132146
fprintf(stderr, "No directory given for --work-tree.\n" );

refs.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,31 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
782782
return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data);
783783
}
784784

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+
785810
int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
786811
const char *prefix, void *cb_data)
787812
{

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, voi
3636
extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
3737
extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
3838

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+
3942
static inline const char *has_glob_specials(const char *pattern)
4043
{
4144
return strpbrk(pattern, "?*[");

0 commit comments

Comments
 (0)