repo2.go 1.3 KB

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