Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

version.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "errors"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. )
  10. var (
  11. // Cached Git version.
  12. gitVer *Version
  13. )
  14. // Version represents version of Git.
  15. type Version struct {
  16. Major, Minor, Patch int
  17. }
  18. func ParseVersion(verStr string) (*Version, error) {
  19. infos := strings.Split(verStr, ".")
  20. if len(infos) < 3 {
  21. return nil, errors.New("incorrect version input")
  22. }
  23. v := &Version{}
  24. for i, s := range infos {
  25. switch i {
  26. case 0:
  27. v.Major, _ = com.StrTo(s).Int()
  28. case 1:
  29. v.Minor, _ = com.StrTo(s).Int()
  30. case 2:
  31. v.Patch, _ = com.StrTo(s).Int()
  32. }
  33. }
  34. return v, nil
  35. }
  36. func MustParseVersion(verStr string) *Version {
  37. v, _ := ParseVersion(verStr)
  38. return v
  39. }
  40. // Compare compares two versions,
  41. // it returns 1 if original is greater, 1 if original is smaller, 0 if equal.
  42. func (v *Version) Compare(that *Version) int {
  43. if v.Major > that.Major {
  44. return 1
  45. } else if v.Major < that.Major {
  46. return -1
  47. }
  48. if v.Minor > that.Minor {
  49. return 1
  50. } else if v.Minor < that.Minor {
  51. return -1
  52. }
  53. if v.Patch > that.Patch {
  54. return 1
  55. } else if v.Patch < that.Patch {
  56. return -1
  57. }
  58. return 0
  59. }
  60. // GetVersion returns current Git version installed.
  61. func GetVersion() (*Version, error) {
  62. if gitVer != nil {
  63. return gitVer, nil
  64. }
  65. stdout, stderr, err := com.ExecCmd("git", "version")
  66. if err != nil {
  67. return nil, errors.New(stderr)
  68. }
  69. infos := strings.Split(stdout, " ")
  70. if len(infos) < 3 {
  71. return nil, errors.New("not enough output")
  72. }
  73. gitVer, err = ParseVersion(infos[2])
  74. return gitVer, err
  75. }