Skip to content

Commit 2d32c9a

Browse files
Merge pull request #6169 from Honny1/sort-glob-output
Ensure extendedGlob returns paths in lexical order
2 parents bd021be + 2717599 commit 2d32c9a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

copier/copier.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path"
1515
"path/filepath"
1616
"slices"
17+
"sort"
1718
"strconv"
1819
"strings"
1920
"sync"
@@ -48,6 +49,7 @@ func init() {
4849
// "**" component in the pattern, filepath.Glob() will be called with the "**"
4950
// replaced with all of the subdirectories under that point, and the results
5051
// will be concatenated.
52+
// The matched paths are returned in lexical order, which makes the output deterministic.
5153
func extendedGlob(pattern string) (matches []string, err error) {
5254
subdirs := func(dir string) []string {
5355
var subdirectories []string
@@ -113,6 +115,7 @@ func extendedGlob(pattern string) (matches []string, err error) {
113115
}
114116
matches = append(matches, theseMatches...)
115117
}
118+
sort.Strings(matches)
116119
return matches, nil
117120
}
118121

copier/copier_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,3 +2341,18 @@ func TestConditionalRemoveNoChroot(t *testing.T) {
23412341
testConditionalRemove(t)
23422342
canChroot = couldChroot
23432343
}
2344+
2345+
func TestSortedExtendedGlob(t *testing.T) {
2346+
tmpdir := t.TempDir()
2347+
buf := []byte("buffer")
2348+
expect := []string{}
2349+
for _, name := range []string{"z", "y", "x", "a", "b", "c", "d", "e", "f"} {
2350+
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, name), buf, 0o600))
2351+
expect = append(expect, filepath.Join(tmpdir, name))
2352+
}
2353+
sort.Strings(expect)
2354+
2355+
matched, err := extendedGlob(filepath.Join(tmpdir, "*"))
2356+
require.NoError(t, err, "globbing")
2357+
require.ElementsMatch(t, expect, matched, "sorted globbing")
2358+
}

0 commit comments

Comments
 (0)