Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i use git namespaces locally?

i've got an interesting use-case in which i want to share repositories via GitLab — but our company's got limited repos per user, and so i've got to ration those out by privacy (i.e. instead of repo 1 for project 1, i've got projects 1 & 2 → repo 1 for team 1, and projects 3 & 4 → repo 2 for team 2).

initially i was going to create pseudo-namespaces in the branch names, e.g. project1-branch1, project2-branch1, project2-branch2 — however i subsequently learned that git includes namespace functionality that is supposed to separate different reference namespaces while sharing one object store. i tried to test this locally by committing different branches to different namespaces, however i still see all branches in any (or no!) namespace:

$ git init .
Initialized empty Git repository in ~/tmp/test/.git/

$ git --namespace test1 checkout --orphan test1
Switched to a new branch 'test1'

$ touch test1

$ git --namespace test1 add -- test1

$ git --namespace test1 commit -m test1
[test1 (root-commit) 27f9d70] test1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test1

$ git --namespace test2 checkout --orphan test2
Switched to a new branch 'test2'

$ touch test2

$ git --namespace test2 add -- test2

$ git --namespace test2 commit -m test2
[test2 (root-commit) 4f0f7c5] test2
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test1
 create mode 100644 test2

$ git log --graph
* commit 4f0f7c555d3c607d97829263a30170cc431c1d01
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:39 2014 -0500
      test2

$ git log --all --graph
* commit 4f0f7c555d3c607d97829263a30170cc431c1d01
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:39 2014 -0500
      test2

* commit 27f9d703758ae401eb77e7e15d75ac863f296291
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:17 2014 -0500
      test1

$ git --namespace test1 log --all --graph
* commit 4f0f7c555d3c607d97829263a30170cc431c1d01
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:39 2014 -0500
      test2

* commit 27f9d703758ae401eb77e7e15d75ac863f296291
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:17 2014 -0500
      test1

$ git --namespace test2 log --all --graph
* commit 4f0f7c555d3c607d97829263a30170cc431c1d01
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:39 2014 -0500

      test2

* commit 27f9d703758ae401eb77e7e15d75ac863f296291
  Author: RubyTuesdayDONO
  Date:   Thu Jul 3 16:06:17 2014 -0500
      test1

# separate namespace should prevent conflict (but doesn't)
$ git --namespace test3 checkout --orphan test1 
fatal: A branch named 'test1' already exists.

# should include refs/namespaces (but doesn't)
$ tree -a
.
├── .git
│   ├── COMMIT_EDITMSG
│   ├── config
│   ├── description
│   ├── HEAD
│   ├── hooks
│   ├── index
│   ├── info
│   │   └── exclude
│   ├── logs
│   │   ├── HEAD
│   │   └── refs
│   │       └── heads
│   │           ├── test1
│   │           └── test2
│   ├── objects
│   │   ├── 18
│   │   │   └── c152442134ca652c83b111b6063c9b75f9157c
│   │   ├── 27
│   │   │   └── f9d703758ae401eb77e7e15d75ac863f296291
│   │   ├── 4f
│   │   │   └── 0f7c555d3c607d97829263a30170cc431c1d01
│   │   ├── e0
│   │   │   └── f402da78bd414bdd926713d2b54c246432adc5
│   │   ├── e6
│   │   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│   │   ├── info
│   │   └── pack
│   └── refs
│       ├── heads
│       │   ├── test1
│       │   └── test2
│       └── tags
├── test1
└── test2

17 directories, 27 files

is it not possible to see the namespaces locally before pushing to a remote repo? or if it works locally with git-remote-ext magick like git clone ext::'git --namespace=foo %s /tmp/prefixed.git', then why not simply with git --namespace ordinary-git-command?

sorry in advance if i'm misunderstanding the purpose of git namespaces — i just want to feel assured that this will work before sharing with my colleagues, else i'll just use the branch names as a kind of pseudo-namespace instead (less elegant, but it'll work). i've reviewed the following posts without really understanding how git namespaces function:

  • https://softwareengineering.stackexchange.com/q/170345/45261
  • How to clone a git repository namespace
like image 761
RubyTuesdayDONO Avatar asked Jul 03 '14 22:07

RubyTuesdayDONO


1 Answers

Git namespaces is only intended to work with remote repos, not locally (https://github.com/git/git/commit/6b01ecfe22f0b6ccb9665cd8d85d78a381fecffc). Most of the operations that work using git namespaces appear to work on operations that go through the git-upload-pack and git-receive-pack functions. Thats why the documenation suggests if you want to test it locally to fake it into thinking you're pulling from a remote machine eg: git clone ext::'git --namespace=foo %s /tmp/prefixed.git'

so commands like

git --namespace foo add
git --namespace foo commit
git --namespace foo branch

All essentially do nothing. The only operations that appear to have any effect are clone/fetch/pull and push.

In order to leverage namespaces the way you're hoping, you'd have to set up your own git backend that is capable of translating URL arguments into the GIT_NAMESPACE variable and pass it along to git-http-backend or something similar. The documentation recommends the following in your apache configuration:

To serve multiple repositories from different gitnamespaces in a single repository:

SetEnvIf Request_URI "^/git/([^/]*)" GIT_NAMESPACE=$1
ScriptAliasMatch ^/git/[^/]*(.*) /usr/libexec/git-core/git-http-backend/storage.git$1

Also note that what this documentation does not say, is that what is going on here is extracting the GIT_NAMESPACE variable from a URL and setting an environment variable that git-http-backend is expecting. i.e. http://myserver.com/git/namespace/repository.git. The 'storage.git' part is a typo and should not be there. I should submit a documentation patch.

Here's most of the commits that created this feature. https://github.com/git/git/commits/398dd4bd039680ba98497fbedffa415a43583c16?author=joshtriplett

like image 52
Mixologic Avatar answered Sep 26 '22 06:09

Mixologic