You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo2.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 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 models
  5. import (
  6. "path"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/gogits/git"
  11. )
  12. type Commit struct {
  13. Author string
  14. Email string
  15. Date time.Time
  16. SHA string
  17. Message string
  18. }
  19. type RepoFile struct {
  20. *git.TreeEntry
  21. Path string
  22. Message string
  23. Created time.Time
  24. Size int64
  25. }
  26. func GetBranches(userName, reposName string) ([]string, error) {
  27. repo, err := git.OpenRepository(RepoPath(userName, reposName))
  28. if err != nil {
  29. return nil, err
  30. }
  31. refs, err := repo.AllReferences()
  32. if err != nil {
  33. return nil, err
  34. }
  35. brs := make([]string, len(refs))
  36. for i, ref := range refs {
  37. brs[i] = ref.Name
  38. }
  39. return brs, nil
  40. }
  41. func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
  42. repo, err := git.OpenRepository(RepoPath(userName, reposName))
  43. if err != nil {
  44. return nil, err
  45. }
  46. ref, err := repo.LookupReference("refs/heads/" + branchName)
  47. if err != nil {
  48. return nil, err
  49. }
  50. lastCommit, err := repo.LookupCommit(ref.Oid)
  51. if err != nil {
  52. return nil, err
  53. }
  54. var repodirs []*RepoFile
  55. var repofiles []*RepoFile
  56. lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
  57. if dirname == rpath {
  58. size, err := repo.ObjectSize(entry.Id)
  59. if err != nil {
  60. return 0
  61. }
  62. switch entry.Filemode {
  63. case git.FileModeBlob, git.FileModeBlobExec:
  64. repofiles = append(repofiles, &RepoFile{
  65. entry,
  66. path.Join(dirname, entry.Name),
  67. lastCommit.Message(),
  68. lastCommit.Committer.When,
  69. size,
  70. })
  71. case git.FileModeTree:
  72. repodirs = append(repodirs, &RepoFile{
  73. entry,
  74. path.Join(dirname, entry.Name),
  75. lastCommit.Message(),
  76. lastCommit.Committer.When,
  77. size,
  78. })
  79. }
  80. }
  81. return 0
  82. })
  83. return append(repodirs, repofiles...), nil
  84. }
  85. func GetLastestCommit(userName, repoName string) (*Commit, error) {
  86. stdout, _, err := com.ExecCmd("git", "--git-dir="+RepoPath(userName, repoName), "log", "-1")
  87. if err != nil {
  88. return nil, err
  89. }
  90. commit := new(Commit)
  91. for _, line := range strings.Split(stdout, "\n") {
  92. if len(line) == 0 {
  93. continue
  94. }
  95. switch {
  96. case line[0] == 'c':
  97. commit.SHA = line[7:]
  98. case line[0] == 'A':
  99. infos := strings.SplitN(line, " ", 3)
  100. commit.Author = infos[1]
  101. commit.Email = infos[2][1 : len(infos[2])-1]
  102. case line[0] == 'D':
  103. commit.Date, err = time.Parse("Mon Jan 02 15:04:05 2006 -0700", line[8:])
  104. if err != nil {
  105. return nil, err
  106. }
  107. case line[:4] == " ":
  108. commit.Message = line[4:]
  109. }
  110. }
  111. return commit, nil
  112. }