Skip to content

Commit 198f37e

Browse files
authored
Move updateref and removeref to gitrepo and remove unnecessary open repository (#35511)
Extracted from #35077 `UpdateRef` and `RemoveRef` will call git commands even for gogit version.
1 parent 9a0ec53 commit 198f37e

File tree

11 files changed

+46
-47
lines changed

11 files changed

+46
-47
lines changed

modules/git/repo_commit_gogit.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
3838
return ref.Hash().String(), nil
3939
}
4040

41-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
42-
func (repo *Repository) SetReference(name, commitID string) error {
43-
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
44-
}
45-
46-
// RemoveReference removes the given reference (e.g. branch or tag).
47-
func (repo *Repository) RemoveReference(name string) error {
48-
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
49-
}
50-
5141
// ConvertToHash returns a Hash object from a potential ID string
5242
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
5343
objectFormat, err := repo.GetObjectFormat()

modules/git/repo_commit_nogogit.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
5151
return string(shaBs), nil
5252
}
5353

54-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
55-
func (repo *Repository) SetReference(name, commitID string) error {
56-
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(name, commitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
57-
return err
58-
}
59-
60-
// RemoveReference removes the given reference (e.g. branch or tag).
61-
func (repo *Repository) RemoveReference(name string) error {
62-
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
63-
return err
64-
}
65-
6654
// IsCommitExist returns true if given commit exists in current repository.
6755
func (repo *Repository) IsCommitExist(name string) bool {
6856
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {

modules/git/repo_compare_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/git/gitcmd"
13+
1214
"github.com/stretchr/testify/assert"
1315
)
1416

@@ -99,7 +101,9 @@ func TestReadWritePullHead(t *testing.T) {
99101

100102
// Write a fake sha1 with only 40 zeros
101103
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
102-
err = repo.SetReference(PullPrefix+"1/head", newCommit)
104+
_, _, err = gitcmd.NewCommand("update-ref").
105+
AddDynamicArguments(PullPrefix+"1/head", newCommit).
106+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
103107
if err != nil {
104108
assert.NoError(t, err)
105109
return
@@ -116,7 +120,9 @@ func TestReadWritePullHead(t *testing.T) {
116120
assert.Equal(t, headContents, newCommit)
117121

118122
// Remove file after the test
119-
err = repo.RemoveReference(PullPrefix + "1/head")
123+
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref", "-d").
124+
AddDynamicArguments(PullPrefix+"1/head").
125+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
120126
assert.NoError(t, err)
121127
}
122128

modules/gitrepo/ref.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/gitcmd"
10+
)
11+
12+
func UpdateRef(ctx context.Context, repo Repository, refName, newCommitID string) error {
13+
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(refName, newCommitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
14+
return err
15+
}
16+
17+
func RemoveRef(ctx context.Context, repo Repository, refName string) error {
18+
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").
19+
AddDynamicArguments(refName).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
20+
return err
21+
}

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
}

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func GetMergedBaseCommitID(ctx *context.Context, issue *issues_model.Issue) stri
224224
commitSHA, err = ctx.Repo.GitRepo.ReadPatchCommit(pull.Index)
225225
if err == nil {
226226
// Recreate pull head in files for next time
227-
if err := ctx.Repo.GitRepo.SetReference(pull.GetGitHeadRefName(), commitSHA); err != nil {
227+
if err := gitrepo.UpdateRef(ctx, ctx.Repo.Repository, pull.GetGitHeadRefName(), commitSHA); err != nil {
228228
log.Error("Could not write head file", err)
229229
}
230230
} else {

services/issue/issue.go

Lines changed: 7 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,11 @@ 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 := issue.PullRequest.LoadBaseRepo(ctx); err != nil {
205+
return err
206+
}
207+
if err := gitrepo.RemoveRef(ctx, issue.PullRequest.BaseRepo, issue.PullRequest.GetGitHeadRefName()); err != nil {
204208
return err
205209
}
206210
}

services/migrations/gitea_uploader.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,7 @@ func (g *GiteaLocalUploader) updateGitForPullRequest(ctx context.Context, pr *ba
682682
pr.Head.SHA = headSha
683683
}
684684

685-
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref").AddDynamicArguments(pr.GetGitHeadRefName(), pr.Head.SHA).RunStdString(ctx, &gitcmd.RunOpts{Dir: g.repo.RepoPath()})
686-
if err != nil {
685+
if err = gitrepo.UpdateRef(ctx, g.repo, pr.GetGitHeadRefName(), pr.Head.SHA); err != nil {
687686
return "", err
688687
}
689688

@@ -705,8 +704,7 @@ func (g *GiteaLocalUploader) updateGitForPullRequest(ctx context.Context, pr *ba
705704
log.Warn("Deprecated local head %s for PR #%d in %s/%s, removing %s", pr.Head.SHA, pr.Number, g.repoOwner, g.repoName, pr.GetGitHeadRefName())
706705
} else {
707706
// set head information
708-
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref").AddDynamicArguments(pr.GetGitHeadRefName(), pr.Head.SHA).RunStdString(ctx, &gitcmd.RunOpts{Dir: g.repo.RepoPath()})
709-
if err != nil {
707+
if err = gitrepo.UpdateRef(ctx, g.repo, pr.GetGitHeadRefName(), pr.Head.SHA); err != nil {
710708
log.Error("unable to set %s as the local head for PR #%d from %s in %s/%s. Error: %v", pr.Head.SHA, pr.Number, pr.Head.Ref, g.repoOwner, g.repoName, err)
711709
}
712710
}

0 commit comments

Comments
 (0)