From c18cc1428eebe65fca34e09705f3b6e25f00f0ea Mon Sep 17 00:00:00 2001 From: Benjamin Tietz Date: Thu, 23 Mar 2023 16:00:13 +0000 Subject: [PATCH] Option to init empty wiki Git remote repository upon repository creation This fixes github issue #7226 --- conf/app.ini | 2 ++ internal/cmd/serv.go | 15 +++++++++++++++ internal/conf/static.go | 1 + 3 files changed, 18 insertions(+) diff --git a/conf/app.ini b/conf/app.ini index 955c00721..d7244a7ea 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -119,6 +119,8 @@ ENABLE_RAW_FILE_RENDER_MODE = false COMMITS_FETCH_CONCURRENCY = 0 ; Default branch name when creating new repositories. DEFAULT_BRANCH = master +; Wether to automatically create wiki repos if they do not exist, but are enabled +AUTOCREATE_WIKI = true [repository.editor] ; List of file extensions that should have line wraps in the CodeMirror editor. diff --git a/internal/cmd/serv.go b/internal/cmd/serv.go index accffc94f..771539ebf 100644 --- a/internal/cmd/serv.go +++ b/internal/cmd/serv.go @@ -10,9 +10,11 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "time" + "github.com/gogs/git-module" "github.com/unknwon/com" "github.com/urfave/cli" log "unknwon.dev/clog/v2" @@ -132,6 +134,8 @@ var allowedCommands = map[string]db.AccessMode{ "git-receive-pack": db.AccessModeWrite, } +var reWikiRepo = regexp.MustCompile("\\.wiki(?:\\.git)?$") + func runServ(c *cli.Context) error { ctx := context.Background() setup(c, "serv.log", true) @@ -254,6 +258,17 @@ func runServ(c *cli.Context) error { verb = strings.Replace(verb, "-", " ", 1) } + repoFsPath := fmt.Sprintf("%s/%s.git", conf.Repository.Root, strings.TrimSuffix(repoFullName, ".git")) + if _, err := os.Stat(repoFsPath); err != nil { + if conf.Repository.AutocreateWiki && repo.EnableWiki && reWikiRepo.MatchString(repoFullName) && requestMode == db.AccessModeWrite { + if err = git.Init(repoFsPath, git.InitOptions{ Bare: true }); err != nil { + fail("Internal error", "Failed to initialize new git repo %s: %v", repoFullName, err) + } + } else { + fail("Internal error", "Repository %v doesn't exist!", repoFullName) + } + } + var gitCmd *exec.Cmd verbs := strings.Split(verb, " ") if len(verbs) == 2 { diff --git a/internal/conf/static.go b/internal/conf/static.go index 016fd139c..1356b3f14 100644 --- a/internal/conf/static.go +++ b/internal/conf/static.go @@ -323,6 +323,7 @@ type RepositoryOpts struct { EnableRawFileRenderMode bool CommitsFetchConcurrency int DefaultBranch string + AutocreateWiki bool // Repository editor settings Editor struct {