MCPcopy
hub / github.com/cli/cli / Join

Method Join

internal/safepaths/absolute.go:38–57  ·  view source on GitHub ↗

Join an absolute path with elements to create a new Absolute path, or error. A PathTraversalError will be returned if the joined path would traverse outside of the base Absolute path. Note that this does not handle symlinks.

(elem ...string)

Source from the content-addressed store, hash-verified

36// A PathTraversalError will be returned if the joined path would traverse outside of
37// the base Absolute path. Note that this does not handle symlinks.
38func (a Absolute) Join(elem ...string) (Absolute, error) {
39 joinedAbsolutePath, err := ParseAbsolute(filepath.Join(append([]string{a.path}, elem...)...))
40 if err != nil {
41 return Absolute{}, fmt.Errorf("failed to parse joined path: %w", err)
42 }
43
44 isSubpath, err := joinedAbsolutePath.isSubpathOf(a)
45 if err != nil {
46 return Absolute{}, err
47 }
48
49 if !isSubpath {
50 return Absolute{}, PathTraversalError{
51 Base: a,
52 Elems: elem,
53 }
54 }
55
56 return joinedAbsolutePath, nil
57}
58
59func (a Absolute) isSubpathOf(dir Absolute) (bool, error) {
60 relativePath, err := filepath.Rel(dir.path, a.path)

Callers 15

mainFunction · 0.80
shellInspectFunction · 0.80
TestClientRemotesFunction · 0.80
TestClientCurrentBranchFunction · 0.80
TestClientShowRefsFunction · 0.80
TestClientConfigFunction · 0.80
TestClientCommitsFunction · 0.80
TestClientDeleteLocalTagFunction · 0.80

Calls 3

ParseAbsoluteFunction · 0.85
isSubpathOfMethod · 0.80
ErrorfMethod · 0.65

Tested by 15

TestClientRemotesFunction · 0.64
TestClientCurrentBranchFunction · 0.64
TestClientShowRefsFunction · 0.64
TestClientConfigFunction · 0.64
TestClientCommitsFunction · 0.64
TestClientDeleteLocalTagFunction · 0.64
TestClientHasLocalBranchFunction · 0.64