commit.go 3.5 KB

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