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

repo2.go 3.0 kB

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