Skip to content

Commit 4689f4b

Browse files
committed
fix
1 parent 58c092c commit 4689f4b

File tree

9 files changed

+80
-82
lines changed

9 files changed

+80
-82
lines changed

models/db/name.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ package db
55

66
import (
77
"fmt"
8-
"regexp"
98
"strings"
109
"unicode/utf8"
1110

1211
"code.gitea.io/gitea/modules/util"
1312
)
1413

15-
var (
16-
// ErrNameEmpty name is empty error
17-
ErrNameEmpty = util.SilentWrap{Message: "name is empty", Err: util.ErrInvalidArgument}
18-
19-
// AlphaDashDotPattern characters prohibited in a username (anything except A-Za-z0-9_.-)
20-
AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
21-
)
14+
var ErrNameEmpty = util.SilentWrap{Message: "name is empty", Err: util.ErrInvalidArgument}
2215

2316
// ErrNameReserved represents a "reserved name" error.
2417
type ErrNameReserved struct {
@@ -82,20 +75,20 @@ func (err ErrNameCharsNotAllowed) Unwrap() error {
8275

8376
// IsUsableName checks if name is reserved or pattern of name is not allowed
8477
// based on given reserved names and patterns.
85-
// Names are exact match, patterns can be prefix or suffix match with placeholder '*'.
86-
func IsUsableName(names, patterns []string, name string) error {
78+
// Names are exact match, patterns can be a prefix or suffix match with placeholder '*'.
79+
func IsUsableName(reservedNames, reservedPatterns []string, name string) error {
8780
name = strings.TrimSpace(strings.ToLower(name))
8881
if utf8.RuneCountInString(name) == 0 {
8982
return ErrNameEmpty
9083
}
9184

92-
for i := range names {
93-
if name == names[i] {
85+
for i := range reservedNames {
86+
if name == reservedNames[i] {
9487
return ErrNameReserved{name}
9588
}
9689
}
9790

98-
for _, pat := range patterns {
91+
for _, pat := range reservedPatterns {
9992
if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
10093
(pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
10194
return ErrNamePatternNotAllowed{pat}

models/repo/repo.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net"
1212
"net/url"
1313
"path/filepath"
14+
"regexp"
1415
"strconv"
1516
"strings"
1617

@@ -60,13 +61,15 @@ func (err ErrRepoIsArchived) Error() string {
6061
}
6162

6263
var (
63-
reservedRepoNames = []string{".", "..", "-"}
64-
reservedRepoPatterns = []string{"*.git", "*.wiki", "*.rss", "*.atom"}
64+
validRepoNamePattern = regexp.MustCompile(`[-.\w]+`)
65+
invalidRepoNamePattern = regexp.MustCompile(`[.]{2,}`)
66+
reservedRepoNames = []string{".", "..", "-"}
67+
reservedRepoPatterns = []string{"*.git", "*.wiki", "*.rss", "*.atom"}
6568
)
6669

67-
// IsUsableRepoName returns true when repository is usable
70+
// IsUsableRepoName returns true when name is usable
6871
func IsUsableRepoName(name string) error {
69-
if db.AlphaDashDotPattern.MatchString(name) {
72+
if !validRepoNamePattern.MatchString(name) || invalidRepoNamePattern.MatchString(name) {
7073
// Note: usually this error is normally caught up earlier in the UI
7174
return db.ErrNameCharsNotAllowed{Name: name}
7275
}

models/repo/repo_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,15 @@ func TestComposeSSHCloneURL(t *testing.T) {
217217
setting.SSH.Port = 123
218218
assert.Equal(t, "ssh://git@[::1]:123/user/repo.git", ComposeSSHCloneURL("user", "repo"))
219219
}
220+
221+
func TestIsUsableRepoName(t *testing.T) {
222+
assert.NoError(t, IsUsableRepoName("a"))
223+
assert.NoError(t, IsUsableRepoName("-1_."))
224+
assert.NoError(t, IsUsableRepoName(".profile"))
225+
226+
assert.Error(t, IsUsableRepoName("-"))
227+
assert.Error(t, IsUsableRepoName("🌞"))
228+
assert.Error(t, IsUsableRepoName("the..repo"))
229+
assert.Error(t, IsUsableRepoName("foo.wiki"))
230+
assert.Error(t, IsUsableRepoName("foo.git"))
231+
}

models/unittest/fscopy.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func SyncDirs(srcPath, destPath string) error {
6767
}
6868

6969
// find and delete all untracked files
70-
destFiles, err := util.StatDir(destPath, true)
70+
destFiles, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true})
7171
if err != nil {
7272
return err
7373
}
@@ -86,13 +86,13 @@ func SyncDirs(srcPath, destPath string) error {
8686
}
8787

8888
// sync src files to dest
89-
srcFiles, err := util.StatDir(srcPath, true)
89+
srcFiles, err := util.ListDirRecursively(srcPath, &util.ListDirOptions{IncludeDir: true})
9090
if err != nil {
9191
return err
9292
}
9393
for _, srcFile := range srcFiles {
9494
destFilePath := filepath.Join(destPath, srcFile)
95-
// util.StatDir appends a slash to the directory name
95+
// util.ListDirRecursively appends a slash to the directory name
9696
if strings.HasSuffix(srcFile, "/") {
9797
err = os.MkdirAll(destFilePath, os.ModePerm)
9898
} else {

modules/assetfs/layered.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (l *LayeredFS) ReadLayeredFile(elems ...string) ([]byte, string, error) {
103103
}
104104

105105
func shouldInclude(info fs.FileInfo, fileMode ...bool) bool {
106-
if util.CommonSkip(info.Name()) {
106+
if util.IsCommonHiddenFileName(info.Name()) {
107107
return false
108108
}
109109
if len(fileMode) == 0 {

modules/repository/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func LoadRepoConfig() error {
8181
if isDir, err := util.IsDir(customPath); err != nil {
8282
return fmt.Errorf("failed to check custom %s dir: %w", t, err)
8383
} else if isDir {
84-
if typeFiles[i].custom, err = util.StatDir(customPath); err != nil {
84+
if typeFiles[i].custom, err = util.ListDirRecursively(customPath, &util.ListDirOptions{SkipCommonHiddenNames: true}); err != nil {
8585
return fmt.Errorf("failed to list custom %s files: %w", t, err)
8686
}
8787
}

modules/util/path.go

+28-59
Original file line numberDiff line numberDiff line change
@@ -140,82 +140,51 @@ func IsExist(path string) (bool, error) {
140140
return false, err
141141
}
142142

143-
func statDir(dirPath, recPath string, includeDir, isDirOnly, followSymlinks bool) ([]string, error) {
144-
dir, err := os.Open(dirPath)
143+
func listDirRecursively(result *[]string, fsDir, recordParentPath string, opts *ListDirOptions) error {
144+
dir, err := os.Open(fsDir)
145145
if err != nil {
146-
return nil, err
146+
return err
147147
}
148148
defer dir.Close()
149149

150150
fis, err := dir.Readdir(0)
151151
if err != nil {
152-
return nil, err
152+
return err
153153
}
154154

155-
statList := make([]string, 0)
156155
for _, fi := range fis {
157-
if CommonSkip(fi.Name()) {
156+
if opts.SkipCommonHiddenNames && IsCommonHiddenFileName(fi.Name()) {
158157
continue
159158
}
160-
161-
relPath := path.Join(recPath, fi.Name())
162-
curPath := path.Join(dirPath, fi.Name())
159+
relPath := path.Join(recordParentPath, fi.Name())
160+
curPath := filepath.Join(fsDir, fi.Name())
163161
if fi.IsDir() {
164-
if includeDir {
165-
statList = append(statList, relPath+"/")
166-
}
167-
s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks)
168-
if err != nil {
169-
return nil, err
170-
}
171-
statList = append(statList, s...)
172-
} else if !isDirOnly {
173-
statList = append(statList, relPath)
174-
} else if followSymlinks && fi.Mode()&os.ModeSymlink != 0 {
175-
link, err := os.Readlink(curPath)
176-
if err != nil {
177-
return nil, err
178-
}
179-
180-
isDir, err := IsDir(link)
181-
if err != nil {
182-
return nil, err
162+
if opts.IncludeDir {
163+
*result = append(*result, relPath+"/")
183164
}
184-
if isDir {
185-
if includeDir {
186-
statList = append(statList, relPath+"/")
187-
}
188-
s, err := statDir(curPath, relPath, includeDir, isDirOnly, followSymlinks)
189-
if err != nil {
190-
return nil, err
191-
}
192-
statList = append(statList, s...)
165+
if err = listDirRecursively(result, curPath, relPath, opts); err != nil {
166+
return err
193167
}
168+
} else {
169+
*result = append(*result, relPath)
194170
}
195171
}
196-
return statList, nil
172+
return nil
197173
}
198174

199-
// StatDir gathers information of given directory by depth-first.
200-
// It returns slice of file list and includes subdirectories if enabled;
201-
// it returns error and nil slice when error occurs in underlying functions,
202-
// or given path is not a directory or does not exist.
203-
//
175+
type ListDirOptions struct {
176+
IncludeDir bool // subdirectories are also included with suffix slash
177+
SkipCommonHiddenNames bool
178+
}
179+
180+
// ListDirRecursively gathers information of given directory by depth-first.
181+
// The paths are always in "dir/slash/file" format (not "\\" even in Windows)
204182
// Slice does not include given path itself.
205-
// If subdirectories is enabled, they will have suffix '/'.
206-
// FIXME: it doesn't like dot-files, for example: "owner/.profile.git"
207-
func StatDir(rootPath string, includeDir ...bool) ([]string, error) {
208-
if isDir, err := IsDir(rootPath); err != nil {
183+
func ListDirRecursively(rootDir string, opts *ListDirOptions) (res []string, err error) {
184+
if err = listDirRecursively(&res, rootDir, "", opts); err != nil {
209185
return nil, err
210-
} else if !isDir {
211-
return nil, errors.New("not a directory or does not exist: " + rootPath)
212-
}
213-
214-
isIncludeDir := false
215-
if len(includeDir) != 0 {
216-
isIncludeDir = includeDir[0]
217186
}
218-
return statDir(rootPath, "", isIncludeDir, false, false)
187+
return res, nil
219188
}
220189

221190
func isOSWindows() bool {
@@ -266,8 +235,8 @@ func HomeDir() (home string, err error) {
266235
return home, nil
267236
}
268237

269-
// CommonSkip will check a provided name to see if it represents file or directory that should not be watched
270-
func CommonSkip(name string) bool {
238+
// IsCommonHiddenFileName will check a provided name to see if it represents file or directory that should not be watched
239+
func IsCommonHiddenFileName(name string) bool {
271240
if name == "" {
272241
return true
273242
}
@@ -276,9 +245,9 @@ func CommonSkip(name string) bool {
276245
case '.':
277246
return true
278247
case 't', 'T':
279-
return name[1:] == "humbs.db"
248+
return name[1:] == "humbs.db" // macOS
280249
case 'd', 'D':
281-
return name[1:] == "esktop.ini"
250+
return name[1:] == "esktop.ini" // Windows
282251
}
283252

284253
return false

modules/util/path_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package util
55

66
import (
77
"net/url"
8+
"os"
89
"runtime"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1214
)
1315

1416
func TestFileURLToPath(t *testing.T) {
@@ -210,3 +212,21 @@ func TestCleanPath(t *testing.T) {
210212
assert.Equal(t, c.expected, FilePathJoinAbs(c.elems[0], c.elems[1:]...), "case: %v", c.elems)
211213
}
212214
}
215+
216+
func TestListDirRecursively(t *testing.T) {
217+
tmpDir := t.TempDir()
218+
_ = os.WriteFile(tmpDir+"/.config", nil, 0644)
219+
_ = os.Mkdir(tmpDir+"/d1", 0755)
220+
_ = os.WriteFile(tmpDir+"/d1/f-d1", nil, 0644)
221+
_ = os.Mkdir(tmpDir+"/d1/s1", 0755)
222+
_ = os.WriteFile(tmpDir+"/d1/s1/f-d1s1", nil, 0644)
223+
_ = os.Mkdir(tmpDir+"/d2", 0755)
224+
225+
res, err := ListDirRecursively(tmpDir, &ListDirOptions{IncludeDir: true})
226+
require.NoError(t, err)
227+
assert.ElementsMatch(t, []string{".config", "d1/", "d1/f-d1", "d1/s1/", "d1/s1/f-d1s1", "d2/"}, res)
228+
229+
res, err = ListDirRecursively(tmpDir, &ListDirOptions{SkipCommonHiddenNames: true})
230+
require.NoError(t, err)
231+
assert.ElementsMatch(t, []string{"d1/f-d1", "d1/s1/f-d1s1"}, res)
232+
}

web_src/js/features/repo-new.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export function initRepoNew() {
1111
const updateUiAutoInit = () => {
1212
inputAutoInit.checked = Boolean(inputGitIgnores.value || inputLicense.value);
1313
};
14-
form.addEventListener('change', updateUiAutoInit);
14+
inputGitIgnores.addEventListener('change', updateUiAutoInit);
15+
inputLicense.addEventListener('change', updateUiAutoInit);
1516
updateUiAutoInit();
1617

1718
const inputRepoName = form.querySelector<HTMLInputElement>('input[name="repo_name"]');

0 commit comments

Comments
 (0)