【发布时间】:2021-12-25 15:11:17
【问题描述】:
我有一个裸存储库,我需要在其中添加和提交一组文件。据我了解,将文件添加到索引需要 worktree。在命令行上使用git,我会将git-dir 选项设置为指向裸目录,同时将work-tree 选项设置为指向要添加到索引的文件所在的工作树。像这样:
$ git --git-dir /path/to/.git --work-tree /path/to/worktree add ...
值得一提的是,“.git”目录不是,也不能简单地命名为“.git”。它实际上是一个“自定义”“.git”目录。喜欢git --git-dir /path/to/.notgit ...。
我尝试设置 core.worktree 配置选项。但是,将core.bare 设置为true 会导致致命错误。都来自命令行:
$ git --git-dir /path/to/.notgit config core.worktree /path/to/worktree
$ git --git-dir /path/to/.notgit add ...
warning: core.bare and core.worktree do not make sense
fatal: unable to set up work tree using invalid config
并使用go-git:
r, err := git.PlainOpen("/path/to/.notgit")
panicOnError(err)
c, err := r.Config()
panicOnError(err)
fmt.Println(c.Core.IsBare) // true
c.Core.Worktree = "/path/to/worktree"
err = r.SetConfig(c)
panicOnError(err)
_, err = r.Worktree() // panic: worktree not available in a bare repository
panicOnError(err)
我的一个想法是依靠git.PlainOpenWithOptions 函数,希望我能够提供一个工作树作为选项。但是,看看 git.PlainOpenOptions 结构类型,这很快就崩溃了。
type PlainOpenOptions struct {
// DetectDotGit defines whether parent directories should be
// walked until a .git directory or file is found.
DetectDotGit bool
// Enable .git/commondir support (see https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt).
// NOTE: This option will only work with the filesystem storage.
EnableDotGitCommonDir bool
}
如何模仿git --work-tree ... 和go-git?
编辑1:解释“.git”不完全命名为“.git”。
【问题讨论】:
-
您可以使用带有参数的
PlainOpenWithOptions构造函数,正如您在我的回答here中看到的那样