Add default .packwizignore contents (fixes #3)

This commit is contained in:
comp500 2022-02-10 16:47:38 +00:00
parent 226a376be9
commit fa5de4b4bc

View File

@ -3,6 +3,7 @@ package core
import ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -168,6 +169,35 @@ func (in Index) GetPackRoot() string {
return filepath.Dir(in.indexFile) return filepath.Dir(in.indexFile)
} }
var ignoreDefaults = []string{
// Defaults (can be overridden with a negating pattern preceded with !)
// Exclude Git metadata
".git/**",
".gitattributes",
".gitignore",
// Exclude exported CurseForge zip files
"/*.zip",
// Exclude exported Modrinth packs
"*.mrpack",
}
func readGitignore(path string) (*gitignore.GitIgnore, bool) {
data, err := ioutil.ReadFile(path)
if err != nil {
// TODO: check for read errors (and present them)
return gitignore.CompileIgnoreLines(ignoreDefaults...), false
}
s := strings.Split(string(data), "\n")
var lines []string
lines = append(lines, ignoreDefaults...)
lines = append(lines, s...)
return gitignore.CompileIgnoreLines(lines...), true
}
// Refresh updates the hashes of all the files in the index, and adds new files to the index // Refresh updates the hashes of all the files in the index, and adds new files to the index
func (in *Index) Refresh() error { func (in *Index) Refresh() error {
// TODO: If needed, multithreaded hashing // TODO: If needed, multithreaded hashing
@ -180,13 +210,10 @@ func (in *Index) Refresh() error {
packRoot := in.GetPackRoot() packRoot := in.GetPackRoot()
ignoreExists := true ignoreExists := true
pathIgnore, _ := filepath.Abs(filepath.Join(packRoot, ".packwizignore")) pathIgnore, _ := filepath.Abs(filepath.Join(packRoot, ".packwizignore"))
ignore, err := gitignore.CompileIgnoreFile(filepath.Join(packRoot, ".packwizignore")) ignore, ignoreExists := readGitignore(filepath.Join(packRoot, ".packwizignore"))
if err != nil {
ignoreExists = false
}
var fileList []string var fileList []string
err = filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
// TODO: Handle errors on individual files properly // TODO: Handle errors on individual files properly
return err return err
@ -205,11 +232,10 @@ func (in *Index) Refresh() error {
if absPath == pathIgnore { if absPath == pathIgnore {
return nil return nil
} }
}
if ignore.MatchesPath(path) { if ignore.MatchesPath(path) {
return nil return nil
} }
}
fileList = append(fileList, path) fileList = append(fileList, path)
return nil return nil