Skip to content

Commit 906a722

Browse files
authored
Refactor git version functions and check compatibility (#29155) (#29157)
Backport #29155 with an extra change: tolerate the git 2.43.1 GIT_FLUSH bug in Gitea 1.21.x, more details in the comment of repo_attribute.go Manually tested with git 2.43.1 and an old git (2.39.2)
1 parent 8cd83ff commit 906a722

File tree

3 files changed

+80
-31
lines changed

3 files changed

+80
-31
lines changed

modules/git/git.go

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,37 @@ var (
3939
gitVersion *version.Version
4040
)
4141

42-
// loadGitVersion returns current Git version from shell. Internal usage only.
43-
func loadGitVersion() (*version.Version, error) {
42+
// loadGitVersion tries to get the current git version and stores it into a global variable
43+
func loadGitVersion() error {
4444
// doesn't need RWMutex because it's executed by Init()
4545
if gitVersion != nil {
46-
return gitVersion, nil
46+
return nil
4747
}
4848

4949
stdout, _, runErr := NewCommand(DefaultContext, "version").RunStdString(nil)
5050
if runErr != nil {
51-
return nil, runErr
51+
return runErr
5252
}
5353

54-
fields := strings.Fields(stdout)
55-
if len(fields) < 3 {
56-
return nil, fmt.Errorf("invalid git version output: %s", stdout)
54+
ver, err := parseGitVersionLine(strings.TrimSpace(stdout))
55+
if err == nil {
56+
gitVersion = ver
5757
}
58+
return err
59+
}
5860

59-
var versionString string
60-
61-
// Handle special case on Windows.
62-
i := strings.Index(fields[2], "windows")
63-
if i >= 1 {
64-
versionString = fields[2][:i-1]
65-
} else {
66-
versionString = fields[2]
61+
func parseGitVersionLine(s string) (*version.Version, error) {
62+
fields := strings.Fields(s)
63+
if len(fields) < 3 {
64+
return nil, fmt.Errorf("invalid git version: %q", s)
6765
}
6866

69-
var err error
70-
gitVersion, err = version.NewVersion(versionString)
71-
return gitVersion, err
67+
// version string is like: "git version 2.29.3" or "git version 2.29.3.windows.1"
68+
versionString := fields[2]
69+
if pos := strings.Index(versionString, "windows"); pos >= 1 {
70+
versionString = versionString[:pos-1]
71+
}
72+
return version.NewVersion(versionString)
7273
}
7374

7475
// SetExecutablePath changes the path of git executable and checks the file permission and version.
@@ -83,8 +84,7 @@ func SetExecutablePath(path string) error {
8384
}
8485
GitExecutable = absPath
8586

86-
_, err = loadGitVersion()
87-
if err != nil {
87+
if err = loadGitVersion(); err != nil {
8888
return fmt.Errorf("unable to load git version: %w", err)
8989
}
9090

@@ -105,6 +105,9 @@ func SetExecutablePath(path string) error {
105105
return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", gitVersion.Original(), RequiredVersion, moreHint)
106106
}
107107

108+
if err = checkGitVersionCompatibility(gitVersion); err != nil {
109+
log.Error("installed git version %s has a known compatibility issue with Gitea: %s, please downgrade (or upgrade) your git", gitVersion.String(), err.Error())
110+
}
108111
return nil
109112
}
110113

@@ -256,19 +259,18 @@ func syncGitConfig() (err error) {
256259
}
257260
}
258261

259-
// Due to CVE-2022-24765, git now denies access to git directories which are not owned by current user
260-
// however, some docker users and samba users find it difficult to configure their systems so that Gitea's git repositories are owned by the Gitea user. (Possibly Windows Service users - but ownership in this case should really be set correctly on the filesystem.)
261-
// see issue: https://github.com/go-gitea/gitea/issues/19455
262-
// Fundamentally the problem lies with the uid-gid-mapping mechanism for filesystems in docker on windows (and to a lesser extent samba).
263-
// Docker's configuration mechanism for local filesystems provides no way of setting this mapping and although there is a mechanism for setting this uid through using cifs mounting it is complicated and essentially undocumented
264-
// Thus the owner uid/gid for files on these filesystems will be marked as root.
262+
// Due to CVE-2022-24765, git now denies access to git directories which are not owned by current user.
263+
// However, some docker users and samba users find it difficult to configure their systems correctly,
264+
// so that Gitea's git repositories are owned by the Gitea user.
265+
// (Possibly Windows Service users - but ownership in this case should really be set correctly on the filesystem.)
266+
// See issue: https://github.com/go-gitea/gitea/issues/19455
265267
// As Gitea now always use its internal git config file, and access to the git repositories is managed through Gitea,
266268
// it is now safe to set "safe.directory=*" for internal usage only.
267-
// Please note: the wildcard "*" is only supported by Git 2.30.4/2.31.3/2.32.2/2.33.3/2.34.3/2.35.3/2.36 and later
268-
// Although only supported by Git 2.30.4/2.31.3/2.32.2/2.33.3/2.34.3/2.35.3/2.36 and later - this setting is tolerated by earlier versions
269+
// Although this setting is only supported by some new git versions, it is also tolerated by earlier versions
269270
if err := configAddNonExist("safe.directory", "*"); err != nil {
270271
return err
271272
}
273+
272274
if runtime.GOOS == "windows" {
273275
if err := configSet("core.longpaths", "true"); err != nil {
274276
return err
@@ -301,8 +303,8 @@ func syncGitConfig() (err error) {
301303

302304
// CheckGitVersionAtLeast check git version is at least the constraint version
303305
func CheckGitVersionAtLeast(atLeast string) error {
304-
if _, err := loadGitVersion(); err != nil {
305-
return err
306+
if gitVersion == nil {
307+
panic("git module is not initialized") // it shouldn't happen
306308
}
307309
atLeastVersion, err := version.NewVersion(atLeast)
308310
if err != nil {
@@ -314,6 +316,21 @@ func CheckGitVersionAtLeast(atLeast string) error {
314316
return nil
315317
}
316318

319+
func checkGitVersionCompatibility(gitVer *version.Version) error {
320+
badVersions := []struct {
321+
Version *version.Version
322+
Reason string
323+
}{
324+
{version.Must(version.NewVersion("2.43.1")), "regression bug of GIT_FLUSH"},
325+
}
326+
for _, bad := range badVersions {
327+
if gitVer.Equal(bad.Version) {
328+
return errors.New(bad.Reason)
329+
}
330+
}
331+
return nil
332+
}
333+
317334
func configSet(key, value string) error {
318335
stdout, _, err := NewCommand(DefaultContext, "config", "--global", "--get").AddDynamicArguments(key).RunStdString(nil)
319336
if err != nil && !err.IsExitCode(1) {

modules/git/git_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/setting"
1414
"code.gitea.io/gitea/modules/util"
1515

16+
"github.com/hashicorp/go-version"
1617
"github.com/stretchr/testify/assert"
1718
)
1819

@@ -93,3 +94,25 @@ func TestSyncConfig(t *testing.T) {
9394
assert.True(t, gitConfigContains("[sync-test]"))
9495
assert.True(t, gitConfigContains("cfg-key-a = CfgValA"))
9596
}
97+
98+
func TestParseGitVersion(t *testing.T) {
99+
v, err := parseGitVersionLine("git version 2.29.3")
100+
assert.NoError(t, err)
101+
assert.Equal(t, "2.29.3", v.String())
102+
103+
v, err = parseGitVersionLine("git version 2.29.3.windows.1")
104+
assert.NoError(t, err)
105+
assert.Equal(t, "2.29.3", v.String())
106+
107+
_, err = parseGitVersionLine("git version")
108+
assert.Error(t, err)
109+
110+
_, err = parseGitVersionLine("git version windows")
111+
assert.Error(t, err)
112+
}
113+
114+
func TestCheckGitVersionCompatibility(t *testing.T) {
115+
assert.NoError(t, checkGitVersionCompatibility(version.Must(version.NewVersion("2.43.0"))))
116+
assert.ErrorContains(t, checkGitVersionCompatibility(version.Must(version.NewVersion("2.43.1"))), "regression bug of GIT_FLUSH")
117+
assert.NoError(t, checkGitVersionCompatibility(version.Must(version.NewVersion("2.43.2"))))
118+
}

modules/git/repo_attribute.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"os"
1212

1313
"code.gitea.io/gitea/modules/log"
14+
15+
"github.com/hashicorp/go-version"
1416
)
1517

1618
// CheckAttributeOpts represents the possible options to CheckAttribute
@@ -133,7 +135,14 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error {
133135
c.env = append(c.env, "GIT_WORK_TREE="+c.WorkTree)
134136
}
135137

136-
c.env = append(c.env, "GIT_FLUSH=1")
138+
if gitVersion.Equal(version.Must(version.NewVersion("2.43.1"))) {
139+
// https://github.com/go-gitea/gitea/issues/29141 gitea hanging with git 2.43.1 #29141
140+
// https://lore.kernel.org/git/CABn0oJvg3M_kBW-u=j3QhKnO=6QOzk-YFTgonYw_UvFS1NTX4g@mail.gmail.com/
141+
// git 2.43.1 has a bug: the GIT_FLUSH polarity is flipped
142+
c.env = append(c.env, "GIT_FLUSH=0")
143+
} else {
144+
c.env = append(c.env, "GIT_FLUSH=1")
145+
}
137146

138147
c.cmd.AddDynamicArguments(c.Attributes...)
139148

0 commit comments

Comments
 (0)