JOJ3/pkg/healthcheck/forbidden.go
周赵嘉程521432910016 b75a756998
Some checks failed
build / build (push) Failing after 19s
build / trigger-build-image (push) Has been skipped
feat(healthcheck): forbidden check guided by .gitignore file (#58) (#60)
Co-authored-by: Hydraallen <wangruiallen@gmail.com>
Co-authored-by: zzjc1234 <2359047351@qq.com>
Reviewed-on: #60
Reviewed-by: 张泊明518370910136 <bomingzh@sjtu.edu.cn>
Co-authored-by: 周赵嘉程521432910016 <zzjc123@sjtu.edu.cn>
Co-committed-by: 周赵嘉程521432910016 <zzjc123@sjtu.edu.cn>
2024-10-18 14:41:09 +08:00

84 lines
2.2 KiB
Go

package healthcheck
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/denormal/go-gitignore"
)
// getForbiddens retrieves a list of forbidden files in the specified root directory.
// It searches for files that match the specified ignore patterns in the .gitignore file.
func getForbiddens(root string) ([]string, error) {
var matches []string
// Create a gitignore instance from the .gitignore file
ignore := gitignore.NewRepositoryWithCache(root, ".gitignore", gitignore.NewCache(), func(e gitignore.Error) bool {
return false
})
var err error
if err != nil {
return nil, 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" {
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.
// It prints the list of forbidden files found, along with instructions on how to fix them.
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)
}
if len(forbids) > 0 {
return fmt.Errorf("The following forbidden files were found: `%s`\n\n"+
"To fix it, first make a backup of your repository and then run the following commands:\n"+
"```bash\n"+
"export GIT_BRANCH=$(git branch --show-current)\n"+
"export GIT_REMOTE_URL=$(git config --get remote.origin.url)\n"+
"for i in %s; do git filter-repo --force --invert-paths --path \"$i\"; done\n"+
"git remote add origin $GIT_REMOTE_URL\n"+
"git push --set-upstream origin $GIT_BRANCH --force\n"+
"```\n",
strings.Join(forbids, "`, `"),
strings.Join(forbids, " "))
}
return nil
}