28 lines
863 B
Go
28 lines
863 B
Go
package gitignore
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
ignore "github.com/sabhiram/go-gitignore"
|
|
)
|
|
|
|
// Load 加载指定 root 目录下的 .gitignore 规则
|
|
//
|
|
// 行为说明:
|
|
// 1. 如果 root/.gitignore 存在,则解析该文件
|
|
// 2. 如果不存在,则返回一个“空规则集”(不会忽略任何文件)
|
|
// 3. 返回 go-gitignore 的 GitIgnore 对象,用于路径匹配
|
|
func Load(root string) (*ignore.GitIgnore, error) {
|
|
// 拼接 .gitignore 文件路径
|
|
path := filepath.Join(root, ".gitignore")
|
|
|
|
// 如果 .gitignore 不存在,则返回一个空的 ignore 规则
|
|
// 这样后续 MatchesPath 不会 panic,也不会过滤任何文件
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return ignore.CompileIgnoreLines(), nil
|
|
}
|
|
|
|
// 如果存在 .gitignore,则解析文件内容生成规则
|
|
return ignore.CompileIgnoreFile(path)
|
|
} |