Switch to a more capable glob implementation that supports **

This commit is contained in:
Kovid Goyal 2023-02-24 13:43:38 +05:30
parent 77c04107f3
commit e4002b5691
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 27 additions and 7 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.20
require (
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924
github.com/bmatcuk/doublestar v1.3.4
github.com/disintegration/imaging v1.6.2
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.3.0

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 h1:DG4UyTVIujioxwJc8Zj8Nabz1L1wTgQ/xNBSQDfdP3I=
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924/go.mod h1:+NaH2gLeY6RPBPPQf4aRotPPStg+eXc8f9ZaE4vRfD4=
github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=

View File

@ -20,7 +20,9 @@ def option_text() -> str:
return '''
--glob
type=bool-set
Interpret file arguments as glob patterns.
Interpret file arguments as glob patterns. Globbing is based on
Based on standard wildcards with the addition that ``/**/`` matches any number of directories.
See the :link:`detailed syntax <https://github.com/bmatcuk/doublestar#patterns>`.
--dest
@ -36,7 +38,10 @@ type=list
A glob pattern. Files with names matching this pattern are excluded from being
transferred. Useful when adding directories. Can
be specified multiple times, if any of the patterns match the file will be
excluded. To exclude a directory use a pattern like :code:`*/directory_name/*`.
excluded. To exclude a directory use a pattern like :code:`**/directory_name/**`.
Based on standard wildcards with the addition that ``/**/`` matches any number of directories
and patterns starting with a single :code:`*` (as opposed to two asterisks) match any prefix.
See the :link:`detailed syntax <https://github.com/bmatcuk/doublestar#patterns>`.
--symlink-strategy

View File

@ -18,6 +18,7 @@ import (
"kitty/tools/utils/paths"
"kitty/tools/utils/shlex"
"github.com/bmatcuk/doublestar"
"golang.org/x/sys/unix"
)
@ -143,12 +144,12 @@ func resolve_file_spec(spec string, is_glob bool) ([]string, error) {
ans = paths_ctx.AbspathFromHome(ans)
}
if is_glob {
files, err := filepath.Glob(ans)
files, err := doublestar.Glob(ans)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s is not a valid glob pattern with error: %w", spec, err)
}
if len(files) == 0 {
return nil, fmt.Errorf("%s does not exist", spec)
return nil, fmt.Errorf("%s matches no files", spec)
}
return files, nil
}
@ -228,6 +229,16 @@ type file_unique_id struct {
dev, inode uint64
}
func excluded(pattern, path string) bool {
if strings.HasPrefix(pattern, "*") && !strings.HasPrefix(pattern, "**") {
path = filepath.Base(path)
}
if matched, err := doublestar.PathMatch(pattern, path); matched && err == nil {
return true
}
return false
}
func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[file_unique_id]string, local_path, arcname string, exclude_patterns []string, recurse bool) error {
s, err := os.Lstat(local_path)
if err != nil {
@ -271,11 +282,12 @@ func get_file_data(callback func(h *tar.Header, data []byte) error, seen map[fil
if recurse {
local_path = filepath.Clean(local_path)
return filepath.WalkDir(local_path, func(path string, d fs.DirEntry, werr error) error {
if filepath.Clean(path) == local_path {
clean_path := filepath.Clean(path)
if clean_path == local_path {
return nil
}
for _, pat := range exclude_patterns {
if matched, err := filepath.Match(pat, path); matched && err == nil {
if excluded(pat, clean_path) {
return nil
}
}