You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.0 KiB

  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. "strings"
  9. "time"
  10. "github.com/mcuadros/go-version"
  11. )
  12. // Version return this package's current version
  13. func Version() string {
  14. return "0.4.2"
  15. }
  16. var (
  17. // Debug enables verbose logging on everything.
  18. // This should be false in case Gogs starts in SSH mode.
  19. Debug = false
  20. // Prefix the log prefix
  21. Prefix = "[git-module] "
  22. // GitVersionRequired is the minimum Git version required
  23. GitVersionRequired = "1.7.2"
  24. )
  25. func log(format string, args ...interface{}) {
  26. if !Debug {
  27. return
  28. }
  29. fmt.Print(Prefix)
  30. if len(args) == 0 {
  31. fmt.Println(format)
  32. } else {
  33. fmt.Printf(format+"\n", args...)
  34. }
  35. }
  36. var gitVersion string
  37. // BinVersion returns current Git version from shell.
  38. func BinVersion() (string, error) {
  39. if len(gitVersion) > 0 {
  40. return gitVersion, nil
  41. }
  42. stdout, err := NewCommand("version").Run()
  43. if err != nil {
  44. return "", err
  45. }
  46. fields := strings.Fields(stdout)
  47. if len(fields) < 3 {
  48. return "", fmt.Errorf("not enough output: %s", stdout)
  49. }
  50. // Handle special case on Windows.
  51. i := strings.Index(fields[2], "windows")
  52. if i >= 1 {
  53. gitVersion = fields[2][:i-1]
  54. return gitVersion, nil
  55. }
  56. gitVersion = fields[2]
  57. return gitVersion, nil
  58. }
  59. func init() {
  60. gitVersion, err := BinVersion()
  61. if err != nil {
  62. panic(fmt.Sprintf("Git version missing: %v", err))
  63. }
  64. if version.Compare(gitVersion, GitVersionRequired, "<") {
  65. panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
  66. }
  67. }
  68. // Fsck verifies the connectivity and validity of the objects in the database
  69. func Fsck(repoPath string, timeout time.Duration, args ...string) error {
  70. // Make sure timeout makes sense.
  71. if timeout <= 0 {
  72. timeout = -1
  73. }
  74. _, err := NewCommand("fsck").AddArguments(args...).RunInDirTimeout(timeout, repoPath)
  75. return err
  76. }