Refactor modules/git global variables (#29376)

Move some global variables into a struct to improve maintainability
pull/29220/head^2
wxiaoguang 9 months ago committed by GitHub
parent 4e3d81e44e
commit 1ef87773b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 38
      modules/git/git.go
  2. 2
      modules/git/repo.go
  3. 2
      routers/private/hook_post_receive.go
  4. 2
      routers/private/hook_pre_receive.go
  5. 2
      routers/private/hook_proc_receive.go
  6. 2
      routers/private/serv.go
  7. 2
      routers/web/misc/misc.go
  8. 2
      routers/web/repo/githttp.go

@ -33,16 +33,18 @@ var (
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx // DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
DefaultContext context.Context DefaultContext context.Context
SupportProcReceive bool // >= 2.29 DefaultFeatures struct {
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’ GitVersion *version.Version
gitVersion *version.Version SupportProcReceive bool // >= 2.29
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
}
) )
// loadGitVersion tries to get the current git version and stores it into a global variable // loadGitVersion tries to get the current git version and stores it into a global variable
func loadGitVersion() error { func loadGitVersion() error {
// doesn't need RWMutex because it's executed by Init() // doesn't need RWMutex because it's executed by Init()
if gitVersion != nil { if DefaultFeatures.GitVersion != nil {
return nil return nil
} }
@ -53,7 +55,7 @@ func loadGitVersion() error {
ver, err := parseGitVersionLine(strings.TrimSpace(stdout)) ver, err := parseGitVersionLine(strings.TrimSpace(stdout))
if err == nil { if err == nil {
gitVersion = ver DefaultFeatures.GitVersion = ver
} }
return err return err
} }
@ -93,7 +95,7 @@ func SetExecutablePath(path string) error {
return err return err
} }
if gitVersion.LessThan(versionRequired) { if DefaultFeatures.GitVersion.LessThan(versionRequired) {
moreHint := "get git: https://git-scm.com/download/" moreHint := "get git: https://git-scm.com/download/"
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
// there are a lot of CentOS/RHEL users using old git, so we add a special hint for them // there are a lot of CentOS/RHEL users using old git, so we add a special hint for them
@ -102,22 +104,22 @@ func SetExecutablePath(path string) error {
moreHint = "get git: https://git-scm.com/download/linux and https://ius.io" moreHint = "get git: https://git-scm.com/download/linux and https://ius.io"
} }
} }
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", gitVersion.Original(), RequiredVersion, moreHint) return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", DefaultFeatures.GitVersion.Original(), RequiredVersion, moreHint)
} }
if err = checkGitVersionCompatibility(gitVersion); err != nil { if err = checkGitVersionCompatibility(DefaultFeatures.GitVersion); err != nil {
return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", gitVersion.String(), err) return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", DefaultFeatures.GitVersion.String(), err)
} }
return nil return nil
} }
// VersionInfo returns git version information // VersionInfo returns git version information
func VersionInfo() string { func VersionInfo() string {
if gitVersion == nil { if DefaultFeatures.GitVersion == nil {
return "(git not found)" return "(git not found)"
} }
format := "%s" format := "%s"
args := []any{gitVersion.Original()} args := []any{DefaultFeatures.GitVersion.Original()}
// Since git wire protocol has been released from git v2.18 // Since git wire protocol has been released from git v2.18
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil { if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
format += ", Wire Protocol %s Enabled" format += ", Wire Protocol %s Enabled"
@ -187,9 +189,9 @@ func InitFull(ctx context.Context) (err error) {
if CheckGitVersionAtLeast("2.9") == nil { if CheckGitVersionAtLeast("2.9") == nil {
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=") globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
} }
SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil DefaultFeatures.SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil && !isGogit DefaultFeatures.SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil && !isGogit
if SupportHashSha256 { if DefaultFeatures.SupportHashSha256 {
SupportedObjectFormats = append(SupportedObjectFormats, Sha256ObjectFormat) SupportedObjectFormats = append(SupportedObjectFormats, Sha256ObjectFormat)
} else { } else {
log.Warn("sha256 hash support is disabled - requires Git >= 2.42. Gogit is currently unsupported") log.Warn("sha256 hash support is disabled - requires Git >= 2.42. Gogit is currently unsupported")
@ -254,7 +256,7 @@ func syncGitConfig() (err error) {
} }
} }
if SupportProcReceive { if DefaultFeatures.SupportProcReceive {
// set support for AGit flow // set support for AGit flow
if err := configAddNonExist("receive.procReceiveRefs", "refs/for"); err != nil { if err := configAddNonExist("receive.procReceiveRefs", "refs/for"); err != nil {
return err return err
@ -309,15 +311,15 @@ func syncGitConfig() (err error) {
// CheckGitVersionAtLeast check git version is at least the constraint version // CheckGitVersionAtLeast check git version is at least the constraint version
func CheckGitVersionAtLeast(atLeast string) error { func CheckGitVersionAtLeast(atLeast string) error {
if gitVersion == nil { if DefaultFeatures.GitVersion == nil {
panic("git module is not initialized") // it shouldn't happen panic("git module is not initialized") // it shouldn't happen
} }
atLeastVersion, err := version.NewVersion(atLeast) atLeastVersion, err := version.NewVersion(atLeast)
if err != nil { if err != nil {
return err return err
} }
if gitVersion.Compare(atLeastVersion) < 0 { if DefaultFeatures.GitVersion.Compare(atLeastVersion) < 0 {
return fmt.Errorf("installed git binary version %s is not at least %s", gitVersion.Original(), atLeast) return fmt.Errorf("installed git binary version %s is not at least %s", DefaultFeatures.GitVersion.Original(), atLeast)
} }
return nil return nil
} }

@ -101,7 +101,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
if !IsValidObjectFormat(objectFormatName) { if !IsValidObjectFormat(objectFormatName) {
return fmt.Errorf("invalid object format: %s", objectFormatName) return fmt.Errorf("invalid object format: %s", objectFormatName)
} }
if SupportHashSha256 { if DefaultFeatures.SupportHashSha256 {
cmd.AddOptionValues("--object-format", objectFormatName) cmd.AddOptionValues("--object-format", objectFormatName)
} }

@ -124,7 +124,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
// post update for agit pull request // post update for agit pull request
// FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR // FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR
if git.SupportProcReceive && refFullName.IsPull() { if git.DefaultFeatures.SupportProcReceive && refFullName.IsPull() {
if repo == nil { if repo == nil {
repo = loadRepository(ctx, ownerName, repoName) repo = loadRepository(ctx, ownerName, repoName)
if ctx.Written() { if ctx.Written() {

@ -122,7 +122,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
preReceiveBranch(ourCtx, oldCommitID, newCommitID, refFullName) preReceiveBranch(ourCtx, oldCommitID, newCommitID, refFullName)
case refFullName.IsTag(): case refFullName.IsTag():
preReceiveTag(ourCtx, oldCommitID, newCommitID, refFullName) preReceiveTag(ourCtx, oldCommitID, newCommitID, refFullName)
case git.SupportProcReceive && refFullName.IsFor(): case git.DefaultFeatures.SupportProcReceive && refFullName.IsFor():
preReceiveFor(ourCtx, oldCommitID, newCommitID, refFullName) preReceiveFor(ourCtx, oldCommitID, newCommitID, refFullName)
default: default:
ourCtx.AssertCanWriteCode() ourCtx.AssertCanWriteCode()

@ -18,7 +18,7 @@ import (
// HookProcReceive proc-receive hook - only handles agit Proc-Receive requests at present // HookProcReceive proc-receive hook - only handles agit Proc-Receive requests at present
func HookProcReceive(ctx *gitea_context.PrivateContext) { func HookProcReceive(ctx *gitea_context.PrivateContext) {
opts := web.GetForm(ctx).(*private.HookOptions) opts := web.GetForm(ctx).(*private.HookOptions)
if !git.SupportProcReceive { if !git.DefaultFeatures.SupportProcReceive {
ctx.Status(http.StatusNotFound) ctx.Status(http.StatusNotFound)
return return
} }

@ -297,7 +297,7 @@ func ServCommand(ctx *context.PrivateContext) {
} }
} else { } else {
// Because of the special ref "refs/for" we will need to delay write permission check // Because of the special ref "refs/for" we will need to delay write permission check
if git.SupportProcReceive && unitType == unit.TypeCode { if git.DefaultFeatures.SupportProcReceive && unitType == unit.TypeCode {
mode = perm.AccessModeRead mode = perm.AccessModeRead
} }

@ -15,7 +15,7 @@ import (
) )
func SSHInfo(rw http.ResponseWriter, req *http.Request) { func SSHInfo(rw http.ResponseWriter, req *http.Request) {
if !git.SupportProcReceive { if !git.DefaultFeatures.SupportProcReceive {
rw.WriteHeader(http.StatusNotFound) rw.WriteHeader(http.StatusNotFound)
return return
} }

@ -183,7 +183,7 @@ func httpBase(ctx *context.Context) *serviceHandler {
if repoExist { if repoExist {
// Because of special ref "refs/for" .. , need delay write permission check // Because of special ref "refs/for" .. , need delay write permission check
if git.SupportProcReceive { if git.DefaultFeatures.SupportProcReceive {
accessMode = perm.AccessModeRead accessMode = perm.AccessModeRead
} }

Loading…
Cancel
Save