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.

80 lines
1.6 KiB

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