You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

blame.go 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2019 The Gitea 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 repo
  5. import (
  6. "bytes"
  7. "container/list"
  8. "fmt"
  9. "html"
  10. gotemplate "html/template"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/highlight"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/templates"
  19. "code.gitea.io/gitea/modules/timeutil"
  20. )
  21. const (
  22. tplBlame base.TplName = "repo/home"
  23. )
  24. // RefBlame render blame page
  25. func RefBlame(ctx *context.Context) {
  26. fileName := ctx.Repo.TreePath
  27. if len(fileName) == 0 {
  28. ctx.NotFound("Blame FileName", nil)
  29. return
  30. }
  31. userName := ctx.Repo.Owner.Name
  32. repoName := ctx.Repo.Repository.Name
  33. commitID := ctx.Repo.CommitID
  34. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  35. if err != nil {
  36. if git.IsErrNotExist(err) {
  37. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  38. } else {
  39. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  40. }
  41. return
  42. }
  43. if len(commitID) != 40 {
  44. commitID = commit.ID.String()
  45. }
  46. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  47. treeLink := branchLink
  48. rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
  49. if len(ctx.Repo.TreePath) > 0 {
  50. treeLink += "/" + ctx.Repo.TreePath
  51. }
  52. var treeNames []string
  53. paths := make([]string, 0, 5)
  54. if len(ctx.Repo.TreePath) > 0 {
  55. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  56. for i := range treeNames {
  57. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  58. }
  59. ctx.Data["HasParentPath"] = true
  60. if len(paths)-2 >= 0 {
  61. ctx.Data["ParentPath"] = "/" + paths[len(paths)-1]
  62. }
  63. }
  64. // Show latest commit info of repository in table header,
  65. // or of directory if not in root directory.
  66. latestCommit := ctx.Repo.Commit
  67. if len(ctx.Repo.TreePath) > 0 {
  68. latestCommit, err = ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
  69. if err != nil {
  70. ctx.ServerError("GetCommitByPath", err)
  71. return
  72. }
  73. }
  74. ctx.Data["LatestCommit"] = latestCommit
  75. ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit)
  76. ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
  77. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
  78. if err != nil {
  79. log.Error("GetLatestCommitStatus: %v", err)
  80. }
  81. // Get current entry user currently looking at.
  82. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  83. if err != nil {
  84. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  85. return
  86. }
  87. blob := entry.Blob()
  88. ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses)
  89. ctx.Data["Paths"] = paths
  90. ctx.Data["TreeLink"] = treeLink
  91. ctx.Data["TreeNames"] = treeNames
  92. ctx.Data["BranchLink"] = branchLink
  93. ctx.Data["RawFileLink"] = rawLink + "/" + ctx.Repo.TreePath
  94. ctx.Data["PageIsViewCode"] = true
  95. ctx.Data["IsBlame"] = true
  96. ctx.Data["FileSize"] = blob.Size()
  97. ctx.Data["FileName"] = blob.Name()
  98. ctx.Data["NumLines"], err = blob.GetBlobLineCount()
  99. if err != nil {
  100. ctx.NotFound("GetBlobLineCount", err)
  101. return
  102. }
  103. blameReader, err := git.CreateBlameReader(ctx.Req.Context(), models.RepoPath(userName, repoName), commitID, fileName)
  104. if err != nil {
  105. ctx.NotFound("CreateBlameReader", err)
  106. return
  107. }
  108. defer blameReader.Close()
  109. blameParts := make([]git.BlamePart, 0)
  110. for {
  111. blamePart, err := blameReader.NextPart()
  112. if err != nil {
  113. ctx.NotFound("NextPart", err)
  114. return
  115. }
  116. if blamePart == nil {
  117. break
  118. }
  119. blameParts = append(blameParts, *blamePart)
  120. }
  121. commitNames := make(map[string]models.UserCommit)
  122. commits := list.New()
  123. for _, part := range blameParts {
  124. sha := part.Sha
  125. if _, ok := commitNames[sha]; ok {
  126. continue
  127. }
  128. commit, err := ctx.Repo.GitRepo.GetCommit(sha)
  129. if err != nil {
  130. if git.IsErrNotExist(err) {
  131. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  132. } else {
  133. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  134. }
  135. return
  136. }
  137. commits.PushBack(commit)
  138. commitNames[commit.ID.String()] = models.UserCommit{}
  139. }
  140. commits = models.ValidateCommitsWithEmails(commits)
  141. for e := commits.Front(); e != nil; e = e.Next() {
  142. c := e.Value.(models.UserCommit)
  143. commitNames[c.ID.String()] = c
  144. }
  145. // Get Topics of this repo
  146. renderRepoTopics(ctx)
  147. if ctx.Written() {
  148. return
  149. }
  150. renderBlame(ctx, blameParts, commitNames)
  151. ctx.HTML(200, tplBlame)
  152. }
  153. func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]models.UserCommit) {
  154. repoLink := ctx.Repo.RepoLink
  155. var lines = make([]string, 0)
  156. var commitInfo bytes.Buffer
  157. var lineNumbers bytes.Buffer
  158. var codeLines bytes.Buffer
  159. var i = 0
  160. for pi, part := range blameParts {
  161. for index, line := range part.Lines {
  162. i++
  163. lines = append(lines, line)
  164. var attr = ""
  165. if len(part.Lines)-1 == index && len(blameParts)-1 != pi {
  166. attr = " bottom-line"
  167. }
  168. commit := commitNames[part.Sha]
  169. if index == 0 {
  170. // User avatar image
  171. commitSince := timeutil.TimeSinceUnix(timeutil.TimeStamp(commit.Author.When.Unix()), ctx.Data["Lang"].(string))
  172. var avatar string
  173. if commit.User != nil {
  174. avatar = string(templates.Avatar(commit.User, 18, "mr-3"))
  175. } else {
  176. avatar = string(templates.AvatarByEmail(commit.Author.Email, commit.Author.Name, 18, "mr-3"))
  177. }
  178. commitInfo.WriteString(fmt.Sprintf(`<div class="blame-info%s"><div class="blame-data"><div class="blame-avatar">%s</div><div class="blame-message"><a href="%s/commit/%s" title="%[5]s">%[5]s</a></div><div class="blame-time">%s</div></div></div>`, attr, avatar, repoLink, part.Sha, html.EscapeString(commit.CommitMessage), commitSince))
  179. } else {
  180. commitInfo.WriteString(fmt.Sprintf(`<div class="blame-info%s">&#8203;</div>`, attr))
  181. }
  182. //Line number
  183. if len(part.Lines)-1 == index && len(blameParts)-1 != pi {
  184. lineNumbers.WriteString(fmt.Sprintf(`<span id="L%d" data-line-number="%d" class="bottom-line"></span>`, i, i))
  185. } else {
  186. lineNumbers.WriteString(fmt.Sprintf(`<span id="L%d" data-line-number="%d"></span>`, i, i))
  187. }
  188. if i != len(lines)-1 {
  189. line += "\n"
  190. }
  191. fileName := fmt.Sprintf("%v", ctx.Data["FileName"])
  192. line = highlight.Code(fileName, line)
  193. line = `<code class="code-inner">` + line + `</code>`
  194. if len(part.Lines)-1 == index && len(blameParts)-1 != pi {
  195. codeLines.WriteString(fmt.Sprintf(`<li class="L%d bottom-line" rel="L%d">%s</li>`, i, i, line))
  196. } else {
  197. codeLines.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, i, i, line))
  198. }
  199. }
  200. }
  201. ctx.Data["BlameContent"] = gotemplate.HTML(codeLines.String())
  202. ctx.Data["BlameCommitInfo"] = gotemplate.HTML(commitInfo.String())
  203. ctx.Data["BlameLineNums"] = gotemplate.HTML(lineNumbers.String())
  204. }