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.
This commit is contained in:
Lunny Xiao
2025-09-19 08:04:18 -07:00
committed by GitHub
parent 9a0ec53ee3
commit 198f37e33c
11 changed files with 46 additions and 47 deletions

View File

@@ -38,16 +38,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
return ref.Hash().String(), nil
}
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
func (repo *Repository) SetReference(name, commitID string) error {
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
}
// RemoveReference removes the given reference (e.g. branch or tag).
func (repo *Repository) RemoveReference(name string) error {
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
}
// ConvertToHash returns a Hash object from a potential ID string
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
objectFormat, err := repo.GetObjectFormat()

View File

@@ -51,18 +51,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
return string(shaBs), nil
}
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
func (repo *Repository) SetReference(name, commitID string) error {
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(name, commitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
return err
}
// RemoveReference removes the given reference (e.g. branch or tag).
func (repo *Repository) RemoveReference(name string) error {
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
return err
}
// IsCommitExist returns true if given commit exists in current repository.
func (repo *Repository) IsCommitExist(name string) bool {
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {

View File

@@ -9,6 +9,8 @@ import (
"path/filepath"
"testing"
"code.gitea.io/gitea/modules/git/gitcmd"
"github.com/stretchr/testify/assert"
)
@@ -99,7 +101,9 @@ func TestReadWritePullHead(t *testing.T) {
// Write a fake sha1 with only 40 zeros
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
err = repo.SetReference(PullPrefix+"1/head", newCommit)
_, _, err = gitcmd.NewCommand("update-ref").
AddDynamicArguments(PullPrefix+"1/head", newCommit).
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
if err != nil {
assert.NoError(t, err)
return
@@ -116,7 +120,9 @@ func TestReadWritePullHead(t *testing.T) {
assert.Equal(t, headContents, newCommit)
// Remove file after the test
err = repo.RemoveReference(PullPrefix + "1/head")
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref", "-d").
AddDynamicArguments(PullPrefix+"1/head").
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
assert.NoError(t, err)
}

21
modules/gitrepo/ref.go Normal file
View File

@@ -0,0 +1,21 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"code.gitea.io/gitea/modules/git/gitcmd"
)
func UpdateRef(ctx context.Context, repo Repository, refName, newCommitID string) error {
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(refName, newCommitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
return err
}
func RemoveRef(ctx context.Context, repo Repository, refName string) error {
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").
AddDynamicArguments(refName).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
return err
}