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.

commit.go 9.5 kB

11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
10 years ago
10 years ago
10 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. "path"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const (
  17. tplCommits base.TplName = "repo/commits"
  18. tplGraph base.TplName = "repo/graph"
  19. tplDiff base.TplName = "repo/diff/page"
  20. )
  21. // RefCommits render commits page
  22. func RefCommits(ctx *context.Context) {
  23. switch {
  24. case len(ctx.Repo.TreePath) == 0:
  25. Commits(ctx)
  26. case ctx.Repo.TreePath == "search":
  27. SearchCommits(ctx)
  28. default:
  29. FileHistory(ctx)
  30. }
  31. }
  32. // Commits render branch's commits
  33. func Commits(ctx *context.Context) {
  34. ctx.Data["PageIsCommits"] = true
  35. if ctx.Repo.Commit == nil {
  36. ctx.NotFound("Commit not found", nil)
  37. return
  38. }
  39. ctx.Data["PageIsViewCode"] = true
  40. commitsCount, err := ctx.Repo.GetCommitsCount()
  41. if err != nil {
  42. ctx.ServerError("GetCommitsCount", err)
  43. return
  44. }
  45. page := ctx.QueryInt("page")
  46. if page <= 1 {
  47. page = 1
  48. }
  49. // Both `git log branchName` and `git log commitId` work.
  50. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  51. if err != nil {
  52. ctx.ServerError("CommitsByRange", err)
  53. return
  54. }
  55. commits = models.ValidateCommitsWithEmails(commits)
  56. commits = models.ParseCommitsWithSignature(commits)
  57. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  58. ctx.Data["Commits"] = commits
  59. ctx.Data["Username"] = ctx.Repo.Owner.Name
  60. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  61. ctx.Data["CommitCount"] = commitsCount
  62. ctx.Data["Branch"] = ctx.Repo.BranchName
  63. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  64. pager.SetDefaultParams(ctx)
  65. ctx.Data["Page"] = pager
  66. ctx.HTML(200, tplCommits)
  67. }
  68. // Graph render commit graph - show commits from all branches.
  69. func Graph(ctx *context.Context) {
  70. ctx.Data["PageIsCommits"] = true
  71. ctx.Data["PageIsViewCode"] = true
  72. commitsCount, err := ctx.Repo.GetCommitsCount()
  73. if err != nil {
  74. ctx.ServerError("GetCommitsCount", err)
  75. return
  76. }
  77. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo)
  78. if err != nil {
  79. ctx.ServerError("GetCommitGraph", err)
  80. return
  81. }
  82. ctx.Data["Graph"] = graph
  83. ctx.Data["Username"] = ctx.Repo.Owner.Name
  84. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  85. ctx.Data["CommitCount"] = commitsCount
  86. ctx.Data["Branch"] = ctx.Repo.BranchName
  87. ctx.Data["RequireGitGraph"] = true
  88. ctx.HTML(200, tplGraph)
  89. }
  90. // SearchCommits render commits filtered by keyword
  91. func SearchCommits(ctx *context.Context) {
  92. ctx.Data["PageIsCommits"] = true
  93. ctx.Data["PageIsViewCode"] = true
  94. query := strings.Trim(ctx.Query("q"), " ")
  95. if len(query) == 0 {
  96. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  97. return
  98. }
  99. all := ctx.QueryBool("all")
  100. opts := git.NewSearchCommitsOptions(query, all)
  101. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  102. if err != nil {
  103. ctx.ServerError("SearchCommits", err)
  104. return
  105. }
  106. commits = models.ValidateCommitsWithEmails(commits)
  107. commits = models.ParseCommitsWithSignature(commits)
  108. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  109. ctx.Data["Commits"] = commits
  110. ctx.Data["Keyword"] = query
  111. if all {
  112. ctx.Data["All"] = "checked"
  113. }
  114. ctx.Data["Username"] = ctx.Repo.Owner.Name
  115. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  116. ctx.Data["CommitCount"] = commits.Len()
  117. ctx.Data["Branch"] = ctx.Repo.BranchName
  118. ctx.HTML(200, tplCommits)
  119. }
  120. // FileHistory show a file's reversions
  121. func FileHistory(ctx *context.Context) {
  122. ctx.Data["IsRepoToolbarCommits"] = true
  123. fileName := ctx.Repo.TreePath
  124. if len(fileName) == 0 {
  125. Commits(ctx)
  126. return
  127. }
  128. branchName := ctx.Repo.BranchName
  129. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  130. if err != nil {
  131. ctx.ServerError("FileCommitsCount", err)
  132. return
  133. } else if commitsCount == 0 {
  134. ctx.NotFound("FileCommitsCount", nil)
  135. return
  136. }
  137. page := ctx.QueryInt("page")
  138. if page <= 1 {
  139. page = 1
  140. }
  141. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  142. if err != nil {
  143. ctx.ServerError("CommitsByFileAndRange", err)
  144. return
  145. }
  146. commits = models.ValidateCommitsWithEmails(commits)
  147. commits = models.ParseCommitsWithSignature(commits)
  148. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  149. ctx.Data["Commits"] = commits
  150. ctx.Data["Username"] = ctx.Repo.Owner.Name
  151. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  152. ctx.Data["FileName"] = fileName
  153. ctx.Data["CommitCount"] = commitsCount
  154. ctx.Data["Branch"] = branchName
  155. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  156. pager.SetDefaultParams(ctx)
  157. ctx.Data["Page"] = pager
  158. ctx.HTML(200, tplCommits)
  159. }
  160. // Diff show different from current commit to previous commit
  161. func Diff(ctx *context.Context) {
  162. ctx.Data["PageIsDiff"] = true
  163. ctx.Data["RequireHighlightJS"] = true
  164. userName := ctx.Repo.Owner.Name
  165. repoName := ctx.Repo.Repository.Name
  166. commitID := ctx.Params(":sha")
  167. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  168. if err != nil {
  169. if git.IsErrNotExist(err) {
  170. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  171. } else {
  172. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  173. }
  174. return
  175. }
  176. if len(commitID) != 40 {
  177. commitID = commit.ID.String()
  178. }
  179. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0)
  180. if err != nil {
  181. log.Error("GetLatestCommitStatus: %v", err)
  182. }
  183. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  184. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  185. commitID, setting.Git.MaxGitDiffLines,
  186. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  187. if err != nil {
  188. ctx.NotFound("GetDiffCommit", err)
  189. return
  190. }
  191. parents := make([]string, commit.ParentCount())
  192. for i := 0; i < commit.ParentCount(); i++ {
  193. sha, err := commit.ParentID(i)
  194. parents[i] = sha.String()
  195. if err != nil {
  196. ctx.NotFound("repo.Diff", err)
  197. return
  198. }
  199. }
  200. ctx.Data["CommitID"] = commitID
  201. ctx.Data["Username"] = userName
  202. ctx.Data["Reponame"] = repoName
  203. ctx.Data["IsImageFile"] = commit.IsImageFile
  204. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  205. ctx.Data["Commit"] = commit
  206. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  207. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  208. ctx.Data["Diff"] = diff
  209. ctx.Data["Parents"] = parents
  210. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  211. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
  212. if commit.ParentCount() > 0 {
  213. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", parents[0])
  214. }
  215. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
  216. ctx.Data["BranchName"], err = commit.GetBranchName()
  217. ctx.HTML(200, tplDiff)
  218. }
  219. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  220. func RawDiff(ctx *context.Context) {
  221. if err := models.GetRawDiff(
  222. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  223. ctx.Params(":sha"),
  224. models.RawDiffType(ctx.Params(":ext")),
  225. ctx.Resp,
  226. ); err != nil {
  227. ctx.ServerError("GetRawDiff", err)
  228. return
  229. }
  230. }
  231. // CompareDiff show different from one commit to another commit
  232. func CompareDiff(ctx *context.Context) {
  233. ctx.Data["IsRepoToolbarCommits"] = true
  234. ctx.Data["IsDiffCompare"] = true
  235. userName := ctx.Repo.Owner.Name
  236. repoName := ctx.Repo.Repository.Name
  237. beforeCommitID := ctx.Params(":before")
  238. afterCommitID := ctx.Params(":after")
  239. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  240. if err != nil {
  241. ctx.NotFound("GetCommit", err)
  242. return
  243. }
  244. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  245. afterCommitID, setting.Git.MaxGitDiffLines,
  246. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  247. if err != nil {
  248. ctx.NotFound("GetDiffRange", err)
  249. return
  250. }
  251. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  252. if err != nil {
  253. ctx.ServerError("CommitsBeforeUntil", err)
  254. return
  255. }
  256. commits = models.ValidateCommitsWithEmails(commits)
  257. commits = models.ParseCommitsWithSignature(commits)
  258. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  259. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  260. ctx.Data["Commits"] = commits
  261. ctx.Data["CommitCount"] = commits.Len()
  262. ctx.Data["BeforeCommitID"] = beforeCommitID
  263. ctx.Data["AfterCommitID"] = afterCommitID
  264. ctx.Data["Username"] = userName
  265. ctx.Data["Reponame"] = repoName
  266. ctx.Data["IsImageFile"] = commit.IsImageFile
  267. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  268. ctx.Data["Commit"] = commit
  269. ctx.Data["Diff"] = diff
  270. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  271. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", afterCommitID)
  272. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", beforeCommitID)
  273. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", afterCommitID)
  274. ctx.Data["RequireHighlightJS"] = true
  275. ctx.HTML(200, tplDiff)
  276. }