mirror of
https://github.com/go-gitea/gitea.git
synced 2025-12-15 18:52:56 +00:00
Move some functions to gitrepo package (#35503)
This commit is contained in:
18
modules/gitrepo/blame.go
Normal file
18
modules/gitrepo/blame.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gitrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
func LineBlame(ctx context.Context, repo Repository, revision, file string, line uint) (string, error) {
|
||||
return runCmdString(ctx, repo,
|
||||
gitcmd.NewCommand("blame").
|
||||
AddOptionFormat("-L %d,%d", line, line).
|
||||
AddOptionValues("-p", revision).
|
||||
AddDashesAndList(file))
|
||||
}
|
||||
@@ -5,6 +5,8 @@ package gitrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
@@ -34,23 +36,61 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
|
||||
|
||||
// SetDefaultBranch sets default branch of repository.
|
||||
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
|
||||
_, _, err := gitcmd.NewCommand("symbolic-ref", "HEAD").
|
||||
AddDynamicArguments(git.BranchPrefix+name).
|
||||
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
_, err := runCmdString(ctx, repo, gitcmd.NewCommand("symbolic-ref", "HEAD").
|
||||
AddDynamicArguments(git.BranchPrefix+name))
|
||||
return err
|
||||
}
|
||||
|
||||
// GetDefaultBranch gets default branch of repository.
|
||||
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
|
||||
return git.GetDefaultBranch(ctx, repoPath(repo))
|
||||
stdout, err := runCmdString(ctx, repo, gitcmd.NewCommand("symbolic-ref", "HEAD"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
stdout = strings.TrimSpace(stdout)
|
||||
if !strings.HasPrefix(stdout, git.BranchPrefix) {
|
||||
return "", errors.New("the HEAD is not a branch: " + stdout)
|
||||
}
|
||||
return strings.TrimPrefix(stdout, git.BranchPrefix), nil
|
||||
}
|
||||
|
||||
// IsReferenceExist returns true if given reference exists in the repository.
|
||||
func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
|
||||
return git.IsReferenceExist(ctx, repoPath(repo), name)
|
||||
_, err := runCmdString(ctx, repo, gitcmd.NewCommand("show-ref", "--verify").AddDashesAndList(name))
|
||||
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)
|
||||
}
|
||||
|
||||
// DeleteBranch delete a branch by name on repository.
|
||||
func DeleteBranch(ctx context.Context, repo Repository, name string, force bool) error {
|
||||
cmd := gitcmd.NewCommand("branch")
|
||||
|
||||
if force {
|
||||
cmd.AddArguments("-D")
|
||||
} else {
|
||||
cmd.AddArguments("-d")
|
||||
}
|
||||
|
||||
cmd.AddDashesAndList(name)
|
||||
_, err := runCmdString(ctx, repo, cmd)
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateBranch create a new branch
|
||||
func CreateBranch(ctx context.Context, repo Repository, branch, oldbranchOrCommit string) error {
|
||||
cmd := gitcmd.NewCommand("branch")
|
||||
cmd.AddDashesAndList(branch, oldbranchOrCommit)
|
||||
|
||||
_, err := runCmdString(ctx, repo, cmd)
|
||||
return err
|
||||
}
|
||||
|
||||
// RenameBranch rename a branch
|
||||
func RenameBranch(ctx context.Context, repo Repository, from, to string) error {
|
||||
_, err := runCmdString(ctx, repo, gitcmd.NewCommand("branch", "-m").AddDynamicArguments(from, to))
|
||||
return err
|
||||
}
|
||||
|
||||
15
modules/gitrepo/command.go
Normal file
15
modules/gitrepo/command.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gitrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
func runCmdString(ctx context.Context, repo Repository, cmd *gitcmd.Command) (string, error) {
|
||||
res, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
return res, err
|
||||
}
|
||||
@@ -12,9 +12,8 @@ import (
|
||||
)
|
||||
|
||||
func GitConfigGet(ctx context.Context, repo Repository, key string) (string, error) {
|
||||
result, _, err := gitcmd.NewCommand("config", "--get").
|
||||
AddDynamicArguments(key).
|
||||
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
result, err := runCmdString(ctx, repo, gitcmd.NewCommand("config", "--get").
|
||||
AddDynamicArguments(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -28,9 +27,8 @@ func getRepoConfigLockKey(repoStoragePath string) string {
|
||||
// GitConfigAdd add a git configuration key to a specific value for the given repository.
|
||||
func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error {
|
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error {
|
||||
_, _, err := gitcmd.NewCommand("config", "--add").
|
||||
AddDynamicArguments(key, value).
|
||||
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
_, err := runCmdString(ctx, repo, gitcmd.NewCommand("config", "--add").
|
||||
AddDynamicArguments(key, value))
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -40,9 +38,8 @@ func GitConfigAdd(ctx context.Context, repo Repository, key, value string) error
|
||||
// If the key exists, it will be updated to the new value.
|
||||
func GitConfigSet(ctx context.Context, repo Repository, key, value string) error {
|
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error {
|
||||
_, _, err := gitcmd.NewCommand("config").
|
||||
AddDynamicArguments(key, value).
|
||||
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
_, err := runCmdString(ctx, repo, gitcmd.NewCommand("config").
|
||||
AddDynamicArguments(key, value))
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
62
modules/gitrepo/diff.go
Normal file
62
modules/gitrepo/diff.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gitrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
// GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions
|
||||
// TODO: it can be merged with another "GetDiffShortStat" in the future
|
||||
func GetDiffShortStatByCmdArgs(ctx context.Context, repo Repository, trustedArgs gitcmd.TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
|
||||
// Now if we call:
|
||||
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
|
||||
// we get:
|
||||
// " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n"
|
||||
cmd := gitcmd.NewCommand("diff", "--shortstat").AddArguments(trustedArgs...).AddDynamicArguments(dynamicArgs...)
|
||||
stdout, err := runCmdString(ctx, repo, cmd)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
|
||||
return parseDiffStat(stdout)
|
||||
}
|
||||
|
||||
var shortStatFormat = regexp.MustCompile(
|
||||
`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`)
|
||||
|
||||
func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) {
|
||||
if len(stdout) == 0 || stdout == "\n" {
|
||||
return 0, 0, 0, nil
|
||||
}
|
||||
groups := shortStatFormat.FindStringSubmatch(stdout)
|
||||
if len(groups) != 4 {
|
||||
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups)
|
||||
}
|
||||
|
||||
numFiles, err = strconv.Atoi(groups[1])
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %w", stdout, err)
|
||||
}
|
||||
|
||||
if len(groups[2]) != 0 {
|
||||
totalAdditions, err = strconv.Atoi(groups[2])
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %w", stdout, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(groups[3]) != 0 {
|
||||
totalDeletions, err = strconv.Atoi(groups[3])
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %w", stdout, err)
|
||||
}
|
||||
}
|
||||
return numFiles, totalAdditions, totalDeletions, err
|
||||
}
|
||||
@@ -36,9 +36,7 @@ func GitRemoteAdd(ctx context.Context, repo Repository, remoteName, remoteURL st
|
||||
return errors.New("unknown remote option: " + string(options[0]))
|
||||
}
|
||||
}
|
||||
_, _, err := cmd.
|
||||
AddDynamicArguments(remoteName, remoteURL).
|
||||
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
_, err := runCmdString(ctx, repo, cmd.AddDynamicArguments(remoteName, remoteURL))
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -46,7 +44,7 @@ func GitRemoteAdd(ctx context.Context, repo Repository, remoteName, remoteURL st
|
||||
func GitRemoteRemove(ctx context.Context, repo Repository, remoteName string) error {
|
||||
return globallock.LockAndDo(ctx, getRepoConfigLockKey(repo.RelativePath()), func(ctx context.Context) error {
|
||||
cmd := gitcmd.NewCommand("remote", "rm").AddDynamicArguments(remoteName)
|
||||
_, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
|
||||
_, err := runCmdString(ctx, repo, cmd)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user