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

Function portableGitPath

script/ghmock/archive.go:157–181  ·  view source on GitHub ↗
(path, root string)

Source from the content-addressed store, hash-verified

155}
156
157func portableGitPath(path, root string) ([]byte, error) {
158 if strings.HasSuffix(filepath.ToSlash(path), "/objects/info/alternates") {
159 return relocateGitAlternates(path, root, root)
160 }
161 data, err := os.ReadFile(path)
162 if err != nil {
163 return nil, err
164 }
165 line := strings.TrimSuffix(string(data), "\n")
166 prefix := ""
167 if after, ok := strings.CutPrefix(line, "gitdir: "); ok {
168 prefix, line = "gitdir: ", after
169 }
170 if !filepath.IsAbs(line) {
171 return data, nil
172 }
173 if !pathWithin(root, line) {
174 return nil, fmt.Errorf("Git path %s points outside the export: %s", path, line)
175 }
176 rel, err := filepath.Rel(filepath.Dir(path), line)
177 if err != nil {
178 return nil, err
179 }
180 return []byte(prefix + filepath.ToSlash(rel) + "\n"), nil
181}

Callers 1

writeTarGzipFunction · 0.85

Calls 3

relocateGitAlternatesFunction · 0.85
pathWithinFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected