跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

bare.go

83 行 · 2.8 KB历史下载
1package gitguard
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "time"
9
10 "github.com/sylvinus/agent-vm/internal/gitx"
11 "github.com/sylvinus/agent-vm/internal/version"
12)
13
14// A bare repository has no name for readonlyNames to protect: a folder with
15// HEAD, objects/ and refs/ is a repository to git, found by the same upward
16// search from the current directory as a .git. Its config then applies, and
17// some of it names commands git runs (core.pager on `git log`). The VM can
18// create such a folder anywhere in a share. safe.bareRepository=explicit (git
19// 2.38+) makes git use one only when --git-dir or GIT_DIR names it.
20
21// Bare-repository states.
22const (
23 BareOK = "ok"
24 BareUnset = "unset"
25 BareOld = "old" // git before 2.38, which ignores the setting
26 BareNoGit = "nogit"
27 BareUnavailable = "unavailable"
28)
29
30// BareState is whether git on this machine ignores bare repositories it is
31// not told about. Read from /, outside any repository: git only honours the
32// setting from the system and global config.
33func BareState(ctx context.Context) string {
34 available, err := gitx.Available(ctx)
35 if err != nil {
36 return BareUnavailable
37 }
38 if !available {
39 return BareNoGit
40 }
41 ctx, cancel := context.WithTimeout(ctx, gitTimeout)
42 defer cancel()
43 out, err := gitx.Untrusted(ctx, "--version").Output()
44 if err != nil {
45 return BareUnavailable
46 }
47 v := strings.TrimPrefix(strings.TrimSpace(string(out)), "git version ")
48 v, _, _ = strings.Cut(v, " ")
49 // Git's release candidates (2.38.0-rc1) are not semver prereleases.
50 v, _, _ = strings.Cut(v, "-")
51 if !version.AtLeast(v, "2.38.0") {
52 return BareOld
53 }
54 // gitx.Untrusted sets the value on the command line: that scope is not
55 // the user's, the last of the others is.
56 cmd := gitx.Untrusted(ctx, "config", "--show-scope", "--get-all", "safe.bareRepository")
57 // Windows needs a volume-qualified directory. The system/user drive root
58 // also keeps this global-config query outside the shared project.
59 cmd.Dir = string(filepath.Separator)
60 if home, err := os.UserHomeDir(); err == nil {
61 cmd.Dir = filepath.VolumeName(home) + string(filepath.Separator)
62 }
63 cmd.WaitDelay = 2 * time.Second
64 out, _ = cmd.Output()
65 value := ""
66 for _, l := range strings.Split(string(out), "\n") {
67 if scope, v, ok := strings.Cut(l, "\t"); ok && scope != "command" {
68 value = strings.TrimSpace(v)
69 }
70 }
71 if value == "explicit" {
72 return BareOK
73 }
74 return BareUnset
75}
76
77// BareHint is why the setting matters and how to set it, one paragraph per
78// line.
79const BareHint = `Git treats any folder with HEAD, objects/ and refs/ as a repository, even without .git, and runs commands its config names (on ` + "`git log`" + `, for one). A VM could create one in your projects, and the .git protection does not cover it.
80
81This makes git ignore such folders unless named with --git-dir:
82 git config --global safe.bareRepository explicit
83`