gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/modules/git/gitcmd"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/hashicorp/go-version"
  18. )
  19. const RequiredVersion = "2.0.0" // the minimum Git version required
  20. type Features struct {
  21. gitVersion *version.Version
  22. UsingGogit bool
  23. SupportProcReceive bool // >= 2.29
  24. SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
  25. SupportedObjectFormats []ObjectFormat // sha1, sha256
  26. SupportCheckAttrOnBare bool // >= 2.40
  27. }
  28. var defaultFeatures *Features
  29. func (f *Features) CheckVersionAtLeast(atLeast string) bool {
  30. return f.gitVersion.Compare(version.Must(version.NewVersion(atLeast))) >= 0
  31. }
  32. // VersionInfo returns git version information
  33. func (f *Features) VersionInfo() string {
  34. return f.gitVersion.Original()
  35. }
  36. func DefaultFeatures() *Features {
  37. if defaultFeatures == nil {
  38. if !setting.IsProd || setting.IsInTesting {
  39. log.Warn("git.DefaultFeatures is called before git.InitXxx, initializing with default values")
  40. }
  41. if err := InitSimple(); err != nil {
  42. log.Fatal("git.InitSimple failed: %v", err)
  43. }
  44. }
  45. return defaultFeatures
  46. }
  47. func loadGitVersionFeatures() (*Features, error) {
  48. stdout, _, runErr := gitcmd.NewCommand("version").RunStdString(context.Background(), nil)
  49. if runErr != nil {
  50. return nil, runErr
  51. }
  52. ver, err := parseGitVersionLine(strings.TrimSpace(stdout))
  53. if err != nil {
  54. return nil, err
  55. }
  56. features := &Features{gitVersion: ver, UsingGogit: isGogit}
  57. features.SupportProcReceive = features.CheckVersionAtLeast("2.29")
  58. features.SupportHashSha256 = features.CheckVersionAtLeast("2.42") && !isGogit
  59. features.SupportedObjectFormats = []ObjectFormat{Sha1ObjectFormat}
  60. if features.SupportHashSha256 {
  61. features.SupportedObjectFormats = append(features.SupportedObjectFormats, Sha256ObjectFormat)
  62. }
  63. features.SupportCheckAttrOnBare = features.CheckVersionAtLeast("2.40")
  64. return features, nil
  65. }
  66. func parseGitVersionLine(s string) (*version.Version, error) {
  67. fields := strings.Fields(s)
  68. if len(fields) < 3 {
  69. return nil, fmt.Errorf("invalid git version: %q", s)
  70. }
  71. // version string is like: "git version 2.29.3" or "git version 2.29.3.windows.1"
  72. versionString := fields[2]
  73. if pos := strings.Index(versionString, "windows"); pos >= 1 {
  74. versionString = versionString[:pos-1]
  75. }
  76. return version.NewVersion(versionString)
  77. }
  78. func checkGitVersionCompatibility(gitVer *version.Version) error {
  79. badVersions := []struct {
  80. Version *version.Version
  81. Reason string
  82. }{
  83. {version.Must(version.NewVersion("2.43.1")), "regression bug of GIT_FLUSH"},
  84. }
  85. for _, bad := range badVersions {
  86. if gitVer.Equal(bad.Version) {
  87. return errors.New(bad.Reason)
  88. }
  89. }
  90. return nil
  91. }
  92. func ensureGitVersion() error {
  93. if !DefaultFeatures().CheckVersionAtLeast(RequiredVersion) {
  94. moreHint := "get git: https://git-scm.com/downloads"
  95. if runtime.GOOS == "linux" {
  96. // there are a lot of CentOS/RHEL users using old git, so we add a special hint for them
  97. if _, err := os.Stat("/etc/redhat-release"); err == nil {
  98. // ius.io is the recommended official(git-scm.com) method to install git
  99. moreHint = "get git: https://git-scm.com/downloads/linux and https://ius.io"
  100. }
  101. }
  102. return fmt.Errorf("installed git version %q is not supported, Gitea requires git version >= %q, %s", DefaultFeatures().gitVersion.Original(), RequiredVersion, moreHint)
  103. }
  104. if err := checkGitVersionCompatibility(DefaultFeatures().gitVersion); err != nil {
  105. return fmt.Errorf("installed git version %s has a known compatibility issue with Gitea: %w, please upgrade (or downgrade) git", DefaultFeatures().gitVersion.String(), err)
  106. }
  107. return nil
  108. }
  109. // InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
  110. // This method doesn't change anything to filesystem. At the moment, it is only used by some Gitea sub-commands.
  111. func InitSimple() error {
  112. if setting.Git.HomePath == "" {
  113. return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
  114. }
  115. if defaultFeatures != nil && (!setting.IsProd || setting.IsInTesting) {
  116. log.Warn("git module has been initialized already, duplicate init may work but it's better to fix it")
  117. }
  118. if setting.Git.Timeout.Default > 0 {
  119. gitcmd.SetDefaultCommandExecutionTimeout(time.Duration(setting.Git.Timeout.Default) * time.Second)
  120. }
  121. if err := gitcmd.SetExecutablePath(setting.Git.Path); err != nil {
  122. return err
  123. }
  124. var err error
  125. defaultFeatures, err = loadGitVersionFeatures()
  126. if err != nil {
  127. return err
  128. }
  129. if err = ensureGitVersion(); err != nil {
  130. return err
  131. }
  132. // when git works with gnupg (commit signing), there should be a stable home for gnupg commands
  133. if _, ok := os.LookupEnv("GNUPGHOME"); !ok {
  134. _ = os.Setenv("GNUPGHOME", filepath.Join(gitcmd.HomeDir(), ".gnupg"))
  135. }
  136. return nil
  137. }
  138. // InitFull initializes git module with version check and change global variables, sync gitconfig.
  139. // It should only be called once at the beginning of the program initialization (TestMain/GlobalInitInstalled) as this code makes unsynchronized changes to variables.
  140. func InitFull() (err error) {
  141. if err = InitSimple(); err != nil {
  142. return err
  143. }
  144. if setting.LFS.StartServer {
  145. if !DefaultFeatures().CheckVersionAtLeast("2.1.2") {
  146. return errors.New("LFS server support requires Git >= 2.1.2")
  147. }
  148. }
  149. return syncGitConfig(context.Background())
  150. }