Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

commit.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2015 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. "bytes"
  8. "container/list"
  9. "fmt"
  10. "net/http"
  11. "strconv"
  12. "strings"
  13. )
  14. // Commit represents a git commit.
  15. type Commit struct {
  16. Tree
  17. ID SHA1 // The ID of this commit object
  18. Author *Signature
  19. Committer *Signature
  20. CommitMessage string
  21. Signature *CommitGPGSignature
  22. parents []SHA1 // SHA1 strings
  23. submoduleCache *ObjectCache
  24. }
  25. // CommitGPGSignature represents a git commit signature part.
  26. type CommitGPGSignature struct {
  27. Signature string
  28. Payload string //TODO check if can be reconstruct from the rest of commit information to not have duplicate data
  29. }
  30. // similar to https://github.com/git/git/blob/3bc53220cb2dcf709f7a027a3f526befd021d858/commit.c#L1128
  31. func newGPGSignatureFromCommitline(data []byte, signatureStart int) (*CommitGPGSignature, error) {
  32. sig := new(CommitGPGSignature)
  33. signatureEnd := bytes.LastIndex(data, []byte("-----END PGP SIGNATURE-----"))
  34. if signatureEnd == -1 {
  35. return nil, fmt.Errorf("end of commit signature not found")
  36. }
  37. sig.Signature = strings.Replace(string(data[signatureStart:signatureEnd+27]), "\n ", "\n", -1)
  38. sig.Payload = string(data[:signatureStart-8]) + string(data[signatureEnd+27:])
  39. return sig, nil
  40. }
  41. // Message returns the commit message. Same as retrieving CommitMessage directly.
  42. func (c *Commit) Message() string {
  43. return c.CommitMessage
  44. }
  45. // Summary returns first line of commit message.
  46. func (c *Commit) Summary() string {
  47. return strings.Split(strings.TrimSpace(c.CommitMessage), "\n")[0]
  48. }
  49. // ParentID returns oid of n-th parent (0-based index).
  50. // It returns nil if no such parent exists.
  51. func (c *Commit) ParentID(n int) (SHA1, error) {
  52. if n >= len(c.parents) {
  53. return SHA1{}, ErrNotExist{"", ""}
  54. }
  55. return c.parents[n], nil
  56. }
  57. // Parent returns n-th parent (0-based index) of the commit.
  58. func (c *Commit) Parent(n int) (*Commit, error) {
  59. id, err := c.ParentID(n)
  60. if err != nil {
  61. return nil, err
  62. }
  63. parent, err := c.repo.getCommit(id)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return parent, nil
  68. }
  69. // ParentCount returns number of parents of the commit.
  70. // 0 if this is the root commit, otherwise 1,2, etc.
  71. func (c *Commit) ParentCount() int {
  72. return len(c.parents)
  73. }
  74. func isImageFile(data []byte) (string, bool) {
  75. contentType := http.DetectContentType(data)
  76. if strings.Index(contentType, "image/") != -1 {
  77. return contentType, true
  78. }
  79. return contentType, false
  80. }
  81. // IsImageFile is a file image type
  82. func (c *Commit) IsImageFile(name string) bool {
  83. blob, err := c.GetBlobByPath(name)
  84. if err != nil {
  85. return false
  86. }
  87. dataRc, err := blob.DataAsync()
  88. if err != nil {
  89. return false
  90. }
  91. defer dataRc.Close()
  92. buf := make([]byte, 1024)
  93. n, _ := dataRc.Read(buf)
  94. buf = buf[:n]
  95. _, isImage := isImageFile(buf)
  96. return isImage
  97. }
  98. // GetCommitByPath return the commit of relative path object.
  99. func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
  100. return c.repo.getCommitByPathWithID(c.ID, relpath)
  101. }
  102. // AddChanges marks local changes to be ready for commit.
  103. func AddChanges(repoPath string, all bool, files ...string) error {
  104. cmd := NewCommand("add")
  105. if all {
  106. cmd.AddArguments("--all")
  107. }
  108. _, err := cmd.AddArguments(files...).RunInDir(repoPath)
  109. return err
  110. }
  111. // CommitChangesOptions the options when a commit created
  112. type CommitChangesOptions struct {
  113. Committer *Signature
  114. Author *Signature
  115. Message string
  116. }
  117. // CommitChanges commits local changes with given committer, author and message.
  118. // If author is nil, it will be the same as committer.
  119. func CommitChanges(repoPath string, opts CommitChangesOptions) error {
  120. cmd := NewCommand()
  121. if opts.Committer != nil {
  122. cmd.AddArguments("-c", "user.name="+opts.Committer.Name, "-c", "user.email="+opts.Committer.Email)
  123. }
  124. cmd.AddArguments("commit")
  125. if opts.Author == nil {
  126. opts.Author = opts.Committer
  127. }
  128. if opts.Author != nil {
  129. cmd.AddArguments(fmt.Sprintf("--author='%s <%s>'", opts.Author.Name, opts.Author.Email))
  130. }
  131. cmd.AddArguments("-m", opts.Message)
  132. _, err := cmd.RunInDir(repoPath)
  133. // No stderr but exit status 1 means nothing to commit.
  134. if err != nil && err.Error() == "exit status 1" {
  135. return nil
  136. }
  137. return err
  138. }
  139. func commitsCount(repoPath, revision, relpath string) (int64, error) {
  140. var cmd *Command
  141. cmd = NewCommand("rev-list", "--count")
  142. cmd.AddArguments(revision)
  143. if len(relpath) > 0 {
  144. cmd.AddArguments("--", relpath)
  145. }
  146. stdout, err := cmd.RunInDir(repoPath)
  147. if err != nil {
  148. return 0, err
  149. }
  150. return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
  151. }
  152. // CommitsCount returns number of total commits of until given revision.
  153. func CommitsCount(repoPath, revision string) (int64, error) {
  154. return commitsCount(repoPath, revision, "")
  155. }
  156. // CommitsCount returns number of total commits of until current revision.
  157. func (c *Commit) CommitsCount() (int64, error) {
  158. return CommitsCount(c.repo.Path, c.ID.String())
  159. }
  160. // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
  161. func (c *Commit) CommitsByRange(page int) (*list.List, error) {
  162. return c.repo.commitsByRange(c.ID, page)
  163. }
  164. // CommitsBefore returns all the commits before current revision
  165. func (c *Commit) CommitsBefore() (*list.List, error) {
  166. return c.repo.getCommitsBefore(c.ID)
  167. }
  168. // CommitsBeforeLimit returns num commits before current revision
  169. func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error) {
  170. return c.repo.getCommitsBeforeLimit(c.ID, num)
  171. }
  172. // CommitsBeforeUntil returns the commits between commitID to current revision
  173. func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error) {
  174. endCommit, err := c.repo.GetCommit(commitID)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return c.repo.CommitsBetween(c, endCommit)
  179. }
  180. // SearchCommits returns the commits match the keyword before current revision
  181. func (c *Commit) SearchCommits(keyword string, all bool) (*list.List, error) {
  182. return c.repo.searchCommits(c.ID, keyword, all)
  183. }
  184. // GetFilesChangedSinceCommit get all changed file names between pastCommit to current revision
  185. func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) {
  186. return c.repo.getFilesChanged(pastCommit, c.ID.String())
  187. }
  188. // GetSubModules get all the sub modules of current revision git tree
  189. func (c *Commit) GetSubModules() (*ObjectCache, error) {
  190. if c.submoduleCache != nil {
  191. return c.submoduleCache, nil
  192. }
  193. entry, err := c.GetTreeEntryByPath(".gitmodules")
  194. if err != nil {
  195. if _, ok := err.(ErrNotExist); ok {
  196. return nil, nil
  197. }
  198. return nil, err
  199. }
  200. rd, err := entry.Blob().Data()
  201. if err != nil {
  202. return nil, err
  203. }
  204. scanner := bufio.NewScanner(rd)
  205. c.submoduleCache = newObjectCache()
  206. var ismodule bool
  207. var path string
  208. for scanner.Scan() {
  209. if strings.HasPrefix(scanner.Text(), "[submodule") {
  210. ismodule = true
  211. continue
  212. }
  213. if ismodule {
  214. fields := strings.Split(scanner.Text(), "=")
  215. k := strings.TrimSpace(fields[0])
  216. if k == "path" {
  217. path = strings.TrimSpace(fields[1])
  218. } else if k == "url" {
  219. c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])})
  220. ismodule = false
  221. }
  222. }
  223. }
  224. return c.submoduleCache, nil
  225. }
  226. // GetSubModule get the sub module according entryname
  227. func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
  228. modules, err := c.GetSubModules()
  229. if err != nil {
  230. return nil, err
  231. }
  232. if modules != nil {
  233. module, has := modules.Get(entryname)
  234. if has {
  235. return module.(*SubModule), nil
  236. }
  237. }
  238. return nil, nil
  239. }