From eb0ad570ee17ee2f9c3381cbe048d4b78084e6e6 Mon Sep 17 00:00:00 2001 From: Hydraallen Date: Tue, 15 Oct 2024 16:58:02 +0800 Subject: [PATCH 1/6] fix: not ignore gitea --- pkg/healthcheck/forbidden.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index 2c58ab9..7cd97c1 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -45,7 +45,7 @@ func getForbiddens(root string, fileList []string, localList string) ([]string, } if info.IsDir() { - if info.Name() == ".git" || info.Name() == ".gitea" || info.Name() == "ci" || (localList != "" && inString(info.Name(), dirs)) { + if info.Name() == ".git" || (localList != "" && inString(info.Name(), dirs)) { return filepath.SkipDir } } else { -- 2.30.2 From 84e168dad67660d47ed616ea50649a2ffb55c961 Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 17 Oct 2024 14:45:51 +0800 Subject: [PATCH 2/6] refractor(healthcheck/forbidden): use go-gitignore package --- cmd/repo-health-checker/main.go | 2 +- go.mod | 2 ++ go.sum | 4 +++ pkg/healthcheck/forbidden.go | 51 ++++++++------------------------- 4 files changed, 19 insertions(+), 40 deletions(-) diff --git a/cmd/repo-health-checker/main.go b/cmd/repo-health-checker/main.go index be152f8..d4ee844 100644 --- a/cmd/repo-health-checker/main.go +++ b/cmd/repo-health-checker/main.go @@ -59,7 +59,7 @@ func main() { if err != nil { fmt.Printf("### Repo Size Check Failed:\n%s\n", err.Error()) } - err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist, *localList) + err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist) if err != nil { fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error()) } diff --git a/go.mod b/go.mod index fdeae04..6c81d72 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.1 require ( github.com/criyle/go-judge v1.8.5 + github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 github.com/go-git/go-git/v5 v5.12.0 github.com/jinzhu/copier v0.4.0 github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 @@ -21,6 +22,7 @@ require ( github.com/creack/pty v1.1.21 // indirect github.com/criyle/go-sandbox v0.10.4 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect + github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/structs v1.1.0 // indirect diff --git a/go.sum b/go.sum index 6f4d886..614bbf5 100644 --- a/go.sum +++ b/go.sum @@ -23,9 +23,13 @@ github.com/criyle/go-sandbox v0.10.4 h1:EHJrJj5V/VSrjm1Y0ZJAea5zPASoOtn1CPZRzhTU github.com/criyle/go-sandbox v0.10.4/go.mod h1:sYJUuTmJ72Jilkc1/PO7eDdpJq3rOZ55o8MxzP80vw0= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q= +github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index 7cd97c1..53e96a1 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -1,67 +1,40 @@ package healthcheck import ( - "bufio" "fmt" "log/slog" "os" "path/filepath" - "regexp" "strings" + + "github.com/denormal/go-gitignore" ) // getForbiddens retrieves a list of forbidden files in the specified root directory. // It searches for files that do not match the specified regex patterns in the given file list. -func getForbiddens(root string, fileList []string, localList string) ([]string, error) { +func getForbiddens(root string, fileList []string) ([]string, error) { var matches []string - var regexList []*regexp.Regexp - regexList, err := getRegex(fileList) + ignore, err := gitignore.NewFromFile("./.gitignore") if err != nil { return nil, err } - var dirs []string - - if localList != "" { - file, err := os.Open(localList) - if err != nil { - return nil, fmt.Errorf("Failed to open file %s: %v\n", localList, err) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - for scanner.Scan() { - dirs = append(dirs, scanner.Text()) - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("Error reading file %s: %v\n", localList, err) - } - } - err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } - if info.IsDir() { - if info.Name() == ".git" || (localList != "" && inString(info.Name(), dirs)) { - return filepath.SkipDir - } + if info.IsDir() && info.Name() == ".git" { + return filepath.SkipDir } else { - match := false - for _, regex := range regexList { - if regex.MatchString(info.Name()) { - match = true - break + match := ignore.Relative(info.Name(), true) + if match != nil { + if match.Ignore() { + matches = append(matches, path) } } - - if !match { - matches = append(matches, path) - } } - return nil }) @@ -70,8 +43,8 @@ func getForbiddens(root string, fileList []string, localList string) ([]string, // forbiddenCheck checks for forbidden files in the specified root directory. // It prints the list of forbidden files found, along with instructions on how to fix them. -func ForbiddenCheck(rootDir string, regexList []string, localList string) error { - forbids, err := getForbiddens(rootDir, regexList, localList) +func ForbiddenCheck(rootDir string, regexList []string) error { + forbids, err := getForbiddens(rootDir, regexList) if err != nil { slog.Error("getting forbiddens", "error", err) return fmt.Errorf("error getting forbiddens: %w", err) -- 2.30.2 From 83b6cb6b2cc8a9ac7f9e5d6946f1138f746e4530 Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 17 Oct 2024 15:01:36 +0800 Subject: [PATCH 3/6] fix(healthcheck/forbidden): skip `.` --- pkg/healthcheck/forbidden.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index 53e96a1..c88c33d 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -25,7 +25,10 @@ func getForbiddens(root string, fileList []string) ([]string, error) { return err } - if info.IsDir() && info.Name() == ".git" { + if info.IsDir() && (info.Name() == ".") { + return nil + } + if info.IsDir() && (info.Name() == ".git") { return filepath.SkipDir } else { match := ignore.Relative(info.Name(), true) -- 2.30.2 From 64cbeef1111ed48e9a094a70850bcbfbb852ba47 Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 17 Oct 2024 15:26:47 +0800 Subject: [PATCH 4/6] fix(healthcheck/forbidden): relative path --- cmd/repo-health-checker/main.go | 2 +- pkg/healthcheck/forbidden.go | 42 ++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cmd/repo-health-checker/main.go b/cmd/repo-health-checker/main.go index d4ee844..2976ee5 100644 --- a/cmd/repo-health-checker/main.go +++ b/cmd/repo-health-checker/main.go @@ -59,7 +59,7 @@ func main() { if err != nil { fmt.Printf("### Repo Size Check Failed:\n%s\n", err.Error()) } - err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist) + err = healthcheck.ForbiddenCheck(*rootDir) if err != nil { fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error()) } diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index c88c33d..d533822 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -11,11 +11,12 @@ import ( ) // getForbiddens retrieves a list of forbidden files in the specified root directory. -// It searches for files that do not match the specified regex patterns in the given file list. -func getForbiddens(root string, fileList []string) ([]string, error) { +// It searches for files that match the specified ignore patterns in the .gitignore file. +func getForbiddens(root string) ([]string, error) { var matches []string - ignore, err := gitignore.NewFromFile("./.gitignore") + // Create a gitignore instance from the .gitignore file + ignore, err := gitignore.NewFromFile(filepath.Join(root, ".gitignore")) if err != nil { return nil, err } @@ -25,29 +26,36 @@ func getForbiddens(root string, fileList []string) ([]string, error) { return err } - if info.IsDir() && (info.Name() == ".") { - return nil - } - if info.IsDir() && (info.Name() == ".git") { - return filepath.SkipDir - } else { - match := ignore.Relative(info.Name(), true) - if match != nil { - if match.Ignore() { - matches = append(matches, path) - } + if info.IsDir() { + if info.Name() == ".git" { + return filepath.SkipDir + } else if info.Name() == root { + return nil } } + + // Get the relative path to the git repo root + relPath, err := filepath.Rel(root, path) + if err != nil { + return err + } + match := ignore.Relative(relPath, true) + + // Check if the relative file path should be ignored based on the .gitignore rules + if match != nil && match.Ignore() { + matches = append(matches, path) + } + return nil }) return matches, err } -// forbiddenCheck checks for forbidden files in the specified root directory. +// ForbiddenCheck checks for forbidden files in the specified root directory. // It prints the list of forbidden files found, along with instructions on how to fix them. -func ForbiddenCheck(rootDir string, regexList []string) error { - forbids, err := getForbiddens(rootDir, regexList) +func ForbiddenCheck(rootDir string) error { + forbids, err := getForbiddens(rootDir) if err != nil { slog.Error("getting forbiddens", "error", err) return fmt.Errorf("error getting forbiddens: %w", err) -- 2.30.2 From f6012bdd5eef8aa47796e2e03222f5a14630941f Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 17 Oct 2024 16:11:53 +0800 Subject: [PATCH 5/6] fix(healthcheck/forbidden): remove unused option --- cmd/repo-health-checker/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/repo-health-checker/main.go b/cmd/repo-health-checker/main.go index 2976ee5..91c1aae 100644 --- a/cmd/repo-health-checker/main.go +++ b/cmd/repo-health-checker/main.go @@ -39,14 +39,13 @@ var Version string // Generally, err is used for runtime errors, and checkRes is used for the result of the checks. func main() { - var gitWhitelist, metaFile []string + var metaFile []string showVersion := flag.Bool("version", false, "print current version") rootDir := flag.String("root", "", "") size := flag.Float64("repoSize", 2, "maximum size of the repo in MiB") localList := flag.String("localList", "", "") checkFileNameList := flag.String("checkFileNameList", "", "Comma-separated list of files to check.") checkFileSumList := flag.String("checkFileSumList", "", "Comma-separated list of expected checksums.") - parseMultiValueFlag(&gitWhitelist, "whitelist", "") parseMultiValueFlag(&metaFile, "meta", "") flag.Parse() if *showVersion { -- 2.30.2 From 44e3feb5c6a575a6a6c8d467b39d143e526c728a Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 17 Oct 2024 18:20:16 +0800 Subject: [PATCH 6/6] feat(healthcheck/forbidden): gitignore in subdir --- pkg/healthcheck/forbidden.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index d533822..f7ce458 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -16,7 +16,12 @@ func getForbiddens(root string) ([]string, error) { var matches []string // Create a gitignore instance from the .gitignore file - ignore, err := gitignore.NewFromFile(filepath.Join(root, ".gitignore")) + ignore := gitignore.NewRepositoryWithCache(root, ".gitignore", gitignore.NewCache(), func(e gitignore.Error) bool { + return false + }) + + var err error + if err != nil { return nil, err } -- 2.30.2