parent
a108f10ccc
commit
5200c49155
4 changed files with 30 additions and 10 deletions
|
@ -6,12 +6,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
||||||
"github.com/nektos/act/pkg/model"
|
"github.com/nektos/act/pkg/model"
|
||||||
"github.com/rhysd/actionlint"
|
"github.com/rhysd/actionlint"
|
||||||
)
|
)
|
||||||
|
@ -178,25 +180,37 @@ func (impl *interperterImpl) fromJSON(value reflect.Value) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *interperterImpl) hashFiles(paths ...reflect.Value) (string, error) {
|
func (impl *interperterImpl) hashFiles(paths ...reflect.Value) (string, error) {
|
||||||
var filepaths []string
|
var ps []gitignore.Pattern
|
||||||
|
|
||||||
|
const cwdPrefix = "." + string(filepath.Separator)
|
||||||
|
const excludeCwdPrefix = "!" + cwdPrefix
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
if path.Kind() == reflect.String {
|
if path.Kind() == reflect.String {
|
||||||
filepaths = append(filepaths, path.String())
|
cleanPath := path.String()
|
||||||
|
if strings.HasPrefix(cleanPath, cwdPrefix) {
|
||||||
|
cleanPath = cleanPath[len(cwdPrefix):]
|
||||||
|
} else if strings.HasPrefix(cleanPath, excludeCwdPrefix) {
|
||||||
|
cleanPath = "!" + cleanPath[len(excludeCwdPrefix):]
|
||||||
|
}
|
||||||
|
ps = append(ps, gitignore.ParsePattern(cleanPath, nil))
|
||||||
} else {
|
} else {
|
||||||
return "", fmt.Errorf("Non-string path passed to hashFiles")
|
return "", fmt.Errorf("Non-string path passed to hashFiles")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matcher := gitignore.NewMatcher(ps)
|
||||||
|
|
||||||
var files []string
|
var files []string
|
||||||
|
if err := filepath.Walk(impl.config.WorkingDir, func(path string, fi fs.FileInfo, err error) error {
|
||||||
for i := range filepaths {
|
sansPrefix := strings.TrimPrefix(path, impl.config.WorkingDir+string(filepath.Separator))
|
||||||
newFiles, err := filepath.Glob(filepath.Join(impl.config.WorkingDir, filepaths[i]))
|
parts := strings.Split(sansPrefix, string(filepath.Separator))
|
||||||
if err != nil {
|
if fi.IsDir() || !matcher.Match(parts, fi.IsDir()) {
|
||||||
return "", fmt.Errorf("Unable to glob.Glob: %v", err)
|
return nil
|
||||||
}
|
}
|
||||||
|
files = append(files, path)
|
||||||
files = append(files, newFiles...)
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return "", fmt.Errorf("Unable to filepath.Walk: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
|
|
|
@ -188,7 +188,11 @@ func TestFunctionHashFiles(t *testing.T) {
|
||||||
{"hashFiles('**/non-extant-files') }}", "", "hash-non-existing-file"},
|
{"hashFiles('**/non-extant-files') }}", "", "hash-non-existing-file"},
|
||||||
{"hashFiles('**/non-extant-files', '**/more-non-extant-files') }}", "", "hash-multiple-non-existing-files"},
|
{"hashFiles('**/non-extant-files', '**/more-non-extant-files') }}", "", "hash-multiple-non-existing-files"},
|
||||||
{"hashFiles('./for-hashing-1.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-single-file"},
|
{"hashFiles('./for-hashing-1.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-single-file"},
|
||||||
{"hashFiles('./for-hashing-*') }}", "8e5935e7e13368cd9688fe8f48a0955293676a021562582c7e848dafe13fb046", "hash-multiple-files"},
|
{"hashFiles('./for-hashing-*.txt') }}", "8e5935e7e13368cd9688fe8f48a0955293676a021562582c7e848dafe13fb046", "hash-multiple-files"},
|
||||||
|
{"hashFiles('./for-hashing-*.txt', '!./for-hashing-2.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-negative-pattern"},
|
||||||
|
{"hashFiles('./for-hashing-**') }}", "c418ba693753c84115ced0da77f876cddc662b9054f4b129b90f822597ee2f94", "hash-multiple-files-and-directories"},
|
||||||
|
{"hashFiles('./for-hashing-3/**') }}", "6f5696b546a7a9d6d42a449dc9a56bef244aaa826601ef27466168846139d2c2", "hash-nested-directories"},
|
||||||
|
{"hashFiles('./for-hashing-3/**/nested-data.txt') }}", "8ecadfb49f7f978d0a9f3a957e9c8da6cc9ab871f5203b5d9f9d1dc87d8af18c", "hash-nested-directories-2"},
|
||||||
}
|
}
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
env := &EvaluationEnvironment{}
|
||||||
|
|
1
pkg/exprparser/testdata/for-hashing-3/data.txt
vendored
Normal file
1
pkg/exprparser/testdata/for-hashing-3/data.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Knock knock!
|
1
pkg/exprparser/testdata/for-hashing-3/nested/nested-data.txt
vendored
Normal file
1
pkg/exprparser/testdata/for-hashing-3/nested/nested-data.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Anybody home?
|
Loading…
Reference in a new issue