MCPcopy Create free account
hub / github.com/coder/envbuilder / ShallowCloneRepo

Function ShallowCloneRepo

git/git.go:143–171  ·  view source on GitHub ↗

ShallowCloneRepo will clone the repository at the given URL into the given path with a depth of 1. If the destination folder exists and is not empty, the clone will not be performed. The bool returned states whether the repository was cloned or not.

(ctx context.Context, logf func(string, ...any), opts CloneRepoOptions)

Source from the content-addressed store, hash-verified

141//
142// The bool returned states whether the repository was cloned or not.
143func ShallowCloneRepo(ctx context.Context, logf func(string, ...any), opts CloneRepoOptions) error {
144 opts.Depth = 1
145 opts.SingleBranch = true
146
147 if opts.Path == "" {
148 return errors.New("path is required")
149 }
150
151 // Avoid clobbering the destination.
152 if _, err := opts.Storage.Stat(opts.Path); err == nil {
153 files, err := opts.Storage.ReadDir(opts.Path)
154 if err != nil {
155 return fmt.Errorf("read dir %q: %w", opts.Path, err)
156 }
157 if len(files) > 0 {
158 return fmt.Errorf("directory %q is not empty", opts.Path)
159 }
160 }
161
162 cloned, err := CloneRepo(ctx, logf, opts)
163 if err != nil {
164 return err
165 }
166 if !cloned {
167 return errors.New("repository already exists")
168 }
169
170 return nil
171}
172
173// ReadPrivateKey attempts to read an SSH private key from path
174// and returns an ssh.Signer.

Callers 3

runFunction · 0.92
RunCacheProbeFunction · 0.92
TestShallowCloneRepoFunction · 0.92

Calls 3

CloneRepoFunction · 0.85
StatMethod · 0.65
ReadDirMethod · 0.65

Tested by 1

TestShallowCloneRepoFunction · 0.74