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.

commit.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) GetCommitOfRelPath(relPath string) (*Commit, error) {
  64. return c.repo.getCommitOfRelPath(c.Id, relPath)
  65. }