git.go 1.4 KB

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