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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "errors"
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. asymkey_model "code.gitea.io/gitea/models/asymkey"
  11. "code.gitea.io/gitea/models/db"
  12. git_model "code.gitea.io/gitea/models/git"
  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"] = git_model.ConvertFromGitCommit(ctx, 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), pageSize, 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(ctx, 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(ctx, 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, 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"] = git_model.ConvertFromGitCommit(ctx, 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(
  201. git.CommitsByFileAndRangeOptions{
  202. Revision: ctx.Repo.RefName,
  203. File: fileName,
  204. Page: page,
  205. })
  206. if err != nil {
  207. ctx.ServerError("CommitsByFileAndRange", err)
  208. return
  209. }
  210. ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commits, ctx.Repo.Repository)
  211. ctx.Data["Username"] = ctx.Repo.Owner.Name
  212. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  213. ctx.Data["FileName"] = fileName
  214. ctx.Data["CommitCount"] = commitsCount
  215. ctx.Data["RefName"] = ctx.Repo.RefName
  216. pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
  217. pager.SetDefaultParams(ctx)
  218. ctx.Data["Page"] = pager
  219. ctx.HTML(http.StatusOK, tplCommits)
  220. }
  221. // Diff show different from current commit to previous commit
  222. func Diff(ctx *context.Context) {
  223. ctx.Data["PageIsDiff"] = true
  224. userName := ctx.Repo.Owner.Name
  225. repoName := ctx.Repo.Repository.Name
  226. commitID := ctx.Params(":sha")
  227. var (
  228. gitRepo *git.Repository
  229. err error
  230. )
  231. if ctx.Data["PageIsWiki"] != nil {
  232. gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
  233. if err != nil {
  234. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  235. return
  236. }
  237. defer gitRepo.Close()
  238. } else {
  239. gitRepo = ctx.Repo.GitRepo
  240. }
  241. commit, err := gitRepo.GetCommit(commitID)
  242. if err != nil {
  243. if git.IsErrNotExist(err) {
  244. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  245. } else {
  246. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  247. }
  248. return
  249. }
  250. if len(commitID) != git.SHAFullLength {
  251. commitID = commit.ID.String()
  252. }
  253. fileOnly := ctx.FormBool("file-only")
  254. maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
  255. files := ctx.FormStrings("files")
  256. if fileOnly && (len(files) == 2 || len(files) == 1) {
  257. maxLines, maxFiles = -1, -1
  258. }
  259. diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
  260. AfterCommitID: commitID,
  261. SkipTo: ctx.FormString("skip-to"),
  262. MaxLines: maxLines,
  263. MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
  264. MaxFiles: maxFiles,
  265. WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
  266. }, files...)
  267. if err != nil {
  268. ctx.NotFound("GetDiff", err)
  269. return
  270. }
  271. parents := make([]string, commit.ParentCount())
  272. for i := 0; i < commit.ParentCount(); i++ {
  273. sha, err := commit.ParentID(i)
  274. if err != nil {
  275. ctx.NotFound("repo.Diff", err)
  276. return
  277. }
  278. parents[i] = sha.String()
  279. }
  280. ctx.Data["CommitID"] = commitID
  281. ctx.Data["AfterCommitID"] = commitID
  282. ctx.Data["Username"] = userName
  283. ctx.Data["Reponame"] = repoName
  284. var parentCommit *git.Commit
  285. if commit.ParentCount() > 0 {
  286. parentCommit, err = gitRepo.GetCommit(parents[0])
  287. if err != nil {
  288. ctx.NotFound("GetParentCommit", err)
  289. return
  290. }
  291. }
  292. setCompareContext(ctx, parentCommit, commit, userName, repoName)
  293. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  294. ctx.Data["Commit"] = commit
  295. ctx.Data["Diff"] = diff
  296. statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, commitID, db.ListOptions{ListAll: true})
  297. if err != nil {
  298. log.Error("GetLatestCommitStatus: %v", err)
  299. }
  300. ctx.Data["CommitStatus"] = git_model.CalcCommitStatus(statuses)
  301. ctx.Data["CommitStatuses"] = statuses
  302. verification := asymkey_model.ParseCommitWithSignature(ctx, commit)
  303. ctx.Data["Verification"] = verification
  304. ctx.Data["Author"] = user_model.ValidateCommitWithEmail(ctx, commit)
  305. ctx.Data["Parents"] = parents
  306. ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
  307. if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
  308. return repo_model.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID)
  309. }, nil); err != nil {
  310. ctx.ServerError("CalculateTrustStatus", err)
  311. return
  312. }
  313. note := &git.Note{}
  314. err = git.GetNote(ctx, ctx.Repo.GitRepo, commitID, note)
  315. if err == nil {
  316. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  317. ctx.Data["NoteCommit"] = note.Commit
  318. ctx.Data["NoteAuthor"] = user_model.ValidateCommitWithEmail(ctx, note.Commit)
  319. }
  320. ctx.Data["BranchName"], err = commit.GetBranchName()
  321. if err != nil {
  322. ctx.ServerError("commit.GetBranchName", err)
  323. return
  324. }
  325. ctx.Data["TagName"], err = commit.GetTagName()
  326. if err != nil {
  327. ctx.ServerError("commit.GetTagName", err)
  328. return
  329. }
  330. ctx.HTML(http.StatusOK, tplCommitPage)
  331. }
  332. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  333. func RawDiff(ctx *context.Context) {
  334. var gitRepo *git.Repository
  335. if ctx.Data["PageIsWiki"] != nil {
  336. wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
  337. if err != nil {
  338. ctx.ServerError("OpenRepository", err)
  339. return
  340. }
  341. defer wikiRepo.Close()
  342. gitRepo = wikiRepo
  343. } else {
  344. gitRepo = ctx.Repo.GitRepo
  345. if gitRepo == nil {
  346. ctx.ServerError("GitRepo not open", fmt.Errorf("no open git repo for '%s'", ctx.Repo.Repository.FullName()))
  347. return
  348. }
  349. }
  350. if err := git.GetRawDiff(
  351. gitRepo,
  352. ctx.Params(":sha"),
  353. git.RawDiffType(ctx.Params(":ext")),
  354. ctx.Resp,
  355. ); err != nil {
  356. if git.IsErrNotExist(err) {
  357. ctx.NotFound("GetRawDiff",
  358. errors.New("commit "+ctx.Params(":sha")+" does not exist."))
  359. return
  360. }
  361. ctx.ServerError("GetRawDiff", err)
  362. return
  363. }
  364. }