【问题标题】:how do i use git namespaces locally?我如何在本地使用 git 命名空间?
【发布时间】:2014-08-25 04:56:38
【问题描述】:

我有一个有趣的用例,我想通过GitLab 共享存储库——但是我们公司每个用户的存储库有限,所以我必须通过隐私来分配这些存储库(即,而不是存储库1 用于项目 1,我有项目 1 和 2 → repo 1 用于团队 1,项目 3 和 4 → repo 2 用于团队 2)。

最初我打算在分支名称中创建伪命名空间,例如project1-branch1project2-branch1project2-branch2 — 但是我随后了解到 git 包含 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

在推送到远程仓库之前是否无法在本地查看命名空间?或者如果它可以在本地使用 git-remote-ext 魔术,如 git clone ext::'git --namespace=foo %s /tmp/prefixed.git',那么为什么不简单地使用 git --namespace ordinary-git-command

如果我误解了 git 命名空间的目的,请提前抱歉——我只是想在与我的同事分享之前确信这将起作用,否则我只会使用分支名称作为一种伪命名空间(不太优雅,但它会工作)。我已经查看了以下帖子,但并没有真正了解 git 命名空间的功能:

【问题讨论】:

  • 不清楚,为什么要在同一个代码库上共享项目 1 和 2?
  • Gitlab 有一个工作组的概念和用户的角色,为什么你不能在你的上下文中应用?
  • 虽然我可以承认我的用例被误导了,但我仍然想了解如何在本地分离 gitref 命名空间,如果可能的话——如果不是,为什么,因为 git 具有声称的命名空间功能做到这一点。

标签: git version-control namespaces gitlab collaboration


【解决方案1】:

Git 命名空间仅适用于远程存储库,而不是本地 (https://github.com/git/git/commit/6b01ecfe22f0b6ccb9665cd8d85d78a381fecffc)。大多数使用 git 命名空间的操作似乎都适用于通过 git-upload-pack 和 git-receive-pack 函数的操作。 这就是为什么文档建议您是否想在本地对其进行测试以假装它认为您是从远程机器中提取的,例如:git clone ext::'git --namespace=foo %s /tmp/prefixed.git'

这样的命令

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

基本上什么都不做。似乎有任何影响的唯一操作是克隆/获取/拉取和推送。

为了以您希望的方式利用命名空间,您必须设置自己的 git 后端,该后端能够将 URL 参数转换为 GIT_NAMESPACE 变量并将其传递给 git-http-backend 或类似的东西. The documentation recommends the following in your apache configuration:

在单个存储库中为来自不同 gitnamespaces 的多个存储库提供服务:

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

还请注意,本文档没有说明的是,这里发生的事情是从 URL 中提取 GIT_NAMESPACE 变量并设置 git-http-backend 期望的环境变量。即http://myserver.com/git/namespace/repository.git。 'storage.git' 部分是一个错字,不应该在那里。我应该提交一个文档补丁。

以下是创建此功能的大部分提交。 https://github.com/git/git/commits/398dd4bd039680ba98497fbedffa415a43583c16?author=joshtriplett

【讨论】:

  • 刚刚意识到我从未接受过您的出色回答,@Mixologic。迟到总比没有好!
【解决方案2】:

由于 Gitlab 本质上是 0 美元投资的免费软件,因此认为有理由根据许可证限制每个用户的存储库数量是愚蠢的。 Gitlab 的家伙确实出售企业许可证,但与社区版相比,即使这也没有说明“无限回购”,因此这不被视为卖点。我假设这两个版本都允许您喜欢任意数量的存储库。

您是否要求您的 Gitlab 管理员增加您帐户的存储库数量?或者您是否甚至要求将团队使用的存储库从您的个人帐户中移出?如果最简单的解决方案是去请求更多不花一分钱的存储库,那么尝试发明一个基于命名空间或分支的解决方案是完全疯狂的。

【讨论】:

  • “疯狂”的判断有点非技术性,尽管您有一个有效的观点,我应该要求我的系统管理员进行升级。话虽如此,我可以修改我用我的问题阐述的理由,但在阅读您的回答后仍然不明白为什么命名空间似乎无法在本地工作。
猜你喜欢
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
相关资源
最近更新 更多