MCPcopy
hub / github.com/git-lfs/git-lfs / ExpandPath

Function ExpandPath

tools/filetools.go:170–204  ·  view source on GitHub ↗

ExpandPath returns a copy of path with any references to the current user's home directory (spelled "~"), or a named user's home directory (spelled "~user") in the path, sanitized to the calling filesystem's path separator preference. If the "expand" argument is given as true, the resolved path to

(path string, expand bool)

Source from the content-addressed store, hash-verified

168// Otherwise, the error returned will be nil, and the string returned will be
169// the expanded path.
170func ExpandPath(path string, expand bool) (string, error) {
171 if len(path) == 0 || path[0] != '~' {
172 return path, nil
173 }
174
175 var username string
176 if slash := strings.Index(path[1:], "/"); slash > -1 {
177 username = path[1 : slash+1]
178 } else {
179 username = path[1:]
180 }
181
182 var (
183 who *user.User
184 err error
185 )
186 if len(username) == 0 {
187 who, err = currentUser()
188 } else {
189 who, err = lookupUser(username)
190 }
191
192 if err != nil {
193 return "", errors.Wrap(err, tr.Tr.Get("could not find user %s", username))
194 }
195
196 homedir := who.HomeDir
197 if expand {
198 homedir, err = filepath.EvalSymlinks(homedir)
199 if err != nil {
200 return "", errors.Wrap(err, tr.Tr.Get("cannot eval symlinks for %s", homedir))
201 }
202 }
203 return filepath.Join(homedir, path[len(username)+1:]), nil
204}
205
206// ExpandConfigPath returns a copy of path expanded as with ExpandPath. If the
207// path is empty, the default path is looked up inside $XDG_CONFIG_HOME, or

Callers 5

getClientCertForHostFunction · 0.92
getCookieJarForHostFunction · 0.92
HookDirMethod · 0.92
AssertMethod · 0.85
ExpandConfigPathFunction · 0.85

Calls 2

WrapFunction · 0.92
GetMethod · 0.65

Tested by 1

AssertMethod · 0.68