diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-09 00:46:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 18:46:32 +0200 |
commit | 4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65 (patch) | |
tree | fee132403adea441987ce25809c764e00fd962cd /modules/repository | |
parent | d4834071da9ce010fd4677ef339c598dd23dc130 (diff) | |
download | gitea-4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65.tar.gz gitea-4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65.zip |
Move some helper files out of models (#19355)
* Move some helper files out of models
* Some improvements
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'modules/repository')
-rw-r--r-- | modules/repository/env.go | 79 | ||||
-rw-r--r-- | modules/repository/init.go | 2 | ||||
-rw-r--r-- | modules/repository/temp.go | 47 |
3 files changed, 127 insertions, 1 deletions
diff --git a/modules/repository/env.go b/modules/repository/env.go new file mode 100644 index 0000000000..e86e0d4535 --- /dev/null +++ b/modules/repository/env.go @@ -0,0 +1,79 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repository + +import ( + "fmt" + "os" + "strings" + + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" +) + +// env keys for git hooks need +const ( + EnvRepoName = "GITEA_REPO_NAME" + EnvRepoUsername = "GITEA_REPO_USER_NAME" + EnvRepoID = "GITEA_REPO_ID" + EnvRepoIsWiki = "GITEA_REPO_IS_WIKI" + EnvPusherName = "GITEA_PUSHER_NAME" + EnvPusherEmail = "GITEA_PUSHER_EMAIL" + EnvPusherID = "GITEA_PUSHER_ID" + EnvKeyID = "GITEA_KEY_ID" // public key ID + EnvDeployKeyID = "GITEA_DEPLOY_KEY_ID" + EnvPRID = "GITEA_PR_ID" + EnvIsInternal = "GITEA_INTERNAL_PUSH" + EnvAppURL = "GITEA_ROOT_URL" +) + +// InternalPushingEnvironment returns an os environment to switch off hooks on push +// It is recommended to avoid using this unless you are pushing within a transaction +// or if you absolutely are sure that post-receive and pre-receive will do nothing +// We provide the full pushing-environment for other hook providers +func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string { + return append(PushingEnvironment(doer, repo), + EnvIsInternal+"=true", + ) +} + +// PushingEnvironment returns an os environment to allow hooks to work on push +func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string { + return FullPushingEnvironment(doer, doer, repo, repo.Name, 0) +} + +// FullPushingEnvironment returns an os environment to allow hooks to work on push +func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID int64) []string { + isWiki := "false" + if strings.HasSuffix(repoName, ".wiki") { + isWiki = "true" + } + + authorSig := author.NewGitSig() + committerSig := committer.NewGitSig() + + environ := append(os.Environ(), + "GIT_AUTHOR_NAME="+authorSig.Name, + "GIT_AUTHOR_EMAIL="+authorSig.Email, + "GIT_COMMITTER_NAME="+committerSig.Name, + "GIT_COMMITTER_EMAIL="+committerSig.Email, + EnvRepoName+"="+repoName, + EnvRepoUsername+"="+repo.OwnerName, + EnvRepoIsWiki+"="+isWiki, + EnvPusherName+"="+committer.Name, + EnvPusherID+"="+fmt.Sprintf("%d", committer.ID), + EnvRepoID+"="+fmt.Sprintf("%d", repo.ID), + EnvPRID+"="+fmt.Sprintf("%d", prID), + EnvAppURL+"="+setting.AppURL, + "SSH_ORIGINAL_COMMAND=gitea-internal", + ) + + if !committer.KeepEmailPrivate { + environ = append(environ, EnvPusherEmail+"="+committer.Email) + } + + return environ +} diff --git a/modules/repository/init.go b/modules/repository/init.go index 515992f97d..845a61ed0a 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -360,7 +360,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi if stdout, _, err := git.NewCommand(ctx, "push", "origin", "HEAD:"+defaultBranch). SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)). - RunStdString(&git.RunOpts{Dir: tmpPath, Env: models.InternalPushingEnvironment(u, repo)}); err != nil { + RunStdString(&git.RunOpts{Dir: tmpPath, Env: InternalPushingEnvironment(u, repo)}); err != nil { log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err) return fmt.Errorf("git push: %v", err) } diff --git a/modules/repository/temp.go b/modules/repository/temp.go new file mode 100644 index 0000000000..5947d29965 --- /dev/null +++ b/modules/repository/temp.go @@ -0,0 +1,47 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repository + +import ( + "fmt" + "os" + "path" + "path/filepath" + + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" +) + +// LocalCopyPath returns the local repository temporary copy path. +func LocalCopyPath() string { + if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) { + return setting.Repository.Local.LocalCopyPath + } + return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath) +} + +// CreateTemporaryPath creates a temporary path +func CreateTemporaryPath(prefix string) (string, error) { + if err := os.MkdirAll(LocalCopyPath(), os.ModePerm); err != nil { + log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err) + return "", fmt.Errorf("Failed to create localcopypath directory %s: %v", LocalCopyPath(), err) + } + basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git") + if err != nil { + log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err) + return "", fmt.Errorf("Failed to create dir %s-*.git: %v", prefix, err) + + } + return basePath, nil +} + +// RemoveTemporaryPath removes the temporary path +func RemoveTemporaryPath(basePath string) error { + if _, err := os.Stat(basePath); !os.IsNotExist(err) { + return util.RemoveAll(basePath) + } + return nil +} |