Skip to content

Commit 1ef8777

Browse files
authored
Refactor modules/git global variables (#29376)
Move some global variables into a struct to improve maintainability
1 parent 4e3d81e commit 1ef8777

File tree

8 files changed

+27
-25
lines changed

8 files changed

+27
-25
lines changed

modules/git/git.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ var (
3333
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
3434
DefaultContext context.Context
3535

36-
SupportProcReceive bool // >= 2.29
37-
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
36+
DefaultFeatures struct {
37+
GitVersion *version.Version
3838

39-
gitVersion *version.Version
39+
SupportProcReceive bool // >= 2.29
40+
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
41+
}
4042
)
4143

4244
// loadGitVersion tries to get the current git version and stores it into a global variable
4345
func loadGitVersion() error {
4446
// doesn't need RWMutex because it's executed by Init()
45-
if gitVersion != nil {
47+
if DefaultFeatures.GitVersion != nil {
4648
return nil
4749
}
4850

@@ -53,7 +55,7 @@ func loadGitVersion() error {
5355

5456
ver, err := parseGitVersionLine(strings.TrimSpace(stdout))
5557
if err == nil {
56-
gitVersion = ver
58+
DefaultFeatures.GitVersion = ver
5759
}
5860
return err
5961
}
@@ -93,7 +95,7 @@ func SetExecutablePath(path string) error {
9395
return err
9496
}
9597

96-
if gitVersion.LessThan(versionRequired) {
98+
if DefaultFeatures.GitVersion.LessThan(versionRequired) {
9799
moreHint := "get git: https://git-scm.com/download/"
98100
if runtime.GOOS == "linux" {
99101
// 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 {
102104
moreHint = "get git: https://git-scm.com/download/linux and https://ius.io"
103105
}
104106
}
105-
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", gitVersion.Original(), RequiredVersion, moreHint)
107+
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", DefaultFeatures.GitVersion.Original(), RequiredVersion, moreHint)
106108
}
107109

108-
if err = checkGitVersionCompatibility(gitVersion); err != nil {
109-
return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", gitVersion.String(), err)
110+
if err = checkGitVersionCompatibility(DefaultFeatures.GitVersion); err != nil {
111+
return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", DefaultFeatures.GitVersion.String(), err)
110112
}
111113
return nil
112114
}
113115

114116
// VersionInfo returns git version information
115117
func VersionInfo() string {
116-
if gitVersion == nil {
118+
if DefaultFeatures.GitVersion == nil {
117119
return "(git not found)"
118120
}
119121
format := "%s"
120-
args := []any{gitVersion.Original()}
122+
args := []any{DefaultFeatures.GitVersion.Original()}
121123
// Since git wire protocol has been released from git v2.18
122124
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
123125
format += ", Wire Protocol %s Enabled"
@@ -187,9 +189,9 @@ func InitFull(ctx context.Context) (err error) {
187189
if CheckGitVersionAtLeast("2.9") == nil {
188190
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
189191
}
190-
SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
191-
SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil && !isGogit
192-
if SupportHashSha256 {
192+
DefaultFeatures.SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
193+
DefaultFeatures.SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil && !isGogit
194+
if DefaultFeatures.SupportHashSha256 {
193195
SupportedObjectFormats = append(SupportedObjectFormats, Sha256ObjectFormat)
194196
} else {
195197
log.Warn("sha256 hash support is disabled - requires Git >= 2.42. Gogit is currently unsupported")
@@ -254,7 +256,7 @@ func syncGitConfig() (err error) {
254256
}
255257
}
256258

257-
if SupportProcReceive {
259+
if DefaultFeatures.SupportProcReceive {
258260
// set support for AGit flow
259261
if err := configAddNonExist("receive.procReceiveRefs", "refs/for"); err != nil {
260262
return err
@@ -309,15 +311,15 @@ func syncGitConfig() (err error) {
309311

310312
// CheckGitVersionAtLeast check git version is at least the constraint version
311313
func CheckGitVersionAtLeast(atLeast string) error {
312-
if gitVersion == nil {
314+
if DefaultFeatures.GitVersion == nil {
313315
panic("git module is not initialized") // it shouldn't happen
314316
}
315317
atLeastVersion, err := version.NewVersion(atLeast)
316318
if err != nil {
317319
return err
318320
}
319-
if gitVersion.Compare(atLeastVersion) < 0 {
320-
return fmt.Errorf("installed git binary version %s is not at least %s", gitVersion.Original(), atLeast)
321+
if DefaultFeatures.GitVersion.Compare(atLeastVersion) < 0 {
322+
return fmt.Errorf("installed git binary version %s is not at least %s", DefaultFeatures.GitVersion.Original(), atLeast)
321323
}
322324
return nil
323325
}

modules/git/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
101101
if !IsValidObjectFormat(objectFormatName) {
102102
return fmt.Errorf("invalid object format: %s", objectFormatName)
103103
}
104-
if SupportHashSha256 {
104+
if DefaultFeatures.SupportHashSha256 {
105105
cmd.AddOptionValues("--object-format", objectFormatName)
106106
}
107107

routers/private/hook_post_receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
124124

125125
// post update for agit pull request
126126
// FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR
127-
if git.SupportProcReceive && refFullName.IsPull() {
127+
if git.DefaultFeatures.SupportProcReceive && refFullName.IsPull() {
128128
if repo == nil {
129129
repo = loadRepository(ctx, ownerName, repoName)
130130
if ctx.Written() {

routers/private/hook_pre_receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
122122
preReceiveBranch(ourCtx, oldCommitID, newCommitID, refFullName)
123123
case refFullName.IsTag():
124124
preReceiveTag(ourCtx, oldCommitID, newCommitID, refFullName)
125-
case git.SupportProcReceive && refFullName.IsFor():
125+
case git.DefaultFeatures.SupportProcReceive && refFullName.IsFor():
126126
preReceiveFor(ourCtx, oldCommitID, newCommitID, refFullName)
127127
default:
128128
ourCtx.AssertCanWriteCode()

routers/private/hook_proc_receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// HookProcReceive proc-receive hook - only handles agit Proc-Receive requests at present
1919
func HookProcReceive(ctx *gitea_context.PrivateContext) {
2020
opts := web.GetForm(ctx).(*private.HookOptions)
21-
if !git.SupportProcReceive {
21+
if !git.DefaultFeatures.SupportProcReceive {
2222
ctx.Status(http.StatusNotFound)
2323
return
2424
}

routers/private/serv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func ServCommand(ctx *context.PrivateContext) {
297297
}
298298
} else {
299299
// Because of the special ref "refs/for" we will need to delay write permission check
300-
if git.SupportProcReceive && unitType == unit.TypeCode {
300+
if git.DefaultFeatures.SupportProcReceive && unitType == unit.TypeCode {
301301
mode = perm.AccessModeRead
302302
}
303303

routers/web/misc/misc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func SSHInfo(rw http.ResponseWriter, req *http.Request) {
18-
if !git.SupportProcReceive {
18+
if !git.DefaultFeatures.SupportProcReceive {
1919
rw.WriteHeader(http.StatusNotFound)
2020
return
2121
}

routers/web/repo/githttp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func httpBase(ctx *context.Context) *serviceHandler {
183183

184184
if repoExist {
185185
// Because of special ref "refs/for" .. , need delay write permission check
186-
if git.SupportProcReceive {
186+
if git.DefaultFeatures.SupportProcReceive {
187187
accessMode = perm.AccessModeRead
188188
}
189189

0 commit comments

Comments
 (0)