repository.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package test
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "gopkg.in/src-d/go-git.v4"
  8. "gopkg.in/src-d/go-git.v4/plumbing"
  9. "gopkg.in/src-d/go-git.v4/plumbing/object"
  10. "gopkg.in/src-d/go-git.v4/storage/memory"
  11. )
  12. // Repository is a boilerplate sample repository (Hercules itself).
  13. var Repository *git.Repository
  14. // FakeChangeForName creates an artificial Git Change from a file name and two arbitrary hashes.
  15. func FakeChangeForName(name string, hashFrom string, hashTo string) *object.Change {
  16. return &object.Change{
  17. From: object.ChangeEntry{Name: name, TreeEntry: object.TreeEntry{
  18. Name: name, Hash: plumbing.NewHash(hashFrom),
  19. }},
  20. To: object.ChangeEntry{Name: name, TreeEntry: object.TreeEntry{
  21. Name: name, Hash: plumbing.NewHash(hashTo),
  22. }},
  23. }
  24. }
  25. func init() {
  26. cwd, err := os.Getwd()
  27. if err == nil {
  28. for true {
  29. files, err := ioutil.ReadDir(cwd)
  30. if err != nil {
  31. break
  32. }
  33. found := false
  34. for _, f := range files {
  35. if f.Name() == "README.md" {
  36. found = true
  37. break
  38. }
  39. }
  40. if found {
  41. break
  42. }
  43. oldCwd := cwd
  44. cwd = path.Dir(cwd)
  45. if oldCwd == cwd {
  46. break
  47. }
  48. }
  49. Repository, err = git.PlainOpen(cwd)
  50. if err == nil {
  51. iter, err := Repository.CommitObjects()
  52. if err == nil {
  53. commits := -1
  54. for ; err != io.EOF; _, err = iter.Next() {
  55. if err != nil {
  56. panic(err)
  57. }
  58. commits++
  59. if commits >= 100 {
  60. return
  61. }
  62. }
  63. }
  64. }
  65. }
  66. Repository, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
  67. URL: "https://github.com/src-d/hercules",
  68. })
  69. if err != nil {
  70. panic(err)
  71. }
  72. }