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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. asymkey_model "code.gitea.io/gitea/models/asymkey"
  12. "code.gitea.io/gitea/models/db"
  13. repo_model "code.gitea.io/gitea/models/repo"
  14. user_model "code.gitea.io/gitea/models/user"
  15. "code.gitea.io/gitea/modules/base"
  16. "code.gitea.io/gitea/modules/charset"
  17. "code.gitea.io/gitea/modules/context"
  18. "code.gitea.io/gitea/modules/git"
  19. "code.gitea.io/gitea/modules/gitgraph"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/setting"
  22. "code.gitea.io/gitea/services/gitdiff"
  23. )
  24. const (
  25. tplCommits base.TplName = "repo/commits"
  26. tplGraph base.TplName = "repo/graph"
  27. tplGraphDiv base.TplName = "repo/graph/div"
  28. tplCommitPage base.TplName = "repo/commit_page"
  29. )
  30. // RefCommits render commits page
  31. func RefCommits(ctx *context.Context) {
  32. switch {
  33. case len(ctx.Repo.TreePath) == 0:
  34. Commits(ctx)
  35. case ctx.Repo.TreePath == "search":
  36. SearchCommits(ctx)
  37. default:
  38. FileHistory(ctx)
  39. }
  40. }
  41. // Commits render branch's commits
  42. func Commits(ctx *context.Context) {
  43. ctx.Data["PageIsCommits"] = true
  44. if ctx.Repo.Commit == nil {
  45. ctx.NotFound("Commit not found", nil)
  46. return
  47. }
  48. ctx.Data["PageIsViewCode"] = true
  49. commitsCount, err := ctx.Repo.GetCommitsCount()
  50. if err != nil {
  51. ctx.ServerError("GetCommitsCount", err)
  52. return
  53. }
  54. page := ctx.FormInt("page")
  55. if page <= 1 {
  56. page = 1
  57. }
  58. pageSize := ctx.FormInt("limit")
  59. if pageSize <= 0 {
  60. pageSize = setting.Git.CommitsRangeSize
  61. }
  62. // Both `git log branchName` and `git log commitId` work.
  63. commits, err := ctx.Repo.Commit.CommitsByRange(page, pageSize)
  64. if err != nil {
  65. ctx.ServerError("CommitsByRange", err)
  66. return
  67. }
  68. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  69. ctx.Data["Username"] = ctx.Repo.Owner.Name
  70. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  71. ctx.Data["CommitCount"] = commitsCount
  72. ctx.Data["RefName"] = ctx.Repo.RefName
  73. pager := context.NewPagination(int(commitsCount), setting.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.FormTrim("mode"))
  84. if mode != "monochrome" {
  85. mode = "color"
  86. }
  87. ctx.Data["Mode"] = mode
  88. hidePRRefs := ctx.FormBool("hide-pr-refs")
  89. ctx.Data["HidePRRefs"] = hidePRRefs
  90. branches := ctx.FormStrings("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] = git.BranchPrefix + branch
  96. }
  97. }
  98. ctx.Data["SelectedBranches"] = realBranches
  99. files := ctx.FormStrings("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.FormInt("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["RefName"] = ctx.Repo.RefName
  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.FormBool("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 := ctx.FormTrim("q")
  158. if len(query) == 0 {
  159. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  160. return
  161. }
  162. all := ctx.FormBool("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. ctx.Data["CommitCount"] = len(commits)
  170. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  171. ctx.Data["Keyword"] = query
  172. if all {
  173. ctx.Data["All"] = "checked"
  174. }
  175. ctx.Data["Username"] = ctx.Repo.Owner.Name
  176. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  177. ctx.Data["RefName"] = ctx.Repo.RefName
  178. ctx.HTML(http.StatusOK, tplCommits)
  179. }
  180. // FileHistory show a file's reversions
  181. func FileHistory(ctx *context.Context) {
  182. ctx.Data["IsRepoToolbarCommits"] = true
  183. fileName := ctx.Repo.TreePath
  184. if len(fileName) == 0 {
  185. Commits(ctx)
  186. return
  187. }
  188. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName)
  189. if err != nil {
  190. ctx.ServerError("FileCommitsCount", err)
  191. return
  192. } else if commitsCount == 0 {
  193. ctx.NotFound("FileCommitsCount", nil)
  194. return
  195. }
  196. page := ctx.FormInt("page")
  197. if page <= 1 {
  198. page = 1
  199. }
  200. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx.Repo.RefName, fileName, page)
  201. if err != nil {
  202. ctx.ServerError("CommitsByFileAndRange", err)
  203. return
  204. }
  205. ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
  206. ctx.Data["Username"] = ctx.Repo.Owner.Name
  207. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  208. ctx.Data["FileName"] = fileName
  209. ctx.Data["CommitCount"] = commitsCount
  210. ctx.Data["RefName"] = ctx.Repo.RefName
  211. pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
  212. pager.SetDefaultParams(ctx)
  213. ctx.Data["Page"] = pager
  214. ctx.HTML(http.StatusOK, tplCommits)
  215. }
  216. // Diff show different from current commit to previous commit
  217. func Diff(ctx *context.Context) {
  218. ctx.Data["PageIsDiff"] = true
  219. ctx.Data["RequireHighlightJS"] = true
  220. ctx.Data["RequireEasyMDE"] = true
  221. ctx.Data["RequireTribute"] = true
  222. userName := ctx.Repo.Owner.Name
  223. repoName := ctx.Repo.Repository.Name
  224. commitID := ctx.Params(":sha")
  225. var (
  226. gitRepo *git.Repository
  227. err error
  228. )
  229. if ctx.Data["PageIsWiki"] != nil {
  230. gitRepo, err = git.OpenRepository(ctx.Repo.Repository.WikiPath())
  231. if err != nil {
  232. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  233. return
  234. }
  235. defer gitRepo.Close()
  236. } else {
  237. gitRepo = ctx.Repo.GitRepo
  238. }
  239. commit, err := gitRepo.GetCommit(commitID)
  240. if err != nil {
  241. if git.IsErrNotExist(err) {
  242. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  243. } else {
  244. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  245. }
  246. return
  247. }
  248. if len(commitID) != 40 {
  249. commitID = commit.ID.String()
  250. }
  251. fileOnly := ctx.FormBool("file-only")
  252. maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
  253. files := ctx.FormStrings("files")
  254. if fileOnly && (len(files) == 2 || len(files) == 1) {
  255. maxLines, maxFiles = -1, -1
  256. }
  257. diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
  258. AfterCommitID: commitID,
  259. SkipTo: ctx.FormString("skip-to"),
  260. MaxLines: maxLines,
  261. MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
  262. MaxFiles: maxFiles,
  263. WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
  264. }, files...)
  265. if err != nil {
  266. ctx.NotFound("GetDiff", err)
  267. return
  268. }
  269. parents := make([]string, commit.ParentCount())
  270. for i := 0; i < commit.ParentCount(); i++ {
  271. sha, err := commit.ParentID(i)
  272. if err != nil {
  273. ctx.NotFound("repo.Diff", err)
  274. return
  275. }
  276. parents[i] = sha.String()
  277. }
  278. ctx.Data["CommitID"] = commitID
  279. ctx.Data["AfterCommitID"] = commitID
  280. ctx.Data["Username"] = userName
  281. ctx.Data["Reponame"] = repoName
  282. var parentCommit *git.Commit
  283. if commit.ParentCount() > 0 {
  284. parentCommit, err = gitRepo.GetCommit(parents[0])
  285. if err != nil {
  286. ctx.NotFound("GetParentCommit", err)
  287. return
  288. }
  289. }
  290. setCompareContext(ctx, parentCommit, commit, userName, repoName)
  291. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  292. ctx.Data["Commit"] = commit
  293. ctx.Data["Diff"] = diff
  294. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, db.ListOptions{})
  295. if err != nil {
  296. log.Error("GetLatestCommitStatus: %v", err)
  297. }
  298. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  299. ctx.Data["CommitStatuses"] = statuses
  300. verification := asymkey_model.ParseCommitWithSignature(commit)
  301. ctx.Data["Verification"] = verification
  302. ctx.Data["Author"] = user_model.ValidateCommitWithEmail(commit)
  303. ctx.Data["Parents"] = parents
  304. ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
  305. if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
  306. return models.IsUserRepoAdmin(ctx.Repo.Repository, user)
  307. }, nil); err != nil {
  308. ctx.ServerError("CalculateTrustStatus", err)
  309. return
  310. }
  311. note := &git.Note{}
  312. err = git.GetNote(ctx, ctx.Repo.GitRepo, commitID, note)
  313. if err == nil {
  314. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  315. ctx.Data["NoteCommit"] = note.Commit
  316. ctx.Data["NoteAuthor"] = user_model.ValidateCommitWithEmail(note.Commit)
  317. }
  318. ctx.Data["BranchName"], err = commit.GetBranchName()
  319. if err != nil {
  320. ctx.ServerError("commit.GetBranchName", err)
  321. return
  322. }
  323. ctx.Data["TagName"], err = commit.GetTagName()
  324. if err != nil {
  325. ctx.ServerError("commit.GetTagName", err)
  326. return
  327. }
  328. ctx.HTML(http.StatusOK, tplCommitPage)
  329. }
  330. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  331. func RawDiff(ctx *context.Context) {
  332. var repoPath string
  333. if ctx.Data["PageIsWiki"] != nil {
  334. repoPath = ctx.Repo.Repository.WikiPath()
  335. } else {
  336. repoPath = repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  337. }
  338. if err := git.GetRawDiff(
  339. repoPath,
  340. ctx.Params(":sha"),
  341. git.RawDiffType(ctx.Params(":ext")),
  342. ctx.Resp,
  343. ); err != nil {
  344. if git.IsErrNotExist(err) {
  345. ctx.NotFound("GetRawDiff",
  346. errors.New("commit "+ctx.Params(":sha")+" does not exist."))
  347. return
  348. }
  349. ctx.ServerError("GetRawDiff", err)
  350. return
  351. }
  352. }