commit.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "container/list"
  7. "strings"
  8. )
  9. // Commit represents a git commit.
  10. type Commit struct {
  11. Tree
  12. Id sha1 // The id of this commit object
  13. Author *Signature
  14. Committer *Signature
  15. CommitMessage string
  16. parents []sha1 // sha1 strings
  17. }
  18. // Return the commit message. Same as retrieving CommitMessage directly.
  19. func (c *Commit) Message() string {
  20. return c.CommitMessage
  21. }
  22. func (c *Commit) Summary() string {
  23. return strings.Split(c.CommitMessage, "\n")[0]
  24. }
  25. // Return oid of the parent number n (0-based index). Return nil if no such parent exists.
  26. func (c *Commit) ParentId(n int) (id sha1, err error) {
  27. if n >= len(c.parents) {
  28. err = IdNotExist
  29. return
  30. }
  31. return c.parents[n], nil
  32. }
  33. // Return parent number n (0-based index)
  34. func (c *Commit) Parent(n int) (*Commit, error) {
  35. id, err := c.ParentId(n)
  36. if err != nil {
  37. return nil, err
  38. }
  39. parent, err := c.repo.getCommit(id)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return parent, nil
  44. }
  45. // Return the number of parents of the commit. 0 if this is the
  46. // root commit, otherwise 1,2,...
  47. func (c *Commit) ParentCount() int {
  48. return len(c.parents)
  49. }
  50. func (c *Commit) CommitsBefore() (*list.List, error) {
  51. return c.repo.getCommitsBefore(c.Id)
  52. }
  53. func (c *Commit) CommitsBeforeUntil(commitId string) (*list.List, error) {
  54. ec, err := c.repo.GetCommit(commitId)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return c.repo.CommitsBetween(c, ec)
  59. }
  60. func (c *Commit) CommitsCount() (int, error) {
  61. return c.repo.commitsCount(c.Id)
  62. }
  63. func (c *Commit) SearchCommits(keyword string) (*list.List, error) {
  64. return c.repo.searchCommits(c.Id, keyword)
  65. }
  66. func (c *Commit) CommitsByRange(page int) (*list.List, error) {
  67. return c.repo.commitsByRange(c.Id, page)
  68. }
  69. func (c *Commit) GetCommitOfRelPath(relPath string) (*Commit, error) {
  70. return c.repo.getCommitOfRelPath(c.Id, relPath)
  71. }