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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. "path"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  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.QueryInt("page")
  52. if page <= 1 {
  53. page = 1
  54. }
  55. pageSize := ctx.QueryInt("limit")
  56. if pageSize <= 0 {
  57. pageSize = 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. commits = models.ValidateCommitsWithEmails(commits)
  66. commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
  67. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  68. ctx.Data["Commits"] = commits
  69. ctx.Data["Username"] = ctx.Repo.Owner.Name
  70. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  71. ctx.Data["CommitCount"] = commitsCount
  72. ctx.Data["Branch"] = ctx.Repo.BranchName
  73. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  74. pager.SetDefaultParams(ctx)
  75. ctx.Data["Page"] = pager
  76. ctx.HTML(http.StatusOK, tplCommits)
  77. }
  78. // Graph render commit graph - show commits from all branches.
  79. func Graph(ctx *context.Context) {
  80. ctx.Data["Title"] = ctx.Tr("repo.commit_graph")
  81. ctx.Data["PageIsCommits"] = true
  82. ctx.Data["PageIsViewCode"] = true
  83. mode := strings.ToLower(ctx.QueryTrim("mode"))
  84. if mode != "monochrome" {
  85. mode = "color"
  86. }
  87. ctx.Data["Mode"] = mode
  88. hidePRRefs := ctx.QueryBool("hide-pr-refs")
  89. ctx.Data["HidePRRefs"] = hidePRRefs
  90. branches := ctx.QueryStrings("branch")
  91. realBranches := make([]string, len(branches))
  92. copy(realBranches, branches)
  93. for i, branch := range realBranches {
  94. if strings.HasPrefix(branch, "--") {
  95. realBranches[i] = "refs/heads/" + branch
  96. }
  97. }
  98. ctx.Data["SelectedBranches"] = realBranches
  99. files := ctx.QueryStrings("file")
  100. commitsCount, err := ctx.Repo.GetCommitsCount()
  101. if err != nil {
  102. ctx.ServerError("GetCommitsCount", err)
  103. return
  104. }
  105. graphCommitsCount, err := ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files)
  106. if err != nil {
  107. 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)
  108. realBranches = []string{}
  109. branches = []string{}
  110. graphCommitsCount, err = ctx.Repo.GetCommitGraphsCount(hidePRRefs, realBranches, files)
  111. if err != nil {
  112. ctx.ServerError("GetCommitGraphsCount", err)
  113. return
  114. }
  115. }
  116. page := ctx.QueryInt("page")
  117. graph, err := gitgraph.GetCommitGraph(ctx.Repo.GitRepo, page, 0, hidePRRefs, realBranches, files)
  118. if err != nil {
  119. ctx.ServerError("GetCommitGraph", err)
  120. return
  121. }
  122. if err := graph.LoadAndProcessCommits(ctx.Repo.Repository, ctx.Repo.GitRepo); err != nil {
  123. ctx.ServerError("LoadAndProcessCommits", err)
  124. return
  125. }
  126. ctx.Data["Graph"] = graph
  127. gitRefs, err := ctx.Repo.GitRepo.GetRefs()
  128. if err != nil {
  129. ctx.ServerError("GitRepo.GetRefs", err)
  130. return
  131. }
  132. ctx.Data["AllRefs"] = gitRefs
  133. ctx.Data["Username"] = ctx.Repo.Owner.Name
  134. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  135. ctx.Data["CommitCount"] = commitsCount
  136. ctx.Data["Branch"] = ctx.Repo.BranchName
  137. paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
  138. paginator.AddParam(ctx, "mode", "Mode")
  139. paginator.AddParam(ctx, "hide-pr-refs", "HidePRRefs")
  140. for _, branch := range branches {
  141. paginator.AddParamString("branch", branch)
  142. }
  143. for _, file := range files {
  144. paginator.AddParamString("file", file)
  145. }
  146. ctx.Data["Page"] = paginator
  147. if ctx.QueryBool("div-only") {
  148. ctx.HTML(http.StatusOK, tplGraphDiv)
  149. return
  150. }
  151. ctx.HTML(http.StatusOK, tplGraph)
  152. }
  153. // SearchCommits render commits filtered by keyword
  154. func SearchCommits(ctx *context.Context) {
  155. ctx.Data["PageIsCommits"] = true
  156. ctx.Data["PageIsViewCode"] = true
  157. query := strings.Trim(ctx.Query("q"), " ")
  158. if len(query) == 0 {
  159. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  160. return
  161. }
  162. all := ctx.QueryBool("all")
  163. opts := git.NewSearchCommitsOptions(query, all)
  164. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  165. if err != nil {
  166. ctx.ServerError("SearchCommits", err)
  167. return
  168. }
  169. commits = models.ValidateCommitsWithEmails(commits)
  170. commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
  171. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  172. ctx.Data["Commits"] = commits
  173. ctx.Data["Keyword"] = query
  174. if all {
  175. ctx.Data["All"] = "checked"
  176. }
  177. ctx.Data["Username"] = ctx.Repo.Owner.Name
  178. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  179. ctx.Data["CommitCount"] = commits.Len()
  180. ctx.Data["Branch"] = ctx.Repo.BranchName
  181. ctx.HTML(http.StatusOK, tplCommits)
  182. }
  183. // FileHistory show a file's reversions
  184. func FileHistory(ctx *context.Context) {
  185. ctx.Data["IsRepoToolbarCommits"] = true
  186. fileName := ctx.Repo.TreePath
  187. if len(fileName) == 0 {
  188. Commits(ctx)
  189. return
  190. }
  191. branchName := ctx.Repo.BranchName
  192. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  193. if err != nil {
  194. ctx.ServerError("FileCommitsCount", err)
  195. return
  196. } else if commitsCount == 0 {
  197. ctx.NotFound("FileCommitsCount", nil)
  198. return
  199. }
  200. page := ctx.QueryInt("page")
  201. if page <= 1 {
  202. page = 1
  203. }
  204. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  205. if err != nil {
  206. ctx.ServerError("CommitsByFileAndRange", err)
  207. return
  208. }
  209. commits = models.ValidateCommitsWithEmails(commits)
  210. commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
  211. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  212. ctx.Data["Commits"] = commits
  213. ctx.Data["Username"] = ctx.Repo.Owner.Name
  214. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  215. ctx.Data["FileName"] = fileName
  216. ctx.Data["CommitCount"] = commitsCount
  217. ctx.Data["Branch"] = branchName
  218. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  219. pager.SetDefaultParams(ctx)
  220. ctx.Data["Page"] = pager
  221. ctx.HTML(http.StatusOK, tplCommits)
  222. }
  223. // Diff show different from current commit to previous commit
  224. func Diff(ctx *context.Context) {
  225. ctx.Data["PageIsDiff"] = true
  226. ctx.Data["RequireHighlightJS"] = true
  227. ctx.Data["RequireSimpleMDE"] = true
  228. ctx.Data["RequireTribute"] = true
  229. userName := ctx.Repo.Owner.Name
  230. repoName := ctx.Repo.Repository.Name
  231. commitID := ctx.Params(":sha")
  232. var (
  233. gitRepo *git.Repository
  234. err error
  235. repoPath string
  236. )
  237. if ctx.Data["PageIsWiki"] != nil {
  238. gitRepo, err = git.OpenRepository(ctx.Repo.Repository.WikiPath())
  239. if err != nil {
  240. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  241. return
  242. }
  243. repoPath = ctx.Repo.Repository.WikiPath()
  244. } else {
  245. gitRepo = ctx.Repo.GitRepo
  246. repoPath = models.RepoPath(userName, repoName)
  247. }
  248. commit, err := gitRepo.GetCommit(commitID)
  249. if err != nil {
  250. if git.IsErrNotExist(err) {
  251. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  252. } else {
  253. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  254. }
  255. return
  256. }
  257. if len(commitID) != 40 {
  258. commitID = commit.ID.String()
  259. }
  260. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, models.ListOptions{})
  261. if err != nil {
  262. log.Error("GetLatestCommitStatus: %v", err)
  263. }
  264. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  265. ctx.Data["CommitStatuses"] = statuses
  266. diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath,
  267. commitID, setting.Git.MaxGitDiffLines,
  268. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
  269. gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
  270. if err != nil {
  271. ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err)
  272. return
  273. }
  274. parents := make([]string, commit.ParentCount())
  275. for i := 0; i < commit.ParentCount(); i++ {
  276. sha, err := commit.ParentID(i)
  277. if err != nil {
  278. ctx.NotFound("repo.Diff", err)
  279. return
  280. }
  281. parents[i] = sha.String()
  282. }
  283. ctx.Data["CommitID"] = commitID
  284. ctx.Data["AfterCommitID"] = commitID
  285. ctx.Data["Username"] = userName
  286. ctx.Data["Reponame"] = repoName
  287. var parentCommit *git.Commit
  288. if commit.ParentCount() > 0 {
  289. parentCommit, err = gitRepo.GetCommit(parents[0])
  290. if err != nil {
  291. ctx.NotFound("GetParentCommit", err)
  292. return
  293. }
  294. }
  295. headTarget := path.Join(userName, repoName)
  296. setCompareContext(ctx, parentCommit, commit, headTarget)
  297. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  298. ctx.Data["Commit"] = commit
  299. verification := models.ParseCommitWithSignature(commit)
  300. ctx.Data["Verification"] = verification
  301. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  302. ctx.Data["Diff"] = diff
  303. ctx.Data["Parents"] = parents
  304. ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
  305. if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil {
  306. ctx.ServerError("CalculateTrustStatus", err)
  307. return
  308. }
  309. note := &git.Note{}
  310. err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
  311. if err == nil {
  312. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  313. ctx.Data["NoteCommit"] = note.Commit
  314. ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
  315. }
  316. ctx.Data["BranchName"], err = commit.GetBranchName()
  317. if err != nil {
  318. ctx.ServerError("commit.GetBranchName", err)
  319. return
  320. }
  321. ctx.Data["TagName"], err = commit.GetTagName()
  322. if err != nil {
  323. ctx.ServerError("commit.GetTagName", err)
  324. return
  325. }
  326. ctx.HTML(http.StatusOK, tplCommitPage)
  327. }
  328. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  329. func RawDiff(ctx *context.Context) {
  330. var repoPath string
  331. if ctx.Data["PageIsWiki"] != nil {
  332. repoPath = ctx.Repo.Repository.WikiPath()
  333. } else {
  334. repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  335. }
  336. if err := git.GetRawDiff(
  337. repoPath,
  338. ctx.Params(":sha"),
  339. git.RawDiffType(ctx.Params(":ext")),
  340. ctx.Resp,
  341. ); err != nil {
  342. if git.IsErrNotExist(err) {
  343. ctx.NotFound("GetRawDiff",
  344. errors.New("commit "+ctx.Params(":sha")+" does not exist."))
  345. return
  346. }
  347. ctx.ServerError("GetRawDiff", err)
  348. return
  349. }
  350. }