Skip to content

Commit 2a832b3

Browse files
committed
some improvements
1 parent 98175f1 commit 2a832b3

File tree

9 files changed

+30
-20
lines changed

9 files changed

+30
-20
lines changed

modules/git/repo_commit_gogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func (repo *Repository) SetReference(name, commitID string) error {
4141
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
4242
}
4343

44-
// RemoveReference removes the given reference (e.g. branch or tag).
45-
func (repo *Repository) RemoveReference(name string) error {
44+
// removeReference removes the given reference (e.g. branch or tag).
45+
func (repo *Repository) removeReference(name string) error {
4646
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
4747
}
4848

modules/git/repo_commit_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func (repo *Repository) SetReference(name, commitID string) error {
5656
return err
5757
}
5858

59-
// RemoveReference removes the given reference (e.g. branch or tag).
60-
func (repo *Repository) RemoveReference(name string) error {
59+
// removeReference removes the given reference (e.g. branch or tag).
60+
func (repo *Repository) removeReference(name string) error {
6161
_, _, err := NewCommand("update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
6262
return err
6363
}

modules/git/repo_compare_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestReadWritePullHead(t *testing.T) {
116116
assert.Equal(t, headContents, newCommit)
117117

118118
// Remove file after the test
119-
err = repo.RemoveReference(PullPrefix + "1/head")
119+
err = repo.removeReference(PullPrefix + "1/head")
120120
assert.NoError(t, err)
121121
}
122122

modules/gitrepo/reference.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
)
11+
12+
func RemoveReference(ctx context.Context, repo Repository, refName string) error {
13+
_, _, err := git.NewCommand("update-ref", "--no-deref", "-d").
14+
AddDynamicArguments(refName).
15+
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})
16+
return err
17+
}

routers/api/v1/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ func DeleteIssue(ctx *context.APIContext) {
979979
return
980980
}
981981

982-
if err = issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil {
982+
if err = issue_service.DeleteIssue(ctx, ctx.Doer, issue); err != nil {
983983
ctx.APIErrorInternal(err)
984984
return
985985
}

routers/web/repo/issue_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func BatchDeleteIssues(ctx *context.Context) {
398398
return
399399
}
400400
for _, issue := range issues {
401-
if err := issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil {
401+
if err := issue_service.DeleteIssue(ctx, ctx.Doer, issue); err != nil {
402402
ctx.ServerError("DeleteIssue", err)
403403
return
404404
}

routers/web/repo/issue_new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func DeleteIssue(ctx *context.Context) {
216216
return
217217
}
218218

219-
if err := issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil {
219+
if err := issue_service.DeleteIssue(ctx, ctx.Doer, issue); err != nil {
220220
ctx.ServerError("DeleteIssueByID", err)
221221
return
222222
}

services/issue/issue.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/container"
1919
"code.gitea.io/gitea/modules/git"
20+
"code.gitea.io/gitea/modules/gitrepo"
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/storage"
2223
notify_service "code.gitea.io/gitea/services/notify"
@@ -180,7 +181,7 @@ func UpdateAssignees(ctx context.Context, issue *issues_model.Issue, oneAssignee
180181
}
181182

182183
// DeleteIssue deletes an issue
183-
func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue) error {
184+
func DeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) error {
184185
// load issue before deleting it
185186
if err := issue.LoadAttributes(ctx); err != nil {
186187
return err
@@ -199,8 +200,8 @@ func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Reposi
199200
}
200201

201202
// delete pull request related git data
202-
if issue.IsPull && gitRepo != nil {
203-
if err := gitRepo.RemoveReference(issue.PullRequest.GetGitHeadRefName()); err != nil {
203+
if issue.IsPull {
204+
if err := gitrepo.RemoveReference(ctx, issue.Repo, issue.PullRequest.GetGitHeadRefName()); err != nil {
204205
return err
205206
}
206207
}

services/pull/pull.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
104104

105105
assigneeCommentMap := make(map[int64]*issues_model.Comment)
106106

107-
// add first push codes comment
108-
baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo)
109-
if err != nil {
110-
return err
111-
}
112-
defer baseGitRepo.Close()
113-
114107
var reviewNotifiers []*issue_service.ReviewRequestNotifier
115108
if err := db.WithTx(ctx, func(ctx context.Context) error {
116109
if err := issues_model.NewPullRequest(ctx, repo, issue, labelIDs, uuids, pr); err != nil {
@@ -162,12 +155,11 @@ func NewPullRequest(ctx context.Context, opts *NewPullRequestOptions) error {
162155
return nil
163156
}); err != nil {
164157
// cleanup: this will only remove the reference, the real commit will be clean up when next GC
165-
if err1 := baseGitRepo.RemoveReference(pr.GetGitHeadRefName()); err1 != nil {
158+
if err1 := gitrepo.RemoveReference(ctx, pr.BaseRepo, pr.GetGitHeadRefName()); err1 != nil {
166159
log.Error("RemoveReference: %v", err1)
167160
}
168161
return err
169162
}
170-
baseGitRepo.Close() // close immediately to avoid notifications will open the repository again
171163

172164
issue_service.ReviewRequestNotify(ctx, issue, issue.Poster, reviewNotifiers)
173165

0 commit comments

Comments
 (0)