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.

134 lines
3.0 KiB

  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 git
  5. import (
  6. "bufio"
  7. "container/list"
  8. "strings"
  9. )
  10. // Commit represents a git commit.
  11. type Commit struct {
  12. Tree
  13. Id sha1 // The id of this commit object
  14. Author *Signature
  15. Committer *Signature
  16. CommitMessage string
  17. parents []sha1 // sha1 strings
  18. submodules map[string]*SubModule
  19. }
  20. // Return the commit message. Same as retrieving CommitMessage directly.
  21. func (c *Commit) Message() string {
  22. return c.CommitMessage
  23. }
  24. func (c *Commit) Summary() string {
  25. return strings.Split(c.CommitMessage, "\n")[0]
  26. }
  27. // Return oid of the parent number n (0-based index). Return nil if no such parent exists.
  28. func (c *Commit) ParentId(n int) (id sha1, err error) {
  29. if n >= len(c.parents) {
  30. err = IdNotExist
  31. return
  32. }
  33. return c.parents[n], nil
  34. }
  35. // Return parent number n (0-based index)
  36. func (c *Commit) Parent(n int) (*Commit, error) {
  37. id, err := c.ParentId(n)
  38. if err != nil {
  39. return nil, err
  40. }
  41. parent, err := c.repo.getCommit(id)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return parent, nil
  46. }
  47. // Return the number of parents of the commit. 0 if this is the
  48. // root commit, otherwise 1,2,...
  49. func (c *Commit) ParentCount() int {
  50. return len(c.parents)
  51. }
  52. func (c *Commit) CommitsBefore() (*list.List, error) {
  53. return c.repo.getCommitsBefore(c.Id)
  54. }
  55. func (c *Commit) CommitsBeforeUntil(commitId string) (*list.List, error) {
  56. ec, err := c.repo.GetCommit(commitId)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return c.repo.CommitsBetween(c, ec)
  61. }
  62. func (c *Commit) CommitsCount() (int, error) {
  63. return c.repo.commitsCount(c.Id)
  64. }
  65. func (c *Commit) SearchCommits(keyword string) (*list.List, error) {
  66. return c.repo.searchCommits(c.Id, keyword)
  67. }
  68. func (c *Commit) CommitsByRange(page int) (*list.List, error) {
  69. return c.repo.commitsByRange(c.Id, page)
  70. }
  71. func (c *Commit) GetCommitOfRelPath(relPath string) (*Commit, error) {
  72. return c.repo.getCommitOfRelPath(c.Id, relPath)
  73. }
  74. func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
  75. moduels, err := c.GetSubModules()
  76. if err != nil {
  77. return nil, err
  78. }
  79. return moduels[entryname], nil
  80. }
  81. func (c *Commit) GetSubModules() (map[string]*SubModule, error) {
  82. if c.submodules != nil {
  83. return c.submodules, nil
  84. }
  85. entry, err := c.GetTreeEntryByPath(".gitmodules")
  86. if err != nil {
  87. return nil, err
  88. }
  89. rd, err := entry.Blob().Data()
  90. if err != nil {
  91. return nil, err
  92. }
  93. scanner := bufio.NewScanner(rd)
  94. c.submodules = make(map[string]*SubModule)
  95. var ismodule bool
  96. var path string
  97. for scanner.Scan() {
  98. if strings.HasPrefix(scanner.Text(), "[submodule") {
  99. ismodule = true
  100. continue
  101. }
  102. if ismodule {
  103. fields := strings.Split(scanner.Text(), "=")
  104. k := strings.TrimSpace(fields[0])
  105. if k == "path" {
  106. path = strings.TrimSpace(fields[1])
  107. } else if k == "url" {
  108. c.submodules[path] = &SubModule{path, strings.TrimSpace(fields[1])}
  109. ismodule = false
  110. }
  111. }
  112. }
  113. return c.submodules, nil
  114. }