Improved gitignore handling (#163)
This commit is contained in:
parent
4a4bd36cf6
commit
bf3824cc10
2 changed files with 16 additions and 7 deletions
2
go.mod
2
go.mod
|
@ -44,7 +44,7 @@ require (
|
|||
gopkg.in/godo.v2 v2.0.9
|
||||
gopkg.in/ini.v1 v1.41.0 // indirect
|
||||
gopkg.in/sourcemap.v1 v1.0.5 // indirect
|
||||
gopkg.in/src-d/go-billy.v4 v4.3.0 // indirect
|
||||
gopkg.in/src-d/go-billy.v4 v4.3.0
|
||||
gopkg.in/src-d/go-git-fixtures.v3 v3.3.0 // indirect
|
||||
gopkg.in/src-d/go-git.v4 v4.9.1
|
||||
gopkg.in/yaml.v3 v3.0.0-20200121175148-a6ecf24a6d71
|
||||
|
|
|
@ -5,6 +5,9 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"gopkg.in/src-d/go-billy.v4/helper/polyfill"
|
||||
"gopkg.in/src-d/go-billy.v4/osfs"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -16,7 +19,6 @@ import (
|
|||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
gitignore "github.com/monochromegane/go-gitignore"
|
||||
"github.com/nektos/act/pkg/common"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -325,22 +327,29 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
|
|||
}
|
||||
log.Debugf("Stripping prefix:%s src:%s", srcPrefix, srcPath)
|
||||
|
||||
gitignore, err := gitignore.NewGitIgnore(filepath.Join(srcPath, ".gitignore"))
|
||||
ps, err := gitignore.ReadPatterns(polyfill.New(osfs.New(srcPath)), nil)
|
||||
if err != nil {
|
||||
log.Debugf("Error loading .gitignore: %v", err)
|
||||
}
|
||||
|
||||
ignorer := gitignore.NewMatcher(ps)
|
||||
|
||||
err = filepath.Walk(srcPath, func(file string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
|
||||
if !fi.Mode().IsRegular() {
|
||||
sansPrefix := strings.TrimPrefix(file, srcPrefix)
|
||||
split := strings.Split(sansPrefix, string(filepath.Separator))
|
||||
if ignorer.Match(split, fi.IsDir()) {
|
||||
if fi.IsDir() {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if gitignore != nil && gitignore.Match(file, fi.IsDir()) {
|
||||
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
|
||||
if !fi.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -351,7 +360,7 @@ func (cr *containerReference) copyDir(dstPath string, srcPath string) common.Exe
|
|||
}
|
||||
|
||||
// update the name to correctly reflect the desired destination when untaring
|
||||
header.Name = strings.TrimPrefix(file, srcPrefix)
|
||||
header.Name = sansPrefix
|
||||
header.Mode = int64(fi.Mode())
|
||||
header.ModTime = fi.ModTime()
|
||||
|
||||
|
|
Loading…
Reference in a new issue