Use git command instead of go git (#1005)

Remove go git library because it doesn't support sha256 repository but have an interface so that we could have other backend for the future.

Reviewed-on: https://gitea.com/gitea/tea/pulls/1005
Reviewed-by: Zettat123 <39446+zettat123@noreply.gitea.com>
This commit is contained in:
Lunny Xiao
2026-05-23 20:24:47 +00:00
parent 8e0666ab85
commit a664449282
19 changed files with 1113 additions and 380 deletions
+11 -17
View File
@@ -10,10 +10,6 @@ import (
"gitea.dev/tea/modules/config"
local_git "gitea.dev/tea/modules/git"
"github.com/go-git/go-git/v5"
git_config "github.com/go-git/go-git/v5/config"
git_plumbing "github.com/go-git/go-git/v5/plumbing"
)
// PullCheckout checkout current workdir to the head branch of specified pull request
@@ -76,7 +72,7 @@ func doPRFetch(
login *config.Login,
pr *gitea.PullRequest,
localRepo *local_git.TeaRepo,
localRemote *git.Remote,
localRemote *local_git.Remote,
callback func(string) (string, error),
) (string, error) {
localRemoteName := localRemote.Config().Name
@@ -90,25 +86,23 @@ func doPRFetch(
if err != nil {
return "", err
}
fetchOpts := &git.FetchOptions{Auth: auth}
refspecs := []string{}
if isRemoteDeleted(pr) {
// When the head branch is already deleted, pr.Head.Ref points to
// `refs/pull/<idx>/head`, where the commits stay available.
// This ref must be fetched explicitly, and does not allow pushing, so we use it
// only in this case as fallback.
localBranchName = fmt.Sprintf("pulls/%d", pr.Index)
fetchOpts.RefSpecs = []git_config.RefSpec{git_config.RefSpec(fmt.Sprintf("%s:refs/remotes/%s/%s",
refspecs = []string{fmt.Sprintf("%s:refs/remotes/%s/%s",
pr.Head.Ref,
localRemoteName,
localBranchName,
))}
)}
}
fmt.Printf("Fetching PR %v (head %s:%s) from remote '%s'\n", pr.Index, url, pr.Head.Ref, localRemoteName)
err = localRemote.Fetch(fetchOpts)
if err == git.NoErrAlreadyUpToDate {
fmt.Println(err)
} else if err != nil {
err = localRepo.Fetch(localRemoteName, refspecs, auth)
if err != nil {
return "", err
}
return localBranchName, nil
@@ -124,12 +118,12 @@ func doPRCheckout(
) error {
// determine the ref to checkout, depending on existence of a matching commit on a local branch
var info string
var checkoutRef git_plumbing.ReferenceName
var checkoutRef local_git.ReferenceName
if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
// if a matching branch exists, use that
checkoutRef = git_plumbing.NewBranchReferenceName(b.Name)
checkoutRef = local_git.NewBranchReferenceName(b.Name)
info = fmt.Sprintf("Found matching local branch %s, checking it out", checkoutRef.Short())
} else if forceCreateBranch {
@@ -139,10 +133,10 @@ func doPRCheckout(
if isRemoteDeleted(pr) {
localBranchName += "-" + pr.Head.Ref
}
checkoutRef = git_plumbing.NewBranchReferenceName(localBranchName)
checkoutRef = local_git.NewBranchReferenceName(localBranchName)
if err := localRepo.TeaCreateBranch(localBranchName, localRemoteBranchName, localRemoteName); err == nil {
info = fmt.Sprintf("Created branch '%s'\n", localBranchName)
} else if err == git.ErrBranchExists {
} else if err == local_git.ErrBranchExists {
info = "There may be changes since you last checked out, run `git pull` to get them."
} else {
return err
@@ -151,7 +145,7 @@ func doPRCheckout(
} else {
// use the remote tracking branch
checkoutRef = git_plumbing.NewRemoteReferenceName(localRemoteName, localRemoteBranchName)
checkoutRef = local_git.NewRemoteReferenceName(localRemoteName, localRemoteBranchName)
info = fmt.Sprintf(
"Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s",
checkoutRef.String(), localRemoteBranchName)
+2 -4
View File
@@ -10,8 +10,6 @@ import (
"gitea.dev/tea/modules/config"
local_git "gitea.dev/tea/modules/git"
git_config "github.com/go-git/go-git/v5/config"
git_plumbing "github.com/go-git/go-git/v5/plumbing"
)
// PullClean deletes local & remote feature-branches for a closed pull
@@ -51,7 +49,7 @@ func PullClean(login *config.Login, repoOwner, repoName string, index int64, ign
}
// find a branch with matching sha or name, that has a remote matching the repo url
var branch *git_config.Branch
var branch *local_git.Branch
if ignoreSHA {
branch, err = r.TeaFindBranchByName(remoteBranch, pr.Head.Repository.CloneURL)
} else {
@@ -77,7 +75,7 @@ call me again with the --ignore-sha flag`, remoteBranch)
}
if headRef.Name().Short() == branch.Name {
fmt.Printf("Checking out '%s' to delete local branch '%s'\n", defaultBranch, branch.Name)
ref := git_plumbing.NewBranchReferenceName(defaultBranch)
ref := local_git.NewBranchReferenceName(defaultBranch)
if err = r.TeaCheckout(ref); err != nil {
return err
}
+4 -27
View File
@@ -4,17 +4,12 @@
package task
import (
"fmt"
"net/url"
"code.gitea.io/sdk/gitea"
"gitea.dev/tea/modules/config"
local_git "gitea.dev/tea/modules/git"
"github.com/go-git/go-git/v5"
git_config "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
)
// RepoClone creates a local git clone in the given path, and sets up upstream remote
@@ -46,12 +41,7 @@ func RepoClone(
path = repoName
}
repo, err := git.PlainClone(path, false, &git.CloneOptions{
URL: originURL.String(),
Auth: auth,
Depth: depth,
InsecureSkipTLS: login.Insecure,
})
repo, err := local_git.Clone(path, originURL.String(), auth, depth, login.Insecure)
if err != nil {
return nil, err
}
@@ -63,28 +53,15 @@ func RepoClone(
return nil, err
}
upstreamBranch := repoMeta.Parent.DefaultBranch
_, err = repo.CreateRemote(&git_config.RemoteConfig{
Name: "upstream",
URLs: []string{upstreamURL.String()},
})
if err != nil {
if err = repo.AddRemote("upstream", upstreamURL.String()); err != nil {
return nil, err
}
repoConf, err := repo.Config()
if err != nil {
return nil, err
}
if b, ok := repoConf.Branches[upstreamBranch]; ok {
b.Remote = "upstream"
b.Merge = plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", upstreamBranch))
}
if err = repo.SetConfig(repoConf); err != nil {
if err = repo.SetBranchUpstream(upstreamBranch, "upstream", upstreamBranch); err != nil {
return nil, err
}
}
return &local_git.TeaRepo{Repository: repo}, nil
return repo, nil
}
func cloneURL(repo *gitea.Repository, login *config.Login) (*url.URL, error) {