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.

repo_commit.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. "bytes"
  7. "container/list"
  8. "errors"
  9. "strings"
  10. "sync"
  11. "github.com/Unknwon/com"
  12. )
  13. func (repo *Repository) getCommitIdOfRef(refpath string) (string, error) {
  14. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "show-ref", "--verify", refpath)
  15. if err != nil {
  16. return "", errors.New(stderr)
  17. }
  18. return strings.Split(stdout, " ")[0], nil
  19. }
  20. func (repo *Repository) GetCommitIdOfBranch(branchName string) (string, error) {
  21. return repo.getCommitIdOfRef("refs/heads/" + branchName)
  22. }
  23. // get branch's last commit or a special commit by id string
  24. func (repo *Repository) GetCommitOfBranch(branchName string) (*Commit, error) {
  25. commitId, err := repo.GetCommitIdOfBranch(branchName)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return repo.GetCommit(commitId)
  30. }
  31. func (repo *Repository) GetCommitIdOfTag(tagName string) (string, error) {
  32. return repo.getCommitIdOfRef("refs/tags/" + tagName)
  33. }
  34. func (repo *Repository) GetCommitOfTag(tagName string) (*Commit, error) {
  35. commitId, err := repo.GetCommitIdOfTag(tagName)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return repo.GetCommit(commitId)
  40. }
  41. // Parse commit information from the (uncompressed) raw
  42. // data from the commit object.
  43. // \n\n separate headers from message
  44. func parseCommitData(data []byte) (*Commit, error) {
  45. commit := new(Commit)
  46. commit.parents = make([]sha1, 0, 1)
  47. // we now have the contents of the commit object. Let's investigate...
  48. nextline := 0
  49. l:
  50. for {
  51. eol := bytes.IndexByte(data[nextline:], '\n')
  52. switch {
  53. case eol > 0:
  54. line := data[nextline : nextline+eol]
  55. spacepos := bytes.IndexByte(line, ' ')
  56. reftype := line[:spacepos]
  57. switch string(reftype) {
  58. case "tree":
  59. id, err := NewIdFromString(string(line[spacepos+1:]))
  60. if err != nil {
  61. return nil, err
  62. }
  63. commit.Tree.Id = id
  64. case "parent":
  65. // A commit can have one or more parents
  66. oid, err := NewIdFromString(string(line[spacepos+1:]))
  67. if err != nil {
  68. return nil, err
  69. }
  70. commit.parents = append(commit.parents, oid)
  71. case "author":
  72. sig, err := newSignatureFromCommitline(line[spacepos+1:])
  73. if err != nil {
  74. return nil, err
  75. }
  76. commit.Author = sig
  77. case "committer":
  78. sig, err := newSignatureFromCommitline(line[spacepos+1:])
  79. if err != nil {
  80. return nil, err
  81. }
  82. commit.Committer = sig
  83. }
  84. nextline += eol + 1
  85. case eol == 0:
  86. commit.CommitMessage = string(data[nextline+1:])
  87. break l
  88. default:
  89. break l
  90. }
  91. }
  92. return commit, nil
  93. }
  94. func (repo *Repository) getCommit(id sha1) (*Commit, error) {
  95. if repo.commitCache != nil {
  96. if c, ok := repo.commitCache[id]; ok {
  97. return c, nil
  98. }
  99. } else {
  100. repo.commitCache = make(map[sha1]*Commit, 10)
  101. }
  102. data, bytErr, err := com.ExecCmdDirBytes(repo.Path, "git", "cat-file", "-p", id.String())
  103. if err != nil {
  104. return nil, errors.New(err.Error() + ": " + string(bytErr))
  105. }
  106. commit, err := parseCommitData(data)
  107. if err != nil {
  108. return nil, err
  109. }
  110. commit.repo = repo
  111. commit.Id = id
  112. repo.commitCache[id] = commit
  113. return commit, nil
  114. }
  115. // Find the commit object in the repository.
  116. func (repo *Repository) GetCommit(commitId string) (*Commit, error) {
  117. id, err := NewIdFromString(commitId)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return repo.getCommit(id)
  122. }
  123. func (repo *Repository) commitsCount(id sha1) (int, error) {
  124. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count", id.String())
  125. if err != nil {
  126. return 0, errors.New(stderr)
  127. }
  128. return com.StrTo(strings.TrimSpace(stdout)).Int()
  129. }
  130. // used only for single tree, (]
  131. func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
  132. l := list.New()
  133. if last == nil || last.ParentCount() == 0 {
  134. return l, nil
  135. }
  136. var err error
  137. cur := last
  138. for {
  139. if cur.Id.Equal(before.Id) {
  140. break
  141. }
  142. l.PushBack(cur)
  143. if cur.ParentCount() == 0 {
  144. break
  145. }
  146. cur, err = cur.Parent(0)
  147. if err != nil {
  148. return nil, err
  149. }
  150. }
  151. return l, nil
  152. }
  153. func (repo *Repository) commitsBefore(lock *sync.Mutex, l *list.List, parent *list.Element, id sha1, limit int) error {
  154. commit, err := repo.getCommit(id)
  155. if err != nil {
  156. return err
  157. }
  158. var e *list.Element
  159. if parent == nil {
  160. e = l.PushBack(commit)
  161. } else {
  162. var in = parent
  163. for {
  164. if in == nil {
  165. break
  166. } else if in.Value.(*Commit).Id.Equal(commit.Id) {
  167. return nil
  168. } else {
  169. if in.Next() == nil {
  170. break
  171. }
  172. if in.Value.(*Commit).Committer.When.Equal(commit.Committer.When) {
  173. break
  174. }
  175. if in.Value.(*Commit).Committer.When.After(commit.Committer.When) &&
  176. in.Next().Value.(*Commit).Committer.When.Before(commit.Committer.When) {
  177. break
  178. }
  179. }
  180. in = in.Next()
  181. }
  182. e = l.InsertAfter(commit, in)
  183. }
  184. var pr = parent
  185. if commit.ParentCount() > 1 {
  186. pr = e
  187. }
  188. for i := 0; i < commit.ParentCount(); i++ {
  189. id, err := commit.ParentId(i)
  190. if err != nil {
  191. return err
  192. }
  193. err = repo.commitsBefore(lock, l, pr, id, 0)
  194. if err != nil {
  195. return err
  196. }
  197. }
  198. return nil
  199. }
  200. func (repo *Repository) CommitsCount(commitId string) (int, error) {
  201. id, err := NewIdFromString(commitId)
  202. if err != nil {
  203. return 0, err
  204. }
  205. return repo.commitsCount(id)
  206. }
  207. func (repo *Repository) FileCommitsCount(branch, file string) (int, error) {
  208. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count",
  209. branch, "--", file)
  210. if err != nil {
  211. return 0, errors.New(stderr)
  212. }
  213. return com.StrTo(strings.TrimSpace(stdout)).Int()
  214. }
  215. func (repo *Repository) CommitsByFileAndRange(branch, file string, page int) (*list.List, error) {
  216. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", branch,
  217. "--skip="+com.ToStr((page-1)*50), "--max-count=50", prettyLogFormat, "--", file)
  218. if err != nil {
  219. return nil, errors.New(string(stderr))
  220. }
  221. return parsePrettyFormatLog(repo, stdout)
  222. }
  223. func (repo *Repository) getCommitsBefore(id sha1) (*list.List, error) {
  224. l := list.New()
  225. lock := new(sync.Mutex)
  226. err := repo.commitsBefore(lock, l, nil, id, 0)
  227. return l, err
  228. }
  229. func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, error) {
  230. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(), "-100",
  231. "-i", "--grep="+keyword, prettyLogFormat)
  232. if err != nil {
  233. return nil, err
  234. } else if len(stderr) > 0 {
  235. return nil, errors.New(string(stderr))
  236. }
  237. return parsePrettyFormatLog(repo, stdout)
  238. }
  239. func (repo *Repository) commitsByRange(id sha1, page int) (*list.List, error) {
  240. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(),
  241. "--skip="+com.ToStr((page-1)*50), "--max-count=50", prettyLogFormat)
  242. if err != nil {
  243. return nil, errors.New(string(stderr))
  244. }
  245. return parsePrettyFormatLog(repo, stdout)
  246. }
  247. func (repo *Repository) getCommitOfRelPath(id sha1, relPath string) (*Commit, error) {
  248. stdout, _, err := com.ExecCmdDir(repo.Path, "git", "log", "-1", prettyLogFormat, id.String(), "--", relPath)
  249. if err != nil {
  250. return nil, err
  251. }
  252. id, err = NewIdFromString(string(stdout))
  253. if err != nil {
  254. return nil, err
  255. }
  256. return repo.getCommit(id)
  257. }