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

Function relocateGitAlternates

script/ghmock/gitfixture.go:244–282  ·  view source on GitHub ↗
(path, source, destination string)

Source from the content-addressed store, hash-verified

242}
243
244func relocateGitAlternates(path, source, destination string) ([]byte, error) {
245 data, err := os.ReadFile(path)
246 if err != nil {
247 return nil, err
248 }
249 objects := filepath.Dir(filepath.Dir(path))
250 relative, err := filepath.Rel(source, objects)
251 if err != nil {
252 return nil, err
253 }
254 copiedObjects := filepath.Join(destination, relative)
255 lines := strings.Split(string(data), "\n")
256 for i, line := range lines {
257 if line == "" {
258 continue
259 }
260 target := line
261 if !filepath.IsAbs(target) {
262 target = filepath.Join(objects, target)
263 }
264 target, err = filepath.EvalSymlinks(target)
265 if err != nil {
266 return nil, fmt.Errorf("relocating Git alternate %s: %w", path, err)
267 }
268 if !pathWithin(source, target) {
269 return nil, fmt.Errorf("Git alternate %s points outside the fixture: %s", path, line)
270 }
271 relative, err = filepath.Rel(source, target)
272 if err != nil {
273 return nil, err
274 }
275 lines[i], err = filepath.Rel(copiedObjects, filepath.Join(destination, relative))
276 if err != nil {
277 return nil, err
278 }
279 lines[i] = filepath.ToSlash(lines[i])
280 }
281 return []byte(strings.Join(lines, "\n")), nil
282}

Callers 2

portableGitPathFunction · 0.85
copyGitPathFileFunction · 0.85

Calls 3

pathWithinFunction · 0.85
JoinMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected