Use ioutil.TmpDir for new created temp directory (#9368)

* Use os.TmpDir for new created temp directory

* fix error message
This commit is contained in:
Lunny Xiao 2019-12-16 13:17:55 +08:00 committed by GitHub
parent 43ada65571
commit 59d6401486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 15 deletions

View File

@ -1198,13 +1198,11 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C
return err return err
} }
tmpDir := filepath.Join(os.TempDir(), "gitea-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond()))
// Initialize repository according to user's choice. // Initialize repository according to user's choice.
if opts.AutoInit { if opts.AutoInit {
tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-"+repo.Name)
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil { if err != nil {
return fmt.Errorf("Failed to create dir %s: %v", tmpDir, err) return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.repoPath(e), err)
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)

View File

@ -179,10 +179,9 @@ func generateRepoCommit(e Engine, repo, templateRepo, generateRepo *Repository,
// generateRepository initializes repository from template // generateRepository initializes repository from template
func generateRepository(e Engine, repo, templateRepo, generateRepo *Repository) (err error) { func generateRepository(e Engine, repo, templateRepo, generateRepo *Repository) (err error) {
tmpDir := filepath.Join(os.TempDir(), "gitea-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-"+repo.Name)
if err != nil {
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil { return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.repoPath(e), err)
return fmt.Errorf("Failed to create dir %s: %v", tmpDir, err)
} }
defer func() { defer func() {

View File

@ -10,10 +10,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strconv"
"strings" "strings"
"time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
@ -67,13 +64,16 @@ func getMergeCommit(pr *models.PullRequest) (*git.Commit, error) {
} }
} }
indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond())) indexTmpPath, err := ioutil.TempDir(os.TempDir(), "gitea-"+pr.BaseRepo.Name)
defer os.Remove(indexTmpPath) if err != nil {
return nil, fmt.Errorf("Failed to create temp dir for repository %s: %v", pr.BaseRepo.RepoPath(), err)
}
defer os.RemoveAll(indexTmpPath)
headFile := pr.GetGitRefName() headFile := pr.GetGitRefName()
// Check if a pull request is merged into BaseBranch // Check if a pull request is merged into BaseBranch
_, err := git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch).RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}) _, err = git.NewCommand("merge-base", "--is-ancestor", headFile, pr.BaseBranch).RunInDirWithEnv(pr.BaseRepo.RepoPath(), []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()})
if err != nil { if err != nil {
// Errors are signaled by a non-zero status that is not 1 // Errors are signaled by a non-zero status that is not 1
if strings.Contains(err.Error(), "exit status 1") { if strings.Contains(err.Error(), "exit status 1") {