repo_commit.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. tag, err := repo.GetTag(tagName)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return tag.Commit()
  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. if gitVer.LessThan(MustParseVersion("1.8.0")) {
  125. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", "--pretty=format:''", id.String())
  126. if err != nil {
  127. return 0, errors.New(string(stderr))
  128. }
  129. return len(bytes.Split(stdout, []byte("\n"))), nil
  130. }
  131. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count", id.String())
  132. if err != nil {
  133. return 0, errors.New(stderr)
  134. }
  135. return com.StrTo(strings.TrimSpace(stdout)).Int()
  136. }
  137. // used only for single tree, (]
  138. func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
  139. l := list.New()
  140. if last == nil || last.ParentCount() == 0 {
  141. return l, nil
  142. }
  143. var err error
  144. cur := last
  145. for {
  146. if cur.Id.Equal(before.Id) {
  147. break
  148. }
  149. l.PushBack(cur)
  150. if cur.ParentCount() == 0 {
  151. break
  152. }
  153. cur, err = cur.Parent(0)
  154. if err != nil {
  155. return nil, err
  156. }
  157. }
  158. return l, nil
  159. }
  160. func (repo *Repository) commitsBefore(lock *sync.Mutex, l *list.List, parent *list.Element, id sha1, limit int) error {
  161. commit, err := repo.getCommit(id)
  162. if err != nil {
  163. return err
  164. }
  165. var e *list.Element
  166. if parent == nil {
  167. e = l.PushBack(commit)
  168. } else {
  169. var in = parent
  170. for {
  171. if in == nil {
  172. break
  173. } else if in.Value.(*Commit).Id.Equal(commit.Id) {
  174. return nil
  175. } else {
  176. if in.Next() == nil {
  177. break
  178. }
  179. if in.Value.(*Commit).Committer.When.Equal(commit.Committer.When) {
  180. break
  181. }
  182. if in.Value.(*Commit).Committer.When.After(commit.Committer.When) &&
  183. in.Next().Value.(*Commit).Committer.When.Before(commit.Committer.When) {
  184. break
  185. }
  186. }
  187. in = in.Next()
  188. }
  189. e = l.InsertAfter(commit, in)
  190. }
  191. var pr = parent
  192. if commit.ParentCount() > 1 {
  193. pr = e
  194. }
  195. for i := 0; i < commit.ParentCount(); i++ {
  196. id, err := commit.ParentId(i)
  197. if err != nil {
  198. return err
  199. }
  200. err = repo.commitsBefore(lock, l, pr, id, 0)
  201. if err != nil {
  202. return err
  203. }
  204. }
  205. return nil
  206. }
  207. func (repo *Repository) CommitsCount(commitId string) (int, error) {
  208. id, err := NewIdFromString(commitId)
  209. if err != nil {
  210. return 0, err
  211. }
  212. return repo.commitsCount(id)
  213. }
  214. func (repo *Repository) FileCommitsCount(branch, file string) (int, error) {
  215. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "rev-list", "--count",
  216. branch, "--", file)
  217. if err != nil {
  218. return 0, errors.New(stderr)
  219. }
  220. return com.StrTo(strings.TrimSpace(stdout)).Int()
  221. }
  222. func (repo *Repository) CommitsByFileAndRange(branch, file string, page int) (*list.List, error) {
  223. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", branch,
  224. "--skip="+com.ToStr((page-1)*50), "--max-count=50", prettyLogFormat, "--", file)
  225. if err != nil {
  226. return nil, errors.New(string(stderr))
  227. }
  228. return parsePrettyFormatLog(repo, stdout)
  229. }
  230. func (repo *Repository) getCommitsBefore(id sha1) (*list.List, error) {
  231. l := list.New()
  232. lock := new(sync.Mutex)
  233. err := repo.commitsBefore(lock, l, nil, id, 0)
  234. return l, err
  235. }
  236. func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, error) {
  237. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(), "-100",
  238. "-i", "--grep="+keyword, prettyLogFormat)
  239. if err != nil {
  240. return nil, err
  241. } else if len(stderr) > 0 {
  242. return nil, errors.New(string(stderr))
  243. }
  244. return parsePrettyFormatLog(repo, stdout)
  245. }
  246. var CommitsRangeSize = 50
  247. func (repo *Repository) commitsByRange(id sha1, page int) (*list.List, error) {
  248. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "log", id.String(),
  249. "--skip="+com.ToStr((page-1)*CommitsRangeSize), "--max-count="+com.ToStr(CommitsRangeSize), prettyLogFormat)
  250. if err != nil {
  251. return nil, errors.New(string(stderr))
  252. }
  253. return parsePrettyFormatLog(repo, stdout)
  254. }
  255. func (repo *Repository) getCommitOfRelPath(id sha1, relPath string) (*Commit, error) {
  256. stdout, _, err := com.ExecCmdDir(repo.Path, "git", "log", "-1", prettyLogFormat, id.String(), "--", relPath)
  257. if err != nil {
  258. return nil, err
  259. }
  260. id, err = NewIdFromString(string(stdout))
  261. if err != nil {
  262. return nil, err
  263. }
  264. return repo.getCommit(id)
  265. }