(sourceFile, destinationFile, source, destination string, mode fs.FileMode)
| 207 | } |
| 208 | |
| 209 | func 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 | |
| 244 | func relocateGitAlternates(path, source, destination string) ([]byte, error) { |
| 245 | data, err := os.ReadFile(path) |
no test coverage detected