Skip to content

Commit

Permalink
apply ignore for add command (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 17, 2023
1 parent 8f01113 commit e9d4b06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
8 changes: 1 addition & 7 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ var addCmd = &cobra.Command{
// check if the arg is the target of excluding path
cleanedArg := filepath.Clean(arg)
cleanedArg = strings.ReplaceAll(cleanedArg, `\`, "/")
isExcluded := false
for _, excludePath := range client.ExcludePaths {
if excludePath == cleanedArg {
isExcluded = true
}
}
if isExcluded {
if client.Ignore.IsIncluded(cleanedArg) {
continue
}

Expand Down
32 changes: 24 additions & 8 deletions internal/store/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,33 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
)

var (
directoryRegexp = regexp.MustCompile(`.*\/`)
)

type Ignore struct {
file []string
directory []string
paths []string
}

func NewIgnore(rootGoitPath string) (*Ignore, error) {
i := newIgnore()
if err := i.load(rootGoitPath); err != nil {
return nil, err
}
fmt.Println(i.file, i.directory)
return i, nil
}

func newIgnore() *Ignore {
return &Ignore{
file: make([]string, 0),
directory: []string{".goit/"},
paths: []string{".goit/.*"},
}
}

func (i *Ignore) load(rootGoitPath string) error {
goitignorePath := filepath.Join(filepath.Dir(rootGoitPath), ".goitignore")
fmt.Println(goitignorePath)
if _, err := os.Stat(goitignorePath); os.IsNotExist(err) {
return nil
}
Expand All @@ -48,12 +45,31 @@ func (i *Ignore) load(rootGoitPath string) error {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
var replacedText string
if directoryRegexp.MatchString(text) {
i.directory = append(i.directory, text)
replacedText = fmt.Sprintf("%s.*", text)
} else {
i.file = append(i.file, text)
replacedText = strings.ReplaceAll(text, ".", `\.`)
replacedText = strings.ReplaceAll(replacedText, "*", ".*")
}
i.paths = append(i.paths, replacedText)
}

return nil
}

// return true if the parameter is included in ignore list
func (i *Ignore) IsIncluded(path string) bool {
target := path
info, _ := os.Stat(path)
if info.IsDir() && !directoryRegexp.MatchString(path) {
target = fmt.Sprintf("%s/", path)
}
for _, exFile := range i.paths {
exRegexp := regexp.MustCompile(exFile)
if exRegexp.MatchString(target) {
return true
}
}
return false
}

0 comments on commit e9d4b06

Please sign in to comment.