MCPcopy Index your code
hub / github.com/jetify-com/devbox / cloneAndRead

Method cloneAndRead

internal/plugin/git.go:93–145  ·  view source on GitHub ↗
(subpath string)

Source from the content-addressed store, hash-verified

91}
92
93func (p *gitPlugin) cloneAndRead(subpath string) ([]byte, error) {
94 tempDir, err := os.MkdirTemp("", "devbox-git-plugin-*")
95 if err != nil {
96 return nil, fmt.Errorf("failed to create temp directory: %w", err)
97 }
98 defer os.RemoveAll(tempDir)
99
100 baseURL := p.getBaseURL()
101
102 cloneArgs := []string{"clone"}
103 if p.ref.Ref != "" {
104 cloneArgs = append(cloneArgs, "--depth", "1", "--branch", p.ref.Ref)
105 } else if p.ref.Rev == "" {
106 cloneArgs = append(cloneArgs, "--depth", "1")
107 }
108 cloneArgs = append(cloneArgs, baseURL, tempDir)
109 cloneCmd := exec.Command("git", cloneArgs...)
110
111 if isSSHURL(baseURL) {
112 gitSSHCommand := os.Getenv("GIT_SSH_COMMAND")
113 if gitSSHCommand == "" {
114 gitSSHCommand = "ssh -o StrictHostKeyChecking=accept-new"
115 }
116 cloneCmd.Env = append(os.Environ(), "GIT_SSH_COMMAND="+gitSSHCommand)
117 }
118
119 output, err := cloneCmd.CombinedOutput()
120 if err != nil {
121 return nil, fmt.Errorf("failed to clone repository %s: %w\nOutput: %s", p.ref.URL, err, string(output))
122 }
123
124 if p.ref.Rev != "" {
125 checkoutCmd := exec.Command("git", "checkout", p.ref.Rev)
126 checkoutCmd.Dir = tempDir
127 output, err := checkoutCmd.CombinedOutput()
128 if err != nil {
129 return nil, fmt.Errorf("failed to checkout revision %s: %w\nOutput: %s", p.ref.Rev, err, string(output))
130 }
131 }
132
133 // Read file from repository root or specified directory
134 filePath := filepath.Join(tempDir, subpath)
135 if p.ref.Dir != "" {
136 filePath = filepath.Join(tempDir, p.ref.Dir, subpath)
137 }
138
139 content, err := os.ReadFile(filePath)
140 if err != nil {
141 return nil, fmt.Errorf("failed to read file %s: %w", filePath, err)
142 }
143
144 return content, nil
145}
146
147// isSSHURL checks if the given URL is an SSH URL.
148// SSH URLs can be in formats:

Callers 1

FileContentMethod · 0.95

Calls 4

getBaseURLMethod · 0.95
isSSHURLFunction · 0.85
CommandMethod · 0.80
CombinedOutputMethod · 0.80

Tested by

no test coverage detected