repo2.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "time"
  8. git "github.com/gogits/git"
  9. )
  10. type RepoFile struct {
  11. Id *git.Oid
  12. Type int
  13. Name string
  14. Path string
  15. Message string
  16. Created time.Time
  17. }
  18. func (f *RepoFile) IsFile() bool {
  19. return f.Type == git.FileModeBlob || f.Type == git.FileModeBlobExec
  20. }
  21. func (f *RepoFile) IsDir() bool {
  22. return f.Type == git.FileModeTree
  23. }
  24. func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, error) {
  25. f := RepoPath(userName, reposName)
  26. repo, err := git.OpenRepository(f)
  27. if err != nil {
  28. return nil, err
  29. }
  30. ref, err := repo.LookupReference("refs/heads/" + branchName)
  31. if err != nil {
  32. return nil, err
  33. }
  34. lastCommit, err := repo.LookupCommit(ref.Oid)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var repodirs []*RepoFile
  39. var repofiles []*RepoFile
  40. lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
  41. if dirname == rpath {
  42. switch entry.Filemode {
  43. case git.FileModeBlob, git.FileModeBlobExec:
  44. repofiles = append(repofiles, &RepoFile{
  45. entry.Id,
  46. entry.Filemode,
  47. entry.Name,
  48. path.Join(dirname, entry.Name),
  49. lastCommit.Message(),
  50. lastCommit.Committer.When,
  51. })
  52. case git.FileModeTree:
  53. repodirs = append(repodirs, &RepoFile{
  54. entry.Id,
  55. entry.Filemode,
  56. entry.Name,
  57. path.Join(dirname, entry.Name),
  58. lastCommit.Message(),
  59. lastCommit.Committer.When,
  60. })
  61. }
  62. }
  63. return 0
  64. })
  65. return append(repodirs, repofiles...), nil
  66. }