跳到主要内容

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

浏览文件

gitx.go

86 行 · 3.0 KB历史下载
1// Package gitx runs git on this machine in folders the VM can write.
2package gitx
3
4import (
5 "context"
6 "errors"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "time"
14)
15
16// Untrusted is git for agent-vm's own calls in a folder the VM can write.
17// The VM may have planted a repository there: a bare one, whatever the
18// user's safe.bareRepository, or a .git where it cannot be protected.
19// Command-line config is trusted where a repository's own is not:
20// safe.bareRepository=explicit refuses the bare one, core.fsmonitor=false
21// stops the command a repository's config names from running on commands
22// that read the index, and --no-pager the pager its config would start.
23func Untrusted(ctx context.Context, args ...string) *exec.Cmd {
24 cmd := exec.CommandContext(ctx, "git", append([]string{"--no-pager", "-c", "safe.bareRepository=explicit", "-c", "core.fsmonitor=false"}, args...)...)
25 if available, err := Available(ctx); err != nil {
26 cmd.Err = err
27 } else if !available {
28 cmd.Err = exec.ErrNotFound
29 }
30 return cmd
31}
32
33// Have reports whether git is installed.
34func Have() bool {
35 available, err := Available(context.Background())
36 // An unreadable installation is not proof of absence. Scans must report
37 // the failed command; they must not silently skip host Git protections.
38 return available || err != nil
39}
40
41// Available checks Apple's launcher without invoking it: running /usr/bin/git
42// on a Mac without developer tools can unexpectedly open the system installer.
43func Available(ctx context.Context) (bool, error) {
44 path, err := exec.LookPath("git")
45 if errors.Is(err, exec.ErrNotFound) {
46 return false, nil
47 }
48 if err != nil {
49 return false, err
50 }
51 resolved, err := filepath.EvalSymlinks(path)
52 if err != nil {
53 return false, err
54 }
55 if runtime.GOOS != "darwin" || resolved != "/usr/bin/git" {
56 return true, nil
57 }
58 return appleGitAvailable(ctx, "/usr/bin/xcode-select")
59}
60
61func appleGitAvailable(ctx context.Context, selector string) (bool, error) {
62 ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
63 defer cancel()
64 cmd := exec.CommandContext(ctx, selector, "--print-path")
65 cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
66 out, err := cmd.Output()
67 if err != nil {
68 var exit *exec.ExitError
69 // Only the documented no-active-tools diagnostic is absence. A broken
70 // DEVELOPER_DIR override, timeout, or other failure stays an error.
71 if os.Getenv("DEVELOPER_DIR") == "" && errors.As(err, &exit) &&
72 strings.Contains(string(exit.Stderr), "unable to get active developer directory") {
73 return false, nil
74 }
75 return false, fmt.Errorf("cannot determine whether Apple Git is installed")
76 }
77 developer := strings.TrimSpace(string(out))
78 if !filepath.IsAbs(developer) {
79 return false, fmt.Errorf("invalid developer tools directory")
80 }
81 info, err := os.Stat(filepath.Join(developer, "usr", "bin", "git"))
82 if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
83 return false, fmt.Errorf("Apple Git is unavailable in the selected developer tools")
84 }
85 return true, nil
86}