MCPcopy Create free account
hub / github.com/cli/cli / copyGitPathFile

Function copyGitPathFile

script/ghmock/gitfixture.go:209–242  ·  view source on GitHub ↗
(sourceFile, destinationFile, source, destination string, mode fs.FileMode)

Source from the content-addressed store, hash-verified

207}
208
209func copyGitPathFile(sourceFile, destinationFile, source, destination string, mode fs.FileMode) error {
210 if strings.HasSuffix(filepath.ToSlash(sourceFile), "/objects/info/alternates") {
211 data, err := relocateGitAlternates(sourceFile, source, destination)
212 if err != nil {
213 return err
214 }
215 return os.WriteFile(destinationFile, data, mode)
216 }
217 data, err := os.ReadFile(sourceFile)
218 if err != nil {
219 return err
220 }
221 line := strings.TrimSuffix(string(data), "\n")
222 prefix := ""
223 if after, ok := strings.CutPrefix(line, "gitdir: "); ok {
224 prefix, line = "gitdir: ", after
225 }
226 if filepath.IsAbs(line) {
227 // Git on macOS may record /private/var while TempDir returns /var.
228 resolved, err := filepath.EvalSymlinks(line)
229 if err != nil {
230 return fmt.Errorf("relocating Git path %s: %w", sourceFile, err)
231 }
232 if !pathWithin(source, resolved) {
233 return fmt.Errorf("Git path %s points outside the fixture: %s", sourceFile, line)
234 }
235 rel, err := filepath.Rel(source, resolved)
236 if err != nil {
237 return err
238 }
239 data = []byte(prefix + filepath.Join(destination, rel) + "\n")
240 }
241 return os.WriteFile(destinationFile, data, mode)
242}
243
244func relocateGitAlternates(path, source, destination string) ([]byte, error) {
245 data, err := os.ReadFile(path)

Callers 1

copyGitTreeFunction · 0.85

Calls 5

relocateGitAlternatesFunction · 0.85
pathWithinFunction · 0.85
WriteFileMethod · 0.80
JoinMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected