From 85a543afac6d7b1cf14ae94deb8d77b7ea8b2053 Mon Sep 17 00:00:00 2001 From: cellarspoon Date: Sat, 11 Dec 2021 20:11:59 +0100 Subject: [PATCH] fix: maybe more robust gitignore checks --- pkg/git/read.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pkg/git/read.go b/pkg/git/read.go index 1d9f7acd..a4b4cbaf 100644 --- a/pkg/git/read.go +++ b/pkg/git/read.go @@ -51,7 +51,10 @@ func IsClean(recipeName string) (bool, error) { if err != nil { return false, err } - worktree.Excludes = append(patterns, worktree.Excludes...) + + if len(patterns) > 0 { + worktree.Excludes = append(patterns, worktree.Excludes...) + } status, err := worktree.Status() if err != nil { @@ -95,9 +98,12 @@ func parseGitConfig() (*gitConfigPkg.Config, error) { } globalGitConfig := filepath.Join(usr.HomeDir, ".gitconfig") - if _, err := os.Stat(globalGitConfig); os.IsNotExist(err) { - logrus.Debugf("no %s exists, not reading any global git ignore config", globalGitConfig) - return cfg, nil + if _, err := os.Stat(globalGitConfig); err != nil { + if os.IsNotExist(err) { + logrus.Debugf("no %s exists, not reading any global git ignore config", globalGitConfig) + return cfg, nil + } + return cfg, err } b, err := ioutil.ReadFile(globalGitConfig) @@ -126,17 +132,26 @@ func getExcludesFile(cfg *gitConfigPkg.Config) string { } func parseExcludesFile(excludesfile string) ([]gitignore.Pattern, error) { + var ps []gitignore.Pattern + excludesfile, err := expandTilde(excludesfile) if err != nil { return nil, err } + if _, err := os.Stat(excludesfile); err != nil { + if os.IsNotExist(err) { + logrus.Debugf("no %s exists, skipping reading ignore paths", excludesfile) + return ps, nil + } + return ps, err + } + data, err := ioutil.ReadFile(excludesfile) if err != nil { return nil, err } - var ps []gitignore.Pattern for _, s := range strings.Split(string(data), "\n") { if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 { ps = append(ps, gitignore.ParsePattern(s, nil)) @@ -150,11 +165,13 @@ func expandTilde(path string) (string, error) { if !strings.HasPrefix(path, "~") { return path, nil } + var paths []string u, err := user.Current() if err != nil { return "", err } + for _, p := range strings.Split(path, string(filepath.Separator)) { if p == "~" { paths = append(paths, u.HomeDir) @@ -162,5 +179,6 @@ func expandTilde(path string) (string, error) { paths = append(paths, p) } } + return "/" + filepath.Join(paths...), nil }