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.

71 lines
1.4 KiB

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