repo.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. "sync"
  12. )
  13. type ReportElem struct {
  14. Lines []string `json:"lines"`
  15. Branch string `json:"branch"`
  16. CommitA string `json:"commitA"`
  17. CommitB string `json:"commitB"`
  18. }
  19. type Repo struct {
  20. url string
  21. name string
  22. path string
  23. }
  24. func repoStart(repoUrl string) {
  25. err := exec.Command("git", "clone", repoUrl).Run()
  26. if err != nil {
  27. log.Fatalf("failed to clone repo %v", err)
  28. }
  29. repoName := strings.Split(repoUrl, "/")[4]
  30. if err := os.Chdir(repoName); err != nil {
  31. log.Fatal(err)
  32. }
  33. repo := Repo{repoUrl, repoName, ""}
  34. report := repo.audit()
  35. repo.cleanup()
  36. reportJson, _ := json.MarshalIndent(report, "", "\t")
  37. err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repo.name), reportJson, 0644)
  38. }
  39. // cleanup changes to app root and recursive rms target repo
  40. func (repo Repo) cleanup() {
  41. if err := os.Chdir(appRoot); err != nil {
  42. log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
  43. }
  44. err := exec.Command("rm", "-rf", repo.name).Run()
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. }
  49. // audit parses git branch --all
  50. func (repo Repo) audit() []ReportElem {
  51. var (
  52. out []byte
  53. err error
  54. branch string
  55. commits [][]byte
  56. leaks []string
  57. wg sync.WaitGroup
  58. )
  59. out, err = exec.Command("git", "branch", "--all").Output()
  60. if err != nil {
  61. log.Fatalf("error retrieving branches %v\n", err)
  62. }
  63. // iterate through branches, git rev-list <branch>
  64. branches := bytes.Split(out, []byte("\n"))
  65. messages := make(chan string)
  66. wg.Add(len(branches) - 3)
  67. for i, branchB := range branches {
  68. if i < 2 || i == len(branches)-1 {
  69. continue
  70. }
  71. branch = string(bytes.Trim(branchB, " "))
  72. go func(branch string) {
  73. defer wg.Done()
  74. cmd := exec.Command("git", "rev-list", branch)
  75. if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repo.name)); err != nil {
  76. log.Fatal(err)
  77. }
  78. out, err := cmd.Output()
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. // iterate through commits
  83. commits = bytes.Split(out, []byte("\n"))
  84. for j, commitB := range commits {
  85. fmt.Println(branch, string(commitB), j)
  86. if j == len(commits)-2 {
  87. break
  88. }
  89. leaks = checkDiff(string(commitB), string(commits[j+1]))
  90. if len(leaks) != 0 {
  91. report = append(report, ReportElem{leaks, branch,
  92. string(commitB), string(commits[j+1])})
  93. }
  94. }
  95. messages <- branch
  96. }(branch)
  97. go func() {
  98. for i := range messages {
  99. fmt.Println(i)
  100. }
  101. }()
  102. }
  103. wg.Wait()
  104. return report
  105. }