Skip to content

Commit 1434bf7

Browse files
committed
Remove WithTemporaryFile
1 parent c6419be commit 1434bf7

File tree

4 files changed

+216
-206
lines changed

4 files changed

+216
-206
lines changed

models/helper_directory.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ func CreateTemporaryPath(prefix string) (string, error) {
3535
return basePath, nil
3636
}
3737

38-
// WithTemporaryPath takes a prefix and callback function to run with a temporary path
39-
func WithTemporaryPath(prefix string, callback func(string) error) error {
40-
basePath, err := CreateTemporaryPath(prefix)
41-
if err != nil {
42-
return err
38+
// RemoveTemporaryPath removes the temporary path
39+
func RemoveTemporaryPath(basePath string) error {
40+
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
41+
return os.RemoveAll(basePath)
4342
}
44-
defer func() {
45-
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
46-
os.RemoveAll(basePath)
47-
}
48-
}()
49-
return callback(basePath)
43+
return nil
5044
}

models/pull.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,12 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
414414
}()
415415

416416
// Clone base repo.
417-
return WithTemporaryPath("merge", func(tmpBasePath string) error {
418-
return pr.doPullRequestMerge(tmpBasePath, doer, baseGitRepo, mergeStyle, message)
419-
})
420-
}
417+
tmpBasePath, err := CreateTemporaryPath("merge")
418+
if err != nil {
419+
return err
420+
}
421+
defer RemoveTemporaryPath(tmpBasePath)
421422

422-
func (pr *PullRequest) doPullRequestMerge(tmpBasePath string, doer *User, baseGitRepo *git.Repository, mergeStyle MergeStyle, message string) error {
423423
headRepoPath := RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
424424

425425
if err := git.Clone(baseGitRepo.Path, tmpBasePath, git.CloneRepoOptions{

models/repo_branch.go

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,40 @@ func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName st
101101
}
102102
}
103103

104-
return WithTemporaryPath("branch-maker", func(basePath string) error {
105-
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
106-
Bare: true,
107-
Shared: true,
108-
}); err != nil {
109-
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
110-
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
111-
}
104+
basePath, err := CreateTemporaryPath("branch-maker")
105+
if err != nil {
106+
return err
107+
}
108+
defer RemoveTemporaryPath(basePath)
109+
110+
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
111+
Bare: true,
112+
Shared: true,
113+
}); err != nil {
114+
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
115+
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
116+
}
112117

113-
gitRepo, err := git.OpenRepository(basePath)
114-
if err != nil {
115-
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
116-
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
117-
}
118+
gitRepo, err := git.OpenRepository(basePath)
119+
if err != nil {
120+
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
121+
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
122+
}
118123

119-
if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
120-
log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
121-
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
122-
}
124+
if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
125+
log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
126+
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
127+
}
123128

124-
if err = git.Push(basePath, git.PushOptions{
125-
Remote: "origin",
126-
Branch: branchName,
127-
Env: PushingEnvironment(doer, repo),
128-
}); err != nil {
129-
return fmt.Errorf("Push: %v", err)
130-
}
129+
if err = git.Push(basePath, git.PushOptions{
130+
Remote: "origin",
131+
Branch: branchName,
132+
Env: PushingEnvironment(doer, repo),
133+
}); err != nil {
134+
return fmt.Errorf("Push: %v", err)
135+
}
131136

132-
return nil
133-
})
137+
return nil
134138
}
135139

136140
// CreateNewBranchFromCommit creates a new repository branch
@@ -139,36 +143,40 @@ func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName
139143
if err := repo.CheckBranchName(branchName); err != nil {
140144
return err
141145
}
142-
return WithTemporaryPath("branch-maker", func(basePath string) error {
143-
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
144-
Bare: true,
145-
Shared: true,
146-
}); err != nil {
147-
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
148-
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
149-
}
146+
basePath, err := CreateTemporaryPath("branch-maker")
147+
if err != nil {
148+
return err
149+
}
150+
defer RemoveTemporaryPath(basePath)
151+
152+
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
153+
Bare: true,
154+
Shared: true,
155+
}); err != nil {
156+
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
157+
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
158+
}
150159

151-
gitRepo, err := git.OpenRepository(basePath)
152-
if err != nil {
153-
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
154-
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
155-
}
160+
gitRepo, err := git.OpenRepository(basePath)
161+
if err != nil {
162+
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
163+
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
164+
}
156165

157-
if err = gitRepo.CreateBranch(branchName, commit); err != nil {
158-
log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
159-
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
160-
}
166+
if err = gitRepo.CreateBranch(branchName, commit); err != nil {
167+
log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
168+
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
169+
}
161170

162-
if err = git.Push(basePath, git.PushOptions{
163-
Remote: "origin",
164-
Branch: branchName,
165-
Env: PushingEnvironment(doer, repo),
166-
}); err != nil {
167-
return fmt.Errorf("Push: %v", err)
168-
}
171+
if err = git.Push(basePath, git.PushOptions{
172+
Remote: "origin",
173+
Branch: branchName,
174+
Env: PushingEnvironment(doer, repo),
175+
}); err != nil {
176+
return fmt.Errorf("Push: %v", err)
177+
}
169178

170-
return nil
171-
})
179+
return nil
172180
}
173181

174182
// GetCommit returns all the commits of a branch

0 commit comments

Comments
 (0)