diff --git a/config/config.go b/config/config.go index 5b8a9d07..bfd3fbc7 100644 --- a/config/config.go +++ b/config/config.go @@ -291,11 +291,12 @@ func (c *Configuration) SetEndpointAccess(e Endpoint, authType string) { } } -func (c *Configuration) FetchIncludePaths() []string { +func (c *Configuration) GlobalFetchIncludePaths() []string { c.loadGitConfig() return c.fetchIncludePaths } -func (c *Configuration) FetchExcludePaths() []string { + +func (c *Configuration) GlobalFetchExcludePaths() []string { c.loadGitConfig() return c.fetchExcludePaths } @@ -587,20 +588,68 @@ func (c *Configuration) readGitConfig(output string, uniqRemotes map[string]bool c.gitConfig[key] = value - if len(keyParts) == 2 && keyParts[0] == "lfs" && keyParts[1] == "fetchinclude" { - for _, inc := range strings.Split(value, ",") { - inc = strings.TrimSpace(inc) - c.fetchIncludePaths = append(c.fetchIncludePaths, inc) - } - } else if len(keyParts) == 2 && keyParts[0] == "lfs" && keyParts[1] == "fetchexclude" { - for _, ex := range strings.Split(value, ",") { - ex = strings.TrimSpace(ex) - c.fetchExcludePaths = append(c.fetchExcludePaths, ex) + if len(keyParts) == 2 && keyParts[0] == "lfs" { + switch keyParts[1] { + case "fetchinclude": + c.fetchIncludePaths = cleanPaths(value, ",") + case "fetchexclude": + c.fetchExcludePaths = cleanPaths(value, ",") } } } } +// FetchIncludeExcludePaths is a convenience method for returning both the +// include and exclude path for a given set of arguments by delegating into the +// FetchIncludePaths and FetchExcludePaths functions respectively. +func (c *Configuration) FetchIncludeExcludePaths(include, exclude string) (includes, exclues []string) { + return c.FetchIncludePaths(include), c.FetchExcludePaths(exclude) +} + +// FetchIncludePaths returns the comma seperated include paths contained in the +// given argument, or the GlobalFetchIncludePaths, if none were present. +func (c *Configuration) FetchIncludePaths(args string) (includes []string) { + return cleanPathsDefault(args, ",", c.GlobalFetchIncludePaths()) +} + +// FetchExcludePaths returns the comma seperated include paths contained in the +// given argument, or the GlobalFetchExcludePaths, if none were present. +func (c *Configuration) FetchExcludePaths(args string) (excludes []string) { + return cleanPathsDefault(args, ",", c.GlobalFetchExcludePaths()) +} + +// cleanPathsDefault cleans the paths contained in the given `paths` argument +// delimited by the `delim`, argument. If an empty set is returned from that +// split, then the fallback argument is returned instead. +func cleanPathsDefault(paths, delim string, fallback []string) []string { + cleaned := cleanPaths(paths, delim) + if len(cleaned) == 0 { + return fallback + } + + return cleaned +} + +// cleanPaths splits the given `paths` argument by the delimiter argument, and +// then "cleans" that path according to the filepath.Clean function (see +// https://golang.org/pkg/file/filepath#Clean). +func cleanPaths(paths, delim string) (cleaned []string) { + // If paths is an empty string, splitting it will yield [""], which will + // become the filepath ".". To avoid this, bail out if trimmed paths + // argument is empty. + if paths = strings.TrimSpace(paths); len(paths) == 0 { + return + } + + for _, part := range strings.Split(paths, delim) { + part = strings.TrimSpace(part) + + cleaned = append(cleaned, filepath.Clean(part)) + } + + return cleaned +} + func keyIsUnsafe(key string) bool { for _, safe := range safeKeys { if safe == key { diff --git a/lfs/lfs.go b/lfs/lfs.go index 530e8f30..5613d1d1 100644 --- a/lfs/lfs.go +++ b/lfs/lfs.go @@ -84,11 +84,11 @@ func Environ() []string { fmt.Sprintf("AccessDownload=%s", config.Config.Access("download")), fmt.Sprintf("AccessUpload=%s", config.Config.Access("upload")), ) - if len(config.Config.FetchExcludePaths()) > 0 { - env = append(env, fmt.Sprintf("FetchExclude=%s", strings.Join(config.Config.FetchExcludePaths(), ", "))) + if len(config.Config.GlobalFetchExcludePaths()) > 0 { + env = append(env, fmt.Sprintf("FetchExclude=%s", strings.Join(config.Config.GlobalFetchExcludePaths(), ", "))) } - if len(config.Config.FetchIncludePaths()) > 0 { - env = append(env, fmt.Sprintf("FetchInclude=%s", strings.Join(config.Config.FetchIncludePaths(), ", "))) + if len(config.Config.GlobalFetchIncludePaths()) > 0 { + env = append(env, fmt.Sprintf("FetchInclude=%s", strings.Join(config.Config.GlobalFetchIncludePaths(), ", "))) } for _, ext := range config.Config.Extensions() { env = append(env, fmt.Sprintf("Extension[%d]=%s", ext.Priority, ext.Name))