diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-12-16 13:17:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-16 13:17:55 +0800 |
commit | 59d6401486627b8f47a0c6b62599e65a40f84c92 (patch) | |
tree | 8ec9f3a32e151cb3b1fba8c30caa60b71db78b7c | |
parent | 43ada6557135919b354d7384c4be699b539b10cb (diff) | |
download | gitea-59d6401486627b8f47a0c6b62599e65a40f84c92.tar.gz gitea-59d6401486627b8f47a0c6b62599e65a40f84c92.zip |
Use ioutil.TmpDir for new created temp directory (#9368)
* Use os.TmpDir for new created temp directory
* fix error message
-rw-r--r-- | models/repo.go | 8 | ||||
-rw-r--r-- | models/repo_generate.go | 7 | ||||
-rw-r--r-- | services/pull/check.go | 12 |
3 files changed, 12 insertions, 15 deletions
diff --git a/models/repo.go b/models/repo.go index c7eee3c1ec..02cb3b9128 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1198,13 +1198,11 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C return err } - tmpDir := filepath.Join(os.TempDir(), "gitea-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) - // Initialize repository according to user's choice. if opts.AutoInit { - - if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil { - return fmt.Errorf("Failed to create dir %s: %v", tmpDir, err) + tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-"+repo.Name) + if err != nil { + return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.repoPath(e), err) } defer os.RemoveAll(tmpDir) diff --git a/models/repo_generate.go b/models/repo_generate.go index 6dd8540d9e..98ef0ea000 100644 --- a/models/repo_generate.go +++ b/models/repo_generate.go @@ -179,10 +179,9 @@ func generateRepoCommit(e Engine, repo, templateRepo, generateRepo *Repository, // generateRepository initializes repository from template func generateRepository(e Engine, repo, templateRepo, generateRepo *Repository) (err error) { - tmpDir := filepath.Join(os.TempDir(), "gitea-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond())) - - if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil { - return fmt.Errorf("Failed to create dir %s: %v", tmpDir, err) + tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-"+repo.Name) + if err != nil { + return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.repoPath(e), err) } defer func() { diff --git a/services/pull/check.go b/services/pull/check.go index db25361b6d..74185b6815 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -10,10 +10,7 @@ import ( "fmt" "io/ioutil" "os" - "path/filepath" - "strconv" "strings" - "time" "code.gitea.io/gitea/models" "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())) - defer os.Remove(indexTmpPath) + indexTmpPath, err := ioutil.TempDir(os.TempDir(), "gitea-"+pr.BaseRepo.Name) + 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() // 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 { // Errors are signaled by a non-zero status that is not 1 if strings.Contains(err.Error(), "exit status 1") { |