git.go 1.5 KB

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