repo2.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/speedata/gogit"
  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 repofiles []*RepoFile
  39. lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int {
  40. if dirname == rpath {
  41. repofiles = append(repofiles, &RepoFile{
  42. entry.Id,
  43. entry.Filemode,
  44. entry.Name,
  45. path.Join(dirname, entry.Name),
  46. lastCommit.Message(),
  47. lastCommit.Committer.When,
  48. })
  49. }
  50. return 0
  51. })
  52. return repofiles, nil
  53. }