Skip to content

Ensure extendedGlob returns paths in lexical order #6169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -48,6 +49,7 @@ func init() {
// "**" component in the pattern, filepath.Glob() will be called with the "**"
// replaced with all of the subdirectories under that point, and the results
// will be concatenated.
// The matched paths are returned in lexical order, which makes the output deterministic.
func extendedGlob(pattern string) (matches []string, err error) {
subdirs := func(dir string) []string {
var subdirectories []string
Expand Down Expand Up @@ -113,6 +115,7 @@ func extendedGlob(pattern string) (matches []string, err error) {
}
matches = append(matches, theseMatches...)
}
sort.Strings(matches)
return matches, nil
}

Expand Down
15 changes: 15 additions & 0 deletions copier/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2025,3 +2025,18 @@ func TestExtendedGlob(t *testing.T) {
require.NoError(t, err, "globbing")
require.ElementsMatch(t, expected2, matched, "**/d/**/*.dat")
}

func TestSortedExtendedGlob(t *testing.T) {
tmpdir := t.TempDir()
buf := []byte("buffer")
expect := []string{}
for _, name := range []string{"z", "y", "x", "a", "b", "c", "d", "e", "f"} {
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, name), buf, 0o600))
expect = append(expect, filepath.Join(tmpdir, name))
}
sort.Strings(expect)

matched, err := extendedGlob(filepath.Join(tmpdir, "*"))
require.NoError(t, err, "globbing")
require.ElementsMatch(t, expect, matched, "sorted globbing")
}