Skip to content

Commit

Permalink
Fix substituting directories
Browse files Browse the repository at this point in the history
  • Loading branch information
kdumontnu committed Jun 19, 2023
1 parent dfea4f8 commit 3b8fe94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 15 additions & 6 deletions modules/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var defaultTransformers = []transformer{
{Name: "TITLE", Transform: util.ToTitleCase},
}

func generateExpansion(src string, templateRepo, generateRepo *repo_model.Repository) string {
func generateExpansion(src string, templateRepo, generateRepo *repo_model.Repository, escapeFileName bool) string {
expansions := []expansion{
{Name: "REPO_NAME", Value: generateRepo.Name, Transformers: defaultTransformers},
{Name: "TEMPLATE_NAME", Value: templateRepo.Name, Transformers: defaultTransformers},
Expand All @@ -75,6 +75,9 @@ func generateExpansion(src string, templateRepo, generateRepo *repo_model.Reposi

return os.Expand(src, func(key string) string {
if expansion, ok := expansionMap[key]; ok {
if escapeFileName {
return fileNameEscape(expansion)
}
return expansion
}
return key
Expand Down Expand Up @@ -192,15 +195,21 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
}

if err := os.WriteFile(path,
[]byte(generateExpansion(string(content), templateRepo, generateRepo)),
[]byte(generateExpansion(string(content), templateRepo, generateRepo, false)),
0o644); err != nil {
return err
}

substPath := filepath.FromSlash(filepath.Join(tmpDirSlash,
generateExpansion(base, templateRepo, generateRepo, true)))

// Create parent subdirectories if needed or continue silently if it exists
if err := os.MkdirAll(filepath.Dir(substPath), 0o755); err != nil {
return err
}

// Substitute filename variables
if err := os.Rename(path,
filepath.FromSlash(filepath.Join(tmpDirSlash,
fileNameEscape(generateExpansion(base, templateRepo, generateRepo))))); err != nil {
if err := os.Rename(path, substPath); err != nil {
return err
}

Expand Down Expand Up @@ -370,5 +379,5 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
func fileNameEscape(s string) string {
re := regexp.MustCompile(`(?i)[\.<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)

return re.ReplaceAllString(s, "_")
return strings.TrimSpace(re.ReplaceAllString(s, "_"))
}
2 changes: 2 additions & 0 deletions modules/repository/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestGiteaTemplate(t *testing.T) {

func TestFileNameEscape(t *testing.T) {
assert.Equal(t, "test_CON", fileNameEscape("test_CON"))
assert.Equal(t, "test CON", fileNameEscape("test CON "))
assert.Equal(t, "___traverse", fileNameEscape("../traverse"))
assert.Equal(t, "http___localhost_3003_user_test_git", fileNameEscape("http://localhost:3003/user/test.git"))
assert.Equal(t, "_", fileNameEscape("CON"))
assert.Equal(t, "_", fileNameEscape("con"))
Expand Down

0 comments on commit 3b8fe94

Please sign in to comment.