git.go 1.4 KB

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