Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

git.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "fmt"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/process"
  12. "github.com/mcuadros/go-version"
  13. )
  14. // Version return this package's current version
  15. func Version() string {
  16. return "0.4.2"
  17. }
  18. var (
  19. // Debug enables verbose logging on everything.
  20. // This should be false in case Gogs starts in SSH mode.
  21. Debug = false
  22. // Prefix the log prefix
  23. Prefix = "[git-module] "
  24. // GitVersionRequired is the minimum Git version required
  25. GitVersionRequired = "1.7.2"
  26. // GitExecutable is the command name of git
  27. // Could be updated to an absolute path while initialization
  28. GitExecutable = "git"
  29. gitVersion string
  30. )
  31. func log(format string, args ...interface{}) {
  32. if !Debug {
  33. return
  34. }
  35. fmt.Print(Prefix)
  36. if len(args) == 0 {
  37. fmt.Println(format)
  38. } else {
  39. fmt.Printf(format+"\n", args...)
  40. }
  41. }
  42. // BinVersion returns current Git version from shell.
  43. func BinVersion() (string, error) {
  44. if len(gitVersion) > 0 {
  45. return gitVersion, nil
  46. }
  47. stdout, err := NewCommand("version").Run()
  48. if err != nil {
  49. return "", err
  50. }
  51. fields := strings.Fields(stdout)
  52. if len(fields) < 3 {
  53. return "", fmt.Errorf("not enough output: %s", stdout)
  54. }
  55. // Handle special case on Windows.
  56. i := strings.Index(fields[2], "windows")
  57. if i >= 1 {
  58. gitVersion = fields[2][:i-1]
  59. return gitVersion, nil
  60. }
  61. gitVersion = fields[2]
  62. return gitVersion, nil
  63. }
  64. // SetExecutablePath changes the path of git executable and checks the file permission and version.
  65. func SetExecutablePath(path string) error {
  66. // If path is empty, we use the default value of GitExecutable "git" to search for the location of git.
  67. if path != "" {
  68. GitExecutable = path
  69. }
  70. absPath, err := exec.LookPath(GitExecutable)
  71. if err != nil {
  72. return fmt.Errorf("Git not found: %v", err)
  73. }
  74. GitExecutable = absPath
  75. gitVersion, err := BinVersion()
  76. if err != nil {
  77. return fmt.Errorf("Git version missing: %v", err)
  78. }
  79. if version.Compare(gitVersion, GitVersionRequired, "<") {
  80. return fmt.Errorf("Git version not supported. Requires version > %v", GitVersionRequired)
  81. }
  82. return nil
  83. }
  84. // Init initializes git module
  85. func Init() error {
  86. // Git requires setting user.name and user.email in order to commit changes.
  87. for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "gitea@fake.local"} {
  88. if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
  89. // ExitError indicates this config is not set
  90. if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
  91. if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
  92. return fmt.Errorf("Failed to set git %s(%s): %s", configKey, gerr, stderr)
  93. }
  94. } else {
  95. return fmt.Errorf("Failed to get git %s(%s): %s", configKey, err, stderr)
  96. }
  97. }
  98. }
  99. // Set git some configurations.
  100. if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
  101. GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
  102. return fmt.Errorf("Failed to execute 'git config --global core.quotepath false': %s", stderr)
  103. }
  104. if version.Compare(gitVersion, "2.18", ">=") {
  105. if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.commitGraph true)",
  106. GitExecutable, "config", "--global", "core.commitGraph", "true"); err != nil {
  107. return fmt.Errorf("Failed to execute 'git config --global core.commitGraph true': %s", stderr)
  108. }
  109. if _, stderr, err := process.GetManager().Exec("git.Init(git config --global gc.writeCommitGraph true)",
  110. GitExecutable, "config", "--global", "gc.writeCommitGraph", "true"); err != nil {
  111. return fmt.Errorf("Failed to execute 'git config --global gc.writeCommitGraph true': %s", stderr)
  112. }
  113. }
  114. return nil
  115. }
  116. // Fsck verifies the connectivity and validity of the objects in the database
  117. func Fsck(repoPath string, timeout time.Duration, args ...string) error {
  118. // Make sure timeout makes sense.
  119. if timeout <= 0 {
  120. timeout = -1
  121. }
  122. _, err := NewCommand("fsck").AddArguments(args...).RunInDirTimeout(timeout, repoPath)
  123. return err
  124. }