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.

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. // Version represents version of Git.
  11. type Version struct {
  12. Major, Minor, Patch int
  13. }
  14. // GetVersion returns current Git version installed.
  15. func GetVersion() (Version, error) {
  16. stdout, stderr, err := com.ExecCmd("git", "version")
  17. if err != nil {
  18. return Version{}, errors.New(stderr)
  19. }
  20. infos := strings.Split(stdout, " ")
  21. if len(infos) < 3 {
  22. return Version{}, errors.New("not enough output")
  23. }
  24. v := Version{}
  25. for i, s := range strings.Split(strings.TrimSpace(infos[2]), ".") {
  26. switch i {
  27. case 0:
  28. v.Major, _ = com.StrTo(s).Int()
  29. case 1:
  30. v.Minor, _ = com.StrTo(s).Int()
  31. case 2:
  32. v.Patch, _ = com.StrTo(s).Int()
  33. }
  34. }
  35. return v, nil
  36. }