Skip to content
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

Variable expansion in repository templates #9163

Merged
merged 21 commits into from
Nov 30, 2019
Merged
Changes from 1 commit
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
72 changes: 51 additions & 21 deletions models/repo_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models
import (
"code.gitea.io/gitea/modules/process"
"fmt"
"github.com/gobwas/glob"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -39,6 +40,34 @@ func (gro GenerateRepoOptions) IsValid() bool {
return gro.GitContent || gro.Topics || gro.GitHooks || gro.Webhooks || gro.Avatar || gro.IssueLabels // or other items as they are added
}

func parseGiteaTemplate(tmpDir string) ([]glob.Glob, error) {
jolheiser marked this conversation as resolved.
Show resolved Hide resolved
globs := make([]glob.Glob, 0)
gtPath := filepath.Join(tmpDir, ".giteatemplate")
if _, err := os.Stat(gtPath); err != nil {
return globs, err
}
content, err := ioutil.ReadFile(gtPath)
if err != nil {
return globs, err
}

lines := strings.Split(string(content), "\n")
jolheiser marked this conversation as resolved.
Show resolved Hide resolved
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
g, err := glob.Compile(line, '.', '/')
jolheiser marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Info("Invalid glob expression '%s' (skipped): %v", line, err)
continue
}
globs = append(globs, g)
}

return globs, os.Remove(gtPath)
}

func generateRepoCommit(e Engine, repo, templateRepo, generateRepo *Repository, tmpDir string) error {
commitTimeStr := time.Now().Format(time.RFC3339)
authorSig := repo.Owner.NewGitSig()
Expand Down Expand Up @@ -69,7 +98,12 @@ func generateRepoCommit(e Engine, repo, templateRepo, generateRepo *Repository,
return fmt.Errorf("remove git dir: %v", err)
}

// Variable expansion in _template files
// Variable expansion
globs, err := parseGiteaTemplate(tmpDir)
if err != nil {
return fmt.Errorf("parseGiteaTemplate: %v", err)
jolheiser marked this conversation as resolved.
Show resolved Hide resolved
}

if err := filepath.Walk(tmpDir, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
Expand All @@ -79,27 +113,23 @@ func generateRepoCommit(e Engine, repo, templateRepo, generateRepo *Repository,
return nil
}

ext := filepath.Ext(path)
pathNoExt := strings.TrimSuffix(path, ext)

if !strings.HasSuffix(pathNoExt, "_template") {
return nil
}

newPath := fmt.Sprintf("%s%s", strings.TrimSuffix(pathNoExt, "_template"), ext)

content, err := ioutil.ReadFile(path)
if err != nil {
return err
}

if err := ioutil.WriteFile(newPath,
[]byte(generateExpansion(string(content), templateRepo, generateRepo)),
0644); err != nil {
return err
base := strings.TrimPrefix(path, tmpDir)
for _, g := range globs {
if g.Match(base) {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}

if err := ioutil.WriteFile(path,
[]byte(generateExpansion(string(content), templateRepo, generateRepo)),
0644); err != nil {
return err
}
break
}
}

return os.Remove(path)
return nil
}); err != nil {
return err
}
Expand Down