Skip to content

Commit d5563be

Browse files
authored
Make sure git version&feature are always prepared (#30877) (#30879)
Backport #30877
1 parent ad5a8d0 commit d5563be

28 files changed

+114
-144
lines changed

cmd/hook.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ Gitea or set your environment appropriately.`, "")
220220
}
221221
}
222222

223-
supportProcReceive := false
224-
if git.CheckGitVersionAtLeast("2.29") == nil {
225-
supportProcReceive = true
226-
}
223+
supportProcReceive := git.DefaultFeatures().SupportProcReceive
227224

228225
for scanner.Scan() {
229226
// TODO: support news feeds for wiki
@@ -497,7 +494,7 @@ Gitea or set your environment appropriately.`, "")
497494
return nil
498495
}
499496

500-
if git.CheckGitVersionAtLeast("2.29") != nil {
497+
if !git.DefaultFeatures().SupportProcReceive {
501498
return fail(ctx, "No proc-receive support", "current git version doesn't support proc-receive.")
502499
}
503500

cmd/serv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func runServ(c *cli.Context) error {
178178
}
179179

180180
if len(words) < 2 {
181-
if git.CheckGitVersionAtLeast("2.29") == nil {
181+
if git.DefaultFeatures().SupportProcReceive {
182182
// for AGit Flow
183183
if cmd == "ssh_info" {
184184
fmt.Print(`{"type":"gitea","version":1}`)

modules/git/blame.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (r *BlameReader) Close() error {
132132
// CreateBlameReader creates reader for given repository, commit and file
133133
func CreateBlameReader(ctx context.Context, objectFormat ObjectFormat, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) {
134134
var ignoreRevsFile *string
135-
if CheckGitVersionAtLeast("2.23") == nil && !bypassBlameIgnore {
135+
if DefaultFeatures().CheckVersionAtLeast("2.23") && !bypassBlameIgnore {
136136
ignoreRevsFile = tryCreateBlameIgnoreRevsFile(commit)
137137
}
138138

modules/git/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
423423
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
424424
func (c *Commit) GetBranchName() (string, error) {
425425
cmd := NewCommand(c.repo.Ctx, "name-rev")
426-
if CheckGitVersionAtLeast("2.13.0") == nil {
426+
if DefaultFeatures().CheckVersionAtLeast("2.13.0") {
427427
cmd.AddArguments("--exclude", "refs/tags/*")
428428
}
429429
cmd.AddArguments("--name-only", "--no-undefined").AddDynamicArguments(c.ID.String())

modules/git/git.go

+83-100
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,63 @@ import (
2222
"github.com/hashicorp/go-version"
2323
)
2424

25-
// RequiredVersion is the minimum Git version required
26-
const RequiredVersion = "2.0.0"
25+
const RequiredVersion = "2.0.0" // the minimum Git version required
2726

28-
var (
29-
// GitExecutable is the command name of git
30-
// Could be updated to an absolute path while initialization
31-
GitExecutable = "git"
32-
33-
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
34-
DefaultContext context.Context
27+
type Features struct {
28+
gitVersion *version.Version
3529

36-
DefaultFeatures struct {
37-
GitVersion *version.Version
30+
UsingGogit bool
31+
SupportProcReceive bool // >= 2.29
32+
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
33+
SupportedObjectFormats []ObjectFormat // sha1, sha256
34+
}
3835

39-
SupportProcReceive bool // >= 2.29
40-
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
41-
}
36+
var (
37+
GitExecutable = "git" // the command name of git, will be updated to an absolute path during initialization
38+
DefaultContext context.Context // the default context to run git commands in, must be initialized by git.InitXxx
39+
defaultFeatures *Features
4240
)
4341

44-
// loadGitVersion tries to get the current git version and stores it into a global variable
45-
func loadGitVersion() error {
46-
// doesn't need RWMutex because it's executed by Init()
47-
if DefaultFeatures.GitVersion != nil {
48-
return nil
42+
func (f *Features) CheckVersionAtLeast(atLeast string) bool {
43+
return f.gitVersion.Compare(version.Must(version.NewVersion(atLeast))) >= 0
44+
}
45+
46+
// VersionInfo returns git version information
47+
func (f *Features) VersionInfo() string {
48+
return f.gitVersion.Original()
49+
}
50+
51+
func DefaultFeatures() *Features {
52+
if defaultFeatures == nil {
53+
if !setting.IsProd || setting.IsInTesting {
54+
log.Warn("git.DefaultFeatures is called before git.InitXxx, initializing with default values")
55+
}
56+
if err := InitSimple(context.Background()); err != nil {
57+
log.Fatal("git.InitSimple failed: %v", err)
58+
}
4959
}
60+
return defaultFeatures
61+
}
5062

63+
func loadGitVersionFeatures() (*Features, error) {
5164
stdout, _, runErr := NewCommand(DefaultContext, "version").RunStdString(nil)
5265
if runErr != nil {
53-
return runErr
66+
return nil, runErr
5467
}
5568

5669
ver, err := parseGitVersionLine(strings.TrimSpace(stdout))
57-
if err == nil {
58-
DefaultFeatures.GitVersion = ver
70+
if err != nil {
71+
return nil, err
5972
}
60-
return err
73+
74+
features := &Features{gitVersion: ver, UsingGogit: isGogit}
75+
features.SupportProcReceive = features.CheckVersionAtLeast("2.29")
76+
features.SupportHashSha256 = features.CheckVersionAtLeast("2.42") && !isGogit
77+
features.SupportedObjectFormats = []ObjectFormat{Sha1ObjectFormat}
78+
if features.SupportHashSha256 {
79+
features.SupportedObjectFormats = append(features.SupportedObjectFormats, Sha256ObjectFormat)
80+
}
81+
return features, nil
6182
}
6283

6384
func parseGitVersionLine(s string) (*version.Version, error) {
@@ -85,56 +106,24 @@ func SetExecutablePath(path string) error {
85106
return fmt.Errorf("git not found: %w", err)
86107
}
87108
GitExecutable = absPath
109+
return nil
110+
}
88111

89-
if err = loadGitVersion(); err != nil {
90-
return fmt.Errorf("unable to load git version: %w", err)
91-
}
92-
93-
versionRequired, err := version.NewVersion(RequiredVersion)
94-
if err != nil {
95-
return err
96-
}
97-
98-
if DefaultFeatures.GitVersion.LessThan(versionRequired) {
112+
func ensureGitVersion() error {
113+
if !DefaultFeatures().CheckVersionAtLeast(RequiredVersion) {
99114
moreHint := "get git: https://git-scm.com/download/"
100115
if runtime.GOOS == "linux" {
101116
// there are a lot of CentOS/RHEL users using old git, so we add a special hint for them
102-
if _, err = os.Stat("/etc/redhat-release"); err == nil {
117+
if _, err := os.Stat("/etc/redhat-release"); err == nil {
103118
// ius.io is the recommended official(git-scm.com) method to install git
104119
moreHint = "get git: https://git-scm.com/download/linux and https://ius.io"
105120
}
106121
}
107-
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", DefaultFeatures.GitVersion.Original(), RequiredVersion, moreHint)
122+
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", DefaultFeatures().gitVersion.Original(), RequiredVersion, moreHint)
108123
}
109124

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)
112-
}
113-
return nil
114-
}
115-
116-
// VersionInfo returns git version information
117-
func VersionInfo() string {
118-
if DefaultFeatures.GitVersion == nil {
119-
return "(git not found)"
120-
}
121-
format := "%s"
122-
args := []any{DefaultFeatures.GitVersion.Original()}
123-
// Since git wire protocol has been released from git v2.18
124-
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
125-
format += ", Wire Protocol %s Enabled"
126-
args = append(args, "Version 2") // for focus color
127-
}
128-
129-
return fmt.Sprintf(format, args...)
130-
}
131-
132-
func checkInit() error {
133-
if setting.Git.HomePath == "" {
134-
return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
135-
}
136-
if DefaultContext != nil {
137-
log.Warn("git module has been initialized already, duplicate init may work but it's better to fix it")
125+
if err := checkGitVersionCompatibility(DefaultFeatures().gitVersion); err != nil {
126+
return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", DefaultFeatures().gitVersion.String(), err)
138127
}
139128
return nil
140129
}
@@ -154,8 +143,12 @@ func HomeDir() string {
154143
// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
155144
// This method doesn't change anything to filesystem. At the moment, it is only used by some Gitea sub-commands.
156145
func InitSimple(ctx context.Context) error {
157-
if err := checkInit(); err != nil {
158-
return err
146+
if setting.Git.HomePath == "" {
147+
return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
148+
}
149+
150+
if DefaultContext != nil && (!setting.IsProd || setting.IsInTesting) {
151+
log.Warn("git module has been initialized already, duplicate init may work but it's better to fix it")
159152
}
160153

161154
DefaultContext = ctx
@@ -165,40 +158,45 @@ func InitSimple(ctx context.Context) error {
165158
defaultCommandExecutionTimeout = time.Duration(setting.Git.Timeout.Default) * time.Second
166159
}
167160

168-
return SetExecutablePath(setting.Git.Path)
169-
}
161+
if err := SetExecutablePath(setting.Git.Path); err != nil {
162+
return err
163+
}
170164

171-
// InitFull initializes git module with version check and change global variables, sync gitconfig.
172-
// It should only be called once at the beginning of the program initialization (TestMain/GlobalInitInstalled) as this code makes unsynchronized changes to variables.
173-
func InitFull(ctx context.Context) (err error) {
174-
if err = InitSimple(ctx); err != nil {
165+
var err error
166+
defaultFeatures, err = loadGitVersionFeatures()
167+
if err != nil {
168+
return err
169+
}
170+
if err = ensureGitVersion(); err != nil {
175171
return err
176172
}
177173

178174
// when git works with gnupg (commit signing), there should be a stable home for gnupg commands
179175
if _, ok := os.LookupEnv("GNUPGHOME"); !ok {
180176
_ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg"))
181177
}
178+
return nil
179+
}
180+
181+
// InitFull initializes git module with version check and change global variables, sync gitconfig.
182+
// It should only be called once at the beginning of the program initialization (TestMain/GlobalInitInstalled) as this code makes unsynchronized changes to variables.
183+
func InitFull(ctx context.Context) (err error) {
184+
if err = InitSimple(ctx); err != nil {
185+
return err
186+
}
182187

183188
// Since git wire protocol has been released from git v2.18
184-
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
189+
if setting.Git.EnableAutoGitWireProtocol && DefaultFeatures().CheckVersionAtLeast("2.18") {
185190
globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2")
186191
}
187192

188193
// Explicitly disable credential helper, otherwise Git credentials might leak
189-
if CheckGitVersionAtLeast("2.9") == nil {
194+
if DefaultFeatures().CheckVersionAtLeast("2.9") {
190195
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
191196
}
192-
DefaultFeatures.SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
193-
DefaultFeatures.SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil && !isGogit
194-
if DefaultFeatures.SupportHashSha256 {
195-
SupportedObjectFormats = append(SupportedObjectFormats, Sha256ObjectFormat)
196-
} else {
197-
log.Warn("sha256 hash support is disabled - requires Git >= 2.42. Gogit is currently unsupported")
198-
}
199197

200198
if setting.LFS.StartServer {
201-
if CheckGitVersionAtLeast("2.1.2") != nil {
199+
if !DefaultFeatures().CheckVersionAtLeast("2.1.2") {
202200
return errors.New("LFS server support requires Git >= 2.1.2")
203201
}
204202
globalCommandArgs = append(globalCommandArgs, "-c", "filter.lfs.required=", "-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
@@ -238,13 +236,13 @@ func syncGitConfig() (err error) {
238236
return err
239237
}
240238

241-
if CheckGitVersionAtLeast("2.10") == nil {
239+
if DefaultFeatures().CheckVersionAtLeast("2.10") {
242240
if err := configSet("receive.advertisePushOptions", "true"); err != nil {
243241
return err
244242
}
245243
}
246244

247-
if CheckGitVersionAtLeast("2.18") == nil {
245+
if DefaultFeatures().CheckVersionAtLeast("2.18") {
248246
if err := configSet("core.commitGraph", "true"); err != nil {
249247
return err
250248
}
@@ -256,7 +254,7 @@ func syncGitConfig() (err error) {
256254
}
257255
}
258256

259-
if DefaultFeatures.SupportProcReceive {
257+
if DefaultFeatures().SupportProcReceive {
260258
// set support for AGit flow
261259
if err := configAddNonExist("receive.procReceiveRefs", "refs/for"); err != nil {
262260
return err
@@ -294,7 +292,7 @@ func syncGitConfig() (err error) {
294292
}
295293

296294
// By default partial clones are disabled, enable them from git v2.22
297-
if !setting.Git.DisablePartialClone && CheckGitVersionAtLeast("2.22") == nil {
295+
if !setting.Git.DisablePartialClone && DefaultFeatures().CheckVersionAtLeast("2.22") {
298296
if err = configSet("uploadpack.allowfilter", "true"); err != nil {
299297
return err
300298
}
@@ -309,21 +307,6 @@ func syncGitConfig() (err error) {
309307
return err
310308
}
311309

312-
// CheckGitVersionAtLeast check git version is at least the constraint version
313-
func CheckGitVersionAtLeast(atLeast string) error {
314-
if DefaultFeatures.GitVersion == nil {
315-
panic("git module is not initialized") // it shouldn't happen
316-
}
317-
atLeastVersion, err := version.NewVersion(atLeast)
318-
if err != nil {
319-
return err
320-
}
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)
323-
}
324-
return nil
325-
}
326-
327310
func checkGitVersionCompatibility(gitVer *version.Version) error {
328311
badVersions := []struct {
329312
Version *version.Version

modules/git/object_format.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ var (
120120
Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{}
121121
)
122122

123-
var SupportedObjectFormats = []ObjectFormat{
124-
Sha1ObjectFormat,
125-
}
126-
127123
func ObjectFormatFromName(name string) ObjectFormat {
128-
for _, objectFormat := range SupportedObjectFormats {
124+
for _, objectFormat := range DefaultFeatures().SupportedObjectFormats {
129125
if name == objectFormat.Name() {
130126
return objectFormat
131127
}

modules/git/object_id.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (*Sha256Hash) Type() ObjectFormat { return Sha256ObjectFormat }
5454

5555
func NewIDFromString(hexHash string) (ObjectID, error) {
5656
var theObjectFormat ObjectFormat
57-
for _, objectFormat := range SupportedObjectFormats {
57+
for _, objectFormat := range DefaultFeatures().SupportedObjectFormats {
5858
if len(hexHash) == objectFormat.FullLength() {
5959
theObjectFormat = objectFormat
6060
break

modules/git/remote.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// GetRemoteAddress returns remote url of git repository in the repoPath with special remote name
1313
func GetRemoteAddress(ctx context.Context, repoPath, remoteName string) (string, error) {
1414
var cmd *Command
15-
if CheckGitVersionAtLeast("2.7") == nil {
15+
if DefaultFeatures().CheckVersionAtLeast("2.7") {
1616
cmd = NewCommand(ctx, "remote", "get-url").AddDynamicArguments(remoteName)
1717
} else {
1818
cmd = NewCommand(ctx, "config", "--get").AddDynamicArguments("remote." + remoteName + ".url")

modules/git/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
7474
if !IsValidObjectFormat(objectFormatName) {
7575
return fmt.Errorf("invalid object format: %s", objectFormatName)
7676
}
77-
if DefaultFeatures.SupportHashSha256 {
77+
if DefaultFeatures().SupportHashSha256 {
7878
cmd.AddOptionValues("--object-format", objectFormatName)
7979
}
8080

modules/git/repo_base.go

-6
This file was deleted.

modules/git/repo_base_gogit.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222
"github.com/go-git/go-git/v5/storage/filesystem"
2323
)
2424

25-
func init() {
26-
isGogit = true
27-
}
25+
const isGogit = true
2826

2927
// Repository represents a Git repository.
3028
type Repository struct {

0 commit comments

Comments
 (0)