Skip to content

Commit

Permalink
Merge pull request #25 from InfuseAI/feature/sc-25125/change-the-arti…
Browse files Browse the repository at this point in the history
…gnore-format

change .avcignore format
  • Loading branch information
popcornylu committed Mar 21, 2022
2 parents 61f9ed1 + 1f236f4 commit 3ee907e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ bin
.idea
generated_docs
.art
.artignore
.avcignore
18 changes: 9 additions & 9 deletions docs/content/en/usage/ignore-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ title: Ignore File
weight: 2
---

Just like git, you can put a `.artignore` file at the root of workspace to define the excluding list. The rule is a regular expression of path. Here is the example
Just like git, you can put a `.avcignore` file at the root of workspace to define the excluding list. The rule is the same as `.gitignore`. For more details, please check the [pattern format](https://git-scm.com/docs/gitignore#_pattern_format) in the git document.

```shell
# Each line defines a regular expression rule
Here is the example:

```shell
# Ignore files
^test$
^path/to/my/file$
\.DS_Store$
test
path/to/my/file
.DS_Store

# Ignore folders. Use a forward slash at the end
^build/
^path/to/my/folder/
build/
path/to/my/folder/
/build/

# Ignore all file with extension '.py'
\.py$
*.py
````


1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/sagikazarmark/crypt v0.3.0/go.mod h1:uD/D+6UF4SrIR1uGEv7bBNkNqLGqUr43MRiaGWX1Nig=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
52 changes: 8 additions & 44 deletions internal/core/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package core

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/BurntSushi/toml"
gitignore "github.com/sabhiram/go-gitignore"
)

func InitWorkspace(baseDir, repo string) error {
Expand Down Expand Up @@ -201,51 +200,16 @@ func (config *ArtConfig) Save() error {
return nil
}

type ArtIgnore struct {
patterns []string
}
type AvcIgnore = gitignore.GitIgnore

func (i ArtIgnore) ShouldIgnore(path string) bool {
for _, p := range i.patterns {
matched, err := regexp.MatchString(p, path)
if matched {
// fmt.Printf("I %s %s\n", path, p)
return true
}
if err != nil {
// TODO print warning
}
}
return false
}
func NewAvcIgnore(dir string) (*AvcIgnore, error) {
avcIgnorePath := path.Join(dir, ".avcignore")

func NewArtIgnore(dir string) ArtIgnore {
artIgnorePath := path.Join(dir, ".artignore")
file, err := os.Open(artIgnorePath)
artIgnore := ArtIgnore{
patterns: []string{},
}
if err != nil {
// return empty ignore
return artIgnore
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())

if len(text) == 0 {
continue
}
avcIgnore, err := gitignore.CompileIgnoreFile(avcIgnorePath)

// skip # comment line
if strings.IndexAny(text, "#") == 0 {
continue
}

artIgnore.patterns = append(artIgnore.patterns, text)
if err != nil {
return nil, err
}

return artIgnore
return avcIgnore, nil
}
75 changes: 53 additions & 22 deletions internal/core/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,21 +335,31 @@ func (mngr *ArtifactManager) Push(options PushOptions) error {
parent = ""
}
checkSkip := true
artIgnore := NewArtIgnore(mngr.baseDir)
artIgnoreFilter := func(path string) bool {
return !artIgnore.ShouldIgnore(path)
avcIgnore, err := NewAvcIgnore(mngr.baseDir)
avcIgnoreFilter := func(path string) bool {
return true
}

commit, err := mngr.MakeWorkspaceCommit(parent, options.Message, artIgnoreFilter)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
avcIgnoreFilter = func(path string) bool {
return !avcIgnore.MatchesPath(path)
}
}

commit, err := mngr.MakeWorkspaceCommit(parent, options.Message, avcIgnoreFilter)
if err != nil {
return err
}

result, err := mngr.Diff(DiffOptions{
LeftRef: RefLatest,
RightCommit: commit,
AddFilter: artIgnoreFilter,
ChangeFilter: artIgnoreFilter,
AddFilter: avcIgnoreFilter,
ChangeFilter: avcIgnoreFilter,
DeleteFilter: nil,
})
if err != nil {
Expand All @@ -360,8 +370,8 @@ func (mngr *ArtifactManager) Push(options PushOptions) error {
result, err = mngr.Diff(DiffOptions{
LeftCommit: mngr.MakeEmptyCommit(),
RightCommit: commit,
AddFilter: artIgnoreFilter,
ChangeFilter: artIgnoreFilter,
AddFilter: avcIgnoreFilter,
ChangeFilter: avcIgnoreFilter,
DeleteFilter: nil,
})
if err != nil {
Expand Down Expand Up @@ -575,11 +585,21 @@ func (mngr *ArtifactManager) Pull(options PullOptions) error {
}

// Get the local commit hash
artIgnore := NewArtIgnore(mngr.baseDir)
artIgnoreFilter := func(path string) bool {
return !artIgnore.ShouldIgnore(path)
avcIgnore, err := NewAvcIgnore(mngr.baseDir)
avcIgnoreFilter := func(path string) bool {
return true
}

if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
avcIgnoreFilter = func(path string) bool {
return !avcIgnore.MatchesPath(path)
}
}
commitLocal, err := mngr.MakeWorkspaceCommit("", nil, artIgnoreFilter)
commitLocal, err := mngr.MakeWorkspaceCommit("", nil, avcIgnoreFilter)
if err != nil {
if err != ErrWorkspaceNotFound {
return err
Expand All @@ -593,9 +613,9 @@ func (mngr *ArtifactManager) Pull(options PullOptions) error {
NoDelete: !options.Delete,
LeftCommit: commitLocal,
RightCommit: commitRemote,
AddFilter: artIgnoreFilter,
ChangeFilter: artIgnoreFilter,
DeleteFilter: artIgnoreFilter,
AddFilter: avcIgnoreFilter,
ChangeFilter: avcIgnoreFilter,
DeleteFilter: avcIgnoreFilter,
})
if err != nil {
return err
Expand Down Expand Up @@ -944,11 +964,22 @@ func (mngr *ArtifactManager) Status() (DiffResult, error) {
}

// Get the local commit hash
artIgnore := NewArtIgnore(mngr.baseDir)
artIgnoreFilter := func(path string) bool {
return !artIgnore.ShouldIgnore(path)
avcIgnore, err := NewAvcIgnore(mngr.baseDir)
avcIgnoreFilter := func(path string) bool {
return true
}
commitLocal, err := mngr.MakeWorkspaceCommit("", nil, artIgnoreFilter)

if err != nil {
if !os.IsNotExist(err) {
return DiffResult{}, err
}
} else {
avcIgnoreFilter = func(path string) bool {
return !avcIgnore.MatchesPath(path)
}
}

commitLocal, err := mngr.MakeWorkspaceCommit("", nil, avcIgnoreFilter)
if err != nil {
if err != ErrWorkspaceNotFound {
return DiffResult{}, err
Expand All @@ -961,9 +992,9 @@ func (mngr *ArtifactManager) Status() (DiffResult, error) {
result, err := mngr.Diff(DiffOptions{
LeftCommit: commitRemote,
RightCommit: commitLocal,
AddFilter: artIgnoreFilter,
ChangeFilter: artIgnoreFilter,
DeleteFilter: artIgnoreFilter,
AddFilter: avcIgnoreFilter,
ChangeFilter: avcIgnoreFilter,
DeleteFilter: avcIgnoreFilter,
})
if err != nil {
return DiffResult{}, err
Expand Down
16 changes: 8 additions & 8 deletions internal/core/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func TestPushWithIgnore(t *testing.T) {
writeFile([]byte("b"), filepath.Join(wp1, "b"))
writeFile([]byte("c"), filepath.Join(wp1, "c"))

artIgnore := `
^a$
^e$
avcIgnore := `
a
e
`

writeFile([]byte(artIgnore), filepath.Join(wp1, ".artignore"))
writeFile([]byte(avcIgnore), filepath.Join(wp1, ".avcignore"))

InitWorkspace(wp1, repo)
config, _ := LoadConfig(wp1)
Expand Down Expand Up @@ -119,11 +119,11 @@ func TestPullWithIgnore(t *testing.T) {
assert.Empty(t, err)

// pull
artIgnore := `
^a$
^e$
avcIgnore := `
a
e
`
writeFile([]byte(artIgnore), filepath.Join(wp2, ".artignore"))
writeFile([]byte(avcIgnore), filepath.Join(wp2, ".avcignore"))
writeFile([]byte("abc"), filepath.Join(wp2, "a"))
writeFile([]byte("efg"), filepath.Join(wp2, "e"))
InitWorkspace(wp2, repo)
Expand Down

0 comments on commit 3ee907e

Please sign in to comment.