Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move git command run on a special repository to standalone methods to hide the implemention detail #28940

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
}
log.Trace("Processing next %d repos of %d", len(repos), count)
for _, repo := range repos {
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), gitrepo.RepoGitURL(repo))
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
if err != nil {
log.Warn("OpenRepository: %v", err)
Expand Down
9 changes: 5 additions & 4 deletions models/git/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -443,9 +444,9 @@ func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error {
return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
}

repoPath := opts.Repo.RepoPath()
repoURL := gitrepo.RepoGitURL(opts.Repo)
if opts.Creator == nil {
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoURL, opts.SHA)
}

ctx, committer, err := db.TxContext(ctx)
Expand All @@ -467,13 +468,13 @@ func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error {
opts.CommitStatus.CreatorID = opts.Creator.ID
opts.CommitStatus.RepoID = opts.Repo.ID
opts.CommitStatus.Index = idx
log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index)
log.Debug("NewCommitStatus[%s, %s]: %d", repoURL, opts.SHA, opts.CommitStatus.Index)

opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)

// Insert new CommitStatus
if _, err = db.GetEngine(ctx).Insert(opts.CommitStatus); err != nil {
return fmt.Errorf("insert CommitStatus[%s, %s]: %w", repoPath, opts.SHA, err)
return fmt.Errorf("insert CommitStatus[%s, %s]: %w", repoURL, opts.SHA, err)
}

return committer.Commit()
Expand Down
7 changes: 3 additions & 4 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,9 @@ func (repo *Repository) MustOwner(ctx context.Context) *user_model.User {
func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
if len(repo.RenderingMetas) == 0 {
metas := map[string]string{
"user": repo.OwnerName,
"repo": repo.Name,
"repoPath": repo.RepoPath(),
"mode": "comment",
"user": repo.OwnerName,
"repo": repo.Name,
"mode": "comment",
}

unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)
Expand Down
4 changes: 4 additions & 0 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ func IsErrorExitCode(err error, code int) bool {
return false
}

func NewRunStdError(err error, stderr string) RunStdError {
return &runStdError{err: err, stderr: stderr}
}

// RunStdString runs the command with options and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
func (c *Command) RunStdString(opts *RunOpts) (stdout, stderr string, runErr RunStdError) {
stdoutBytes, stderrBytes, err := c.RunStdBytes(opts)
Expand Down
21 changes: 1 addition & 20 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
if err != nil {
return nil, err
}
parent, err := c.repo.getCommit(id)
parent, err := c.repo.GetCommitByObjectID(id)

Check failure on line 67 in modules/git/commit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

c.repo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -142,25 +142,6 @@
return err
}

// AllCommitsCount returns count of all commits in repository
func AllCommitsCount(ctx context.Context, repoPath string, hidePRRefs bool, files ...string) (int64, error) {
cmd := NewCommand(ctx, "rev-list")
if hidePRRefs {
cmd.AddArguments("--exclude=" + PullPrefix + "*")
}
cmd.AddArguments("--all", "--count")
if len(files) > 0 {
cmd.AddDashesAndList(files...)
}

stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
if err != nil {
return 0, err
}

return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
}

// CommitsCountOptions the options when counting commits
type CommitsCountOptions struct {
RepoPath string
Expand Down
5 changes: 0 additions & 5 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,3 @@ func configUnsetAll(key, value string) error {
}
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
}

// Fsck verifies the connectivity and validity of the objects in the database
func Fsck(ctx context.Context, repoPath string, timeout time.Duration, args TrustedCmdArgs) error {
return NewCommand(ctx, "fsck").AddArguments(args...).Run(&RunOpts{Timeout: timeout, Dir: repoPath})
}
2 changes: 1 addition & 1 deletion modules/git/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

// Commit return the commit of the reference
func (ref *Reference) Commit() (*Commit, error) {
return ref.repo.getCommit(ref.Object)
return ref.repo.GetCommitByObjectID(ref.Object)

Check failure on line 53 in modules/git/ref.go

View workflow job for this annotation

GitHub Actions / test-sqlite

ref.repo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
}

// ShortName returns the short name of the reference
Expand Down
39 changes: 7 additions & 32 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package git
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
Expand All @@ -33,11 +32,6 @@ type GPGSettings struct {

const prettyLogFormat = `--pretty=format:%H`

// GetAllCommitsCount returns count of all commits in repository
func (repo *Repository) GetAllCommitsCount() (int64, error) {
return AllCommitsCount(repo.Ctx, repo.Path, false)
}

func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
var commits []*Commit
if len(logs) == 0 {
Expand All @@ -63,32 +57,6 @@ func IsRepoURLAccessible(ctx context.Context, url string) bool {
return err == nil
}

// GetObjectFormatOfRepo returns the hash type of repository at a given path
func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat, error) {
var stdout, stderr strings.Builder

err := NewCommand(ctx, "hash-object", "--stdin").Run(&RunOpts{
Dir: repoPath,
Stdout: &stdout,
Stderr: &stderr,
Stdin: &strings.Reader{},
})
if err != nil {
return nil, err
}

if stderr.Len() > 0 {
return nil, errors.New(stderr.String())
}

h, err := NewIDFromString(strings.TrimRight(stdout.String(), "\n"))
if err != nil {
return nil, err
}

return h.Type(), nil
}

// InitRepository initializes a new Git repository.
func InitRepository(ctx context.Context, repoPath string, bare bool, objectFormatName string) error {
err := os.MkdirAll(repoPath, os.ModePerm)
Expand Down Expand Up @@ -343,3 +311,10 @@ func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.
_, err = io.Copy(out, fi)
return err
}

// GetRelativePath FIXME
func (repo *Repository) GetRelativePath() string {
repoName := filepath.Base(repo.Path)
ownerName := filepath.Base(filepath.Dir(repo.Path))
return ownerName + "/" + repoName
}
11 changes: 0 additions & 11 deletions modules/git/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import (
// BranchPrefix base dir of the branch information file store on git
const BranchPrefix = "refs/heads/"

// IsReferenceExist returns true if given reference exists in the repository.
func IsReferenceExist(ctx context.Context, repoPath, name string) bool {
_, _, err := NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name).RunStdString(&RunOpts{Dir: repoPath})
return err == nil
}

// IsBranchExist returns true if given branch exists in the repository.
func IsBranchExist(ctx context.Context, repoPath, name string) bool {
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
}

// Branch represents a Git branch.
type Branch struct {
Name string
Expand Down
8 changes: 4 additions & 4 deletions modules/git/repo_branch_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func (repo *Repository) IsObjectExist(name string) bool {
}

// IsReferenceExist returns true if given reference exists in the repository.
func (repo *Repository) IsReferenceExist(name string) bool {
func (repo *Repository) IsReferenceExist(name string) (bool, error) {
if name == "" {
return false
return false, nil
}

reference, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
if err != nil {
return false
return false, err
}
return reference.Type() != plumbing.InvalidReference
return reference.Type() != plumbing.InvalidReference, nil
}

// IsBranchExist returns true if given branch exists in current repository.
Expand Down
11 changes: 6 additions & 5 deletions modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ func (repo *Repository) IsObjectExist(name string) bool {
}

// IsReferenceExist returns true if given reference exists in the repository.
func (repo *Repository) IsReferenceExist(name string) bool {
func (repo *Repository) IsReferenceExist(name string) (bool, error) {
if name == "" {
return false
return false, nil
}

wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
defer cancel()
_, err := wr.Write([]byte(name + "\n"))
if err != nil {
log.Debug("Error writing to CatFileBatchCheck %v", err)
return false
return false, err
}
_, _, _, err = ReadBatchLine(rd)
return err == nil
return err == nil, err
}

// IsBranchExist returns true if given branch exists in current repository.
Expand All @@ -56,7 +56,8 @@ func (repo *Repository) IsBranchExist(name string) bool {
return false
}

return repo.IsReferenceExist(BranchPrefix + name)
exist, _ := repo.IsReferenceExist(BranchPrefix + name)
return exist
}

// GetBranchNames returns branches from the repository, skipping "skip" initial branches and
Expand Down
6 changes: 3 additions & 3 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
return nil, err
}

return repo.getCommit(id)
return repo.GetCommitByObjectID(id)

Check failure on line 34 in modules/git/repo_commit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

repo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
}

// GetBranchCommit returns the last commit of given branch.
Expand Down Expand Up @@ -68,7 +68,7 @@
return nil, err
}

return repo.getCommit(id)
return repo.GetCommitByObjectID(id)

Check failure on line 71 in modules/git/repo_commit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

repo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
}

// GetCommitByPath returns the last commit of relative path.
Expand Down Expand Up @@ -266,7 +266,7 @@
if err != nil {
return nil, err
}
commit, err := repo.getCommit(objectID)
commit, err := repo.GetCommitByObjectID(objectID)

Check failure on line 269 in modules/git/repo_commit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

repo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_commit_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (repo *Repository) IsCommitExist(name string) bool {
return err == nil
}

func (repo *Repository) getCommit(id ObjectID) (*Commit, error) {
func (repo *Repository) GetCommitByObjectID(id ObjectID) (*Commit, error) {
wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
defer cancel()

Expand Down
6 changes: 0 additions & 6 deletions modules/git/repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package git

import (
"context"
"fmt"
"io"
"strings"
Expand All @@ -17,11 +16,6 @@ import (
// TagPrefix tags prefix path on the repository
const TagPrefix = "refs/tags/"

// IsTagExist returns true if given tag exists in the repository.
func IsTagExist(ctx context.Context, repoPath, name string) bool {
return IsReferenceExist(ctx, repoPath, TagPrefix+name)
}

// CreateTag create one tag in the repository
func (repo *Repository) CreateTag(name, revision string) error {
_, _, err := NewCommand(repo.Ctx, "tag").AddDashesAndList(name, revision).RunStdString(&RunOpts{Dir: repo.Path})
Expand Down
3 changes: 2 additions & 1 deletion modules/git/repo_tag_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func (repo *Repository) IsTagExist(name string) bool {
return false
}

return repo.IsReferenceExist(TagPrefix + name)
exist, _ := repo.IsReferenceExist(TagPrefix + name)
return exist
}

// GetTags returns all tags of the repository.
Expand Down
2 changes: 1 addition & 1 deletion modules/git/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

// Commit return the commit of the tag reference
func (tag *Tag) Commit(gitRepo *Repository) (*Commit, error) {
return gitRepo.getCommit(tag.Object)
return gitRepo.GetCommitByObjectID(tag.Object)

Check failure on line 26 in modules/git/tag.go

View workflow job for this annotation

GitHub Actions / test-sqlite

gitRepo.GetCommitByObjectID undefined (type *Repository has no field or method GetCommitByObjectID)
}

func parsePayloadSignature(data []byte, messageStart int) (payload, msg, sign string) {
Expand Down
34 changes: 28 additions & 6 deletions modules/gitrepo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,44 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
}
defer gitRepo.Close()

return gitRepo.GetBranchCommitID(branch)
return gitRepo.GetRefCommitID(git.BranchPrefix + branch)
}

// IsReferenceExist returns true if given reference exists in the repository.
func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
cmd := git.NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name)
_, _, err := RunGitCmdStdString(repo, cmd, &RunOpts{})
return err == nil
}

func IsWikiReferenceExist(ctx context.Context, repo Repository, name string) bool {
cmd := git.NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name)
_, _, err := RunGitCmdStdString(repo, cmd, &RunOpts{IsWiki: true})
return err == nil
}

// IsBranchExist returns true if given branch exists in the repository.
func IsBranchExist(ctx context.Context, repo Repository, name string) bool {
return IsReferenceExist(ctx, repo, git.BranchPrefix+name)
}

func IsWikiBranchExist(ctx context.Context, repo Repository, name string) bool {
return IsWikiReferenceExist(ctx, repo, git.BranchPrefix+name)
}

// SetDefaultBranch sets default branch of repository.
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
_, _, err := git.NewCommand(ctx, "symbolic-ref", "HEAD").
AddDynamicArguments(git.BranchPrefix + name).
RunStdString(&git.RunOpts{Dir: repoPath(repo)})
cmd := git.NewCommand(ctx, "symbolic-ref", "HEAD").
AddDynamicArguments(git.BranchPrefix + name)
_, _, err := RunGitCmdStdString(repo, cmd, &RunOpts{})
return err
}

// GetDefaultBranch gets default branch of repository.
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
return git.GetDefaultBranch(ctx, repoPath(repo))
return curService.GetDefaultBranch(ctx, repoRelativePath(repo))
}

func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
return git.GetDefaultBranch(ctx, wikiPath(repo))
return curService.GetDefaultBranch(ctx, wikiRelativePath(repo))
}
Loading
Loading