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.

commit.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "errors"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/db"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/charset"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/gitgraph"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/services/gitdiff"
  20. )
  21. const (
  22. tplCommits base.TplName = "repo/commits"
  23. tplGraph base.TplName = "repo/graph"
  24. tplGraphDiv base.TplName = "repo/graph/div"
  25. tplCommitPage base.TplName = "repo/commit_page"
  26. )
  27. // RefCommits render commits page
  28. func RefCommits(ctx *context.Context) {
  29. switch {
  30. case len(ctx.Repo.TreePath) == 0:
  31. Commits(ctx)
  32. case ctx.Repo.TreePath == "search":
  33. SearchCommits(ctx)
  34. default:
  35. FileHistory(ctx)
  36. }
  37. }
  38. // Commits render branch's commits
  39. func Commits(ctx *context.Context) {
  40. ctx.Data["PageIsCommits"] = true
  41. if ctx.Repo.Commit == nil {
  42. ctx.NotFound("Commit not found", nil)
  43. return
  44. }
  45. ctx.Data["PageIsViewCode"] = true
  46. commitsCount, err := ctx.Repo.GetCommitsCount()
  47. if err != nil {
  48. ctx.ServerError("GetCommitsCount", err)
  49. return
  50. }
  51. page := ctx.FormInt("page")
  52. if page <= 1 {
  53. page = 1
  54. }
  55. pageSize := ctx.FormInt("limit")
  56. if pageSize <= 0 {
  57. pageSize = setting.Git.CommitsRangeSize
  58. }
  59. // Both `git log branchName` and `git log commitId` work.
  60. commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize)
  61. if err != nil {
  62. ctx.ServerError("CommitsByRange", err)
  63. return
  64. }
  65. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  66. ctx.Data["Username"] = ctx.Repo.Owner.Name
  67. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  68. ctx.Data["CommitCount"] = commitsCount
  69. ctx.Data["RefName"] = ctx.Repo.RefName
  70. pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
  71. pager.SetDefaultParams(ctx)
  72. ctx.Data["Page"] = pager
  73. ctx.HTML(http.StatusOK, tplCommits)
  74. }
  75. // Graph render commit graph - show commits from all branches.
  76. func Graph(ctx *context.Context) {
  77. ctx.Data["Title"] = ctx.Tr("repo.commit_graph")
  78. ctx.Data["PageIsCommits"] = true
  79. ctx.Data["PageIsViewCode"] = true
  80. mode := strings.ToLower(ctx.FormTrim("mode"))
  81. if mode != "monochrome" {
  82. mode = "color"
  83. }
  84. ctx.Data["Mode"] = mode
  85. hidePRRefs := ctx.FormBool("hide-pr-refs")
  86. ctx.Data["HidePRRefs"] = hidePRRefs
  87. branches := ctx.FormStrings("branch")
  88. realBranches := make([]string, len(branches))
  89. copy(realBranches, branches)
  90. for i, branch := range realBranches {
  91. if strings.HasPrefix(branch, "--") {
  92. realBranches[i] = "refs/heads/" + branch
  93. }
  94. }
  95. ctx.Data["SelectedBranches"] = realBranches
  96. files := ctx.FormStrings("file")
  97. commitsCount, err := ctx.Repo.GetCommitsCount()
  98. if err != nil {
  99. ctx.ServerError("GetCommitsCount", err)
  100. return
  101. }
  102. graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files)
  103. if err != nil {
  104. log.Warn("GetCommitGraphsCount error for generate graph exclude prs: %t branches: %s in %-v, Will Ignore branches and try again. Underlying Error: %v", hidePRRefs, branches, ctx.Repo.Repository, err)
  105. realBranches = []string{}
  106. branches = []string{}
  107. graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files)
  108. if err != nil {
  109. ctx.ServerError("GetCommitGraphsCount", err)
  110. return
  111. }
  112. }
  113. page := ctx.FormInt("page")
  114. graph, err := gitgraph.GetCommitGraph(ctx.Repo.GitRepo, page, 0, hidePRRefs, realBranches, files)
  115. if err != nil {
  116. ctx.ServerError("GetCommitGraph", err)
  117. return
  118. }
  119. if err := graph.LoadAndProcessCommits(ctx.Repo.Repository, ctx.Repo.GitRepo); err != nil {
  120. ctx.ServerError("LoadAndProcessCommits", err)
  121. return
  122. }
  123. ctx.Data["Graph"] = graph
  124. gitRefs, err := ctx.Repo.GitRepo.GetRefs()
  125. if err != nil {
  126. ctx.ServerError("GitRepo.GetRefs", err)
  127. return
  128. }
  129. ctx.Data["AllRefs"] = gitRefs
  130. ctx.Data["Username"] = ctx.Repo.Owner.Name
  131. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  132. ctx.Data["CommitCount"] = commitsCount
  133. ctx.Data["RefName"] = ctx.Repo.RefName
  134. paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
  135. paginator.AddParam(ctx, "mode", "Mode")
  136. paginator.AddParam(ctx, "hide-pr-refs", "HidePRRefs")
  137. for _, branch := range branches {
  138. paginator.AddParamString("branch", branch)
  139. }
  140. for _, file := range files {
  141. paginator.AddParamString("file", file)
  142. }
  143. ctx.Data["Page"] = paginator
  144. if ctx.FormBool("div-only") {
  145. ctx.HTML(http.StatusOK, tplGraphDiv)
  146. return
  147. }
  148. ctx.HTML(http.StatusOK, tplGraph)
  149. }
  150. // SearchCommits render commits filtered by keyword
  151. func SearchCommits(ctx *context.Context) {
  152. ctx.Data["PageIsCommits"] = true
  153. ctx.Data["PageIsViewCode"] = true
  154. query := ctx.FormTrim("q")
  155. if len(query) == 0 {
  156. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  157. return
  158. }
  159. all := ctx.FormBool("all")
  160. opts := git.NewSearchCommitsOptions(query, all)
  161. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  162. if err != nil {
  163. ctx.ServerError("SearchCommits", err)
  164. return
  165. }
  166. ctx.Data["CommitCount"] = len(commits)
  167. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  168. ctx.Data["Keyword"] = query
  169. if all {
  170. ctx.Data["All"] = "checked"
  171. }
  172. ctx.Data["Username"] = ctx.Repo.Owner.Name
  173. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  174. ctx.Data["RefName"] = ctx.Repo.RefName
  175. ctx.HTML(http.StatusOK, tplCommits)
  176. }
  177. // FileHistory show a file's reversions
  178. func FileHistory(ctx *context.Context) {
  179. ctx.Data["IsRepoToolbarCommits"] = true
  180. fileName := ctx.Repo.TreePath
  181. if len(fileName) == 0 {
  182. Commits(ctx)
  183. return
  184. }
  185. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName)
  186. if err != nil {
  187. ctx.ServerError("FileCommitsCount", err)
  188. return
  189. } else if commitsCount == 0 {
  190. ctx.NotFound("FileCommitsCount", nil)
  191. return
  192. }
  193. page := ctx.FormInt("page")
  194. if page <= 1 {
  195. page = 1
  196. }
  197. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx.Repo.RefName, fileName, page)
  198. if err != nil {
  199. ctx.ServerError("CommitsByFileAndRange", err)
  200. return
  201. }
  202. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  203. ctx.Data["Username"] = ctx.Repo.Owner.Name
  204. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  205. ctx.Data["FileName"] = fileName
  206. ctx.Data["CommitCount"] = commitsCount
  207. ctx.Data["RefName"] = ctx.Repo.RefName
  208. pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
  209. pager.SetDefaultParams(ctx)
  210. ctx.Data["Page"] = pager
  211. ctx.HTML(http.StatusOK, tplCommits)
  212. }
  213. // Diff show different from current commit to previous commit
  214. func Diff(ctx *context.Context) {
  215. ctx.Data["PageIsDiff"] = true
  216. ctx.Data["RequireHighlightJS"] = true
  217. ctx.Data["RequireSimpleMDE"] = true
  218. ctx.Data["RequireTribute"] = true
  219. userName := ctx.Repo.Owner.Name
  220. repoName := ctx.Repo.Repository.Name
  221. commitID := ctx.Params(":sha")
  222. var (
  223. gitRepo *git.Repository
  224. err error
  225. )
  226. if ctx.Data["PageIsWiki"] != nil {
  227. gitRepo, err = git.OpenRepository(ctx.Repo.Repository.WikiPath())
  228. if err != nil {
  229. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  230. return
  231. }
  232. defer gitRepo.Close()
  233. } else {
  234. gitRepo = ctx.Repo.GitRepo
  235. }
  236. commit, err := gitRepo.GetCommit(commitID)
  237. if err != nil {
  238. if git.IsErrNotExist(err) {
  239. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  240. } else {
  241. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  242. }
  243. return
  244. }
  245. if len(commitID) != 40 {
  246. commitID = commit.ID.String()
  247. }
  248. fileOnly := ctx.FormBool("file-only")
  249. maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
  250. files := ctx.FormStrings("files")
  251. if fileOnly && (len(files) == 2 || len(files) == 1) {
  252. maxLines, maxFiles = -1, -1
  253. }
  254. diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
  255. AfterCommitID: commitID,
  256. SkipTo: ctx.FormString("skip-to"),
  257. MaxLines: maxLines,
  258. MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
  259. MaxFiles: maxFiles,
  260. WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
  261. }, files...)
  262. if err != nil {
  263. ctx.NotFound("GetDiff", err)
  264. return
  265. }
  266. parents := make([]string, commit.ParentCount())
  267. for i := 0; i < commit.ParentCount(); i++ {
  268. sha, err := commit.ParentID(i)
  269. if err != nil {
  270. ctx.NotFound("repo.Diff", err)
  271. return
  272. }
  273. parents[i] = sha.String()
  274. }
  275. ctx.Data["CommitID"] = commitID
  276. ctx.Data["AfterCommitID"] = commitID
  277. ctx.Data["Username"] = userName
  278. ctx.Data["Reponame"] = repoName
  279. var parentCommit *git.Commit
  280. if commit.ParentCount() > 0 {
  281. parentCommit, err = gitRepo.GetCommit(parents[0])
  282. if err != nil {
  283. ctx.NotFound("GetParentCommit", err)
  284. return
  285. }
  286. }
  287. setCompareContext(ctx, parentCommit, commit, userName, repoName)
  288. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  289. ctx.Data["Commit"] = commit
  290. ctx.Data["Diff"] = diff
  291. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, db.ListOptions{})
  292. if err != nil {
  293. log.Error("GetLatestCommitStatus: %v", err)
  294. }
  295. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  296. ctx.Data["CommitStatuses"] = statuses
  297. verification := models.ParseCommitWithSignature(commit)
  298. ctx.Data["Verification"] = verification
  299. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  300. ctx.Data["Parents"] = parents
  301. ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
  302. if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil {
  303. ctx.ServerError("CalculateTrustStatus", err)
  304. return
  305. }
  306. note := &git.Note{}
  307. err = git.GetNote(ctx, ctx.Repo.GitRepo, commitID, note)
  308. if err == nil {
  309. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  310. ctx.Data["NoteCommit"] = note.Commit
  311. ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
  312. }
  313. ctx.Data["BranchName"], err = commit.GetBranchName()
  314. if err != nil {
  315. ctx.ServerError("commit.GetBranchName", err)
  316. return
  317. }
  318. ctx.Data["TagName"], err = commit.GetTagName()
  319. if err != nil {
  320. ctx.ServerError("commit.GetTagName", err)
  321. return
  322. }
  323. ctx.HTML(http.StatusOK, tplCommitPage)
  324. }
  325. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  326. func RawDiff(ctx *context.Context) {
  327. var repoPath string
  328. if ctx.Data["PageIsWiki"] != nil {
  329. repoPath = ctx.Repo.Repository.WikiPath()
  330. } else {
  331. repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  332. }
  333. if err := git.GetRawDiff(
  334. repoPath,
  335. ctx.Params(":sha"),
  336. git.RawDiffType(ctx.Params(":ext")),
  337. ctx.Resp,
  338. ); err != nil {
  339. if git.IsErrNotExist(err) {
  340. ctx.NotFound("GetRawDiff",
  341. errors.New("commit "+ctx.Params(":sha")+" does not exist."))
  342. return
  343. }
  344. ctx.ServerError("GetRawDiff", err)
  345. return
  346. }
  347. }