version2 (#1)

Co-authored-by: u80864958 <niklas.breitenstein@bit.admin.ch>
Reviewed-on: #1
This commit is contained in:
2025-04-02 17:29:26 +02:00
parent abe9c7d1fb
commit 158e963c7b
11 changed files with 310 additions and 130 deletions

9
pkg/ignore/filesystem.go Normal file
View File

@ -0,0 +1,9 @@
package ignore
import "strings"
type Filesystem struct{}
func (f Filesystem) Ignore(name string) bool {
return strings.Contains(name, "/.") || strings.HasPrefix(name, ".")
}

65
pkg/ignore/gitignore.go Normal file
View File

@ -0,0 +1,65 @@
package ignore
import (
"errors"
"os"
"path"
ignore "github.com/denormal/go-gitignore"
)
const (
gitIgnore = ".gitignore"
)
var (
ErrNotFound = errors.New("Not Found")
)
// getIgnore attempts to find and parse a .gitignore file recursively.
// It searches for the .gitignore file starting from the given path and traversing up the directory tree.
// It returns a pointer to the parsed ignore.GitIgnore object if found, otherwise nil.
func FindGitignore(name string) (ignore.GitIgnore, error) {
for {
stat, err := os.Stat(name)
if err != nil {
return nil, err
}
// If the current path is a directory, iterate through its contents.
if stat.IsDir() {
dir, err := os.ReadDir(name)
if err != nil {
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
for _, e := range dir {
if !e.IsDir() && e.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(path.Join(name, e.Name())); err == nil {
return ignore, nil
}
return nil, err
}
}
}
// If the current path is the .gitignore file itself.
if stat.Name() == gitIgnore {
if ignore, err := ignore.NewFromFile(name); err == nil {
return ignore, nil
}
return nil, err
}
name, _ = path.Split(name)
if name == "" {
return nil, ErrNotFound
}
}
}