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.3 kB

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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "path"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/Unknwon/paginater"
  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. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  50. // Both `git log branchName` and `git log commitId` work.
  51. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  52. if err != nil {
  53. ctx.ServerError("CommitsByRange", err)
  54. return
  55. }
  56. commits = models.ValidateCommitsWithEmails(commits)
  57. commits = models.ParseCommitsWithSignature(commits)
  58. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  59. ctx.Data["Commits"] = commits
  60. ctx.Data["Username"] = ctx.Repo.Owner.Name
  61. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  62. ctx.Data["CommitCount"] = commitsCount
  63. ctx.Data["Branch"] = ctx.Repo.BranchName
  64. ctx.HTML(200, tplCommits)
  65. }
  66. // Graph render commit graph - show commits from all branches.
  67. func Graph(ctx *context.Context) {
  68. ctx.Data["PageIsCommits"] = true
  69. ctx.Data["PageIsViewCode"] = true
  70. commitsCount, err := ctx.Repo.GetCommitsCount()
  71. if err != nil {
  72. ctx.ServerError("GetCommitsCount", err)
  73. return
  74. }
  75. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo)
  76. if err != nil {
  77. ctx.ServerError("GetCommitGraph", err)
  78. return
  79. }
  80. ctx.Data["Graph"] = graph
  81. ctx.Data["Username"] = ctx.Repo.Owner.Name
  82. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  83. ctx.Data["CommitCount"] = commitsCount
  84. ctx.Data["Branch"] = ctx.Repo.BranchName
  85. ctx.Data["RequireGitGraph"] = true
  86. ctx.HTML(200, tplGraph)
  87. }
  88. // SearchCommits render commits filtered by keyword
  89. func SearchCommits(ctx *context.Context) {
  90. ctx.Data["PageIsCommits"] = true
  91. ctx.Data["PageIsViewCode"] = true
  92. query := strings.Trim(ctx.Query("q"), " ")
  93. if len(query) == 0 {
  94. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  95. return
  96. }
  97. all := ctx.QueryBool("all")
  98. opts := git.NewSearchCommitsOptions(query, all)
  99. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  100. if err != nil {
  101. ctx.ServerError("SearchCommits", err)
  102. return
  103. }
  104. commits = models.ValidateCommitsWithEmails(commits)
  105. commits = models.ParseCommitsWithSignature(commits)
  106. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  107. ctx.Data["Commits"] = commits
  108. ctx.Data["Keyword"] = query
  109. if all {
  110. ctx.Data["All"] = "checked"
  111. }
  112. ctx.Data["Username"] = ctx.Repo.Owner.Name
  113. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  114. ctx.Data["CommitCount"] = commits.Len()
  115. ctx.Data["Branch"] = ctx.Repo.BranchName
  116. ctx.HTML(200, tplCommits)
  117. }
  118. // FileHistory show a file's reversions
  119. func FileHistory(ctx *context.Context) {
  120. ctx.Data["IsRepoToolbarCommits"] = true
  121. fileName := ctx.Repo.TreePath
  122. if len(fileName) == 0 {
  123. Commits(ctx)
  124. return
  125. }
  126. branchName := ctx.Repo.BranchName
  127. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  128. if err != nil {
  129. ctx.ServerError("FileCommitsCount", err)
  130. return
  131. } else if commitsCount == 0 {
  132. ctx.NotFound("FileCommitsCount", nil)
  133. return
  134. }
  135. page := ctx.QueryInt("page")
  136. if page <= 1 {
  137. page = 1
  138. }
  139. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  140. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  141. if err != nil {
  142. ctx.ServerError("CommitsByFileAndRange", err)
  143. return
  144. }
  145. commits = models.ValidateCommitsWithEmails(commits)
  146. commits = models.ParseCommitsWithSignature(commits)
  147. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  148. ctx.Data["Commits"] = commits
  149. ctx.Data["Username"] = ctx.Repo.Owner.Name
  150. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  151. ctx.Data["FileName"] = fileName
  152. ctx.Data["CommitCount"] = commitsCount
  153. ctx.Data["Branch"] = branchName
  154. ctx.HTML(200, tplCommits)
  155. }
  156. // Diff show different from current commit to previous commit
  157. func Diff(ctx *context.Context) {
  158. ctx.Data["PageIsDiff"] = true
  159. ctx.Data["RequireHighlightJS"] = true
  160. userName := ctx.Repo.Owner.Name
  161. repoName := ctx.Repo.Repository.Name
  162. commitID := ctx.Params(":sha")
  163. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  164. if err != nil {
  165. if git.IsErrNotExist(err) {
  166. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  167. } else {
  168. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  169. }
  170. return
  171. }
  172. if len(commitID) != 40 {
  173. commitID = commit.ID.String()
  174. }
  175. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0)
  176. if err != nil {
  177. log.Error("GetLatestCommitStatus: %v", err)
  178. }
  179. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  180. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  181. commitID, setting.Git.MaxGitDiffLines,
  182. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  183. if err != nil {
  184. ctx.NotFound("GetDiffCommit", err)
  185. return
  186. }
  187. parents := make([]string, commit.ParentCount())
  188. for i := 0; i < commit.ParentCount(); i++ {
  189. sha, err := commit.ParentID(i)
  190. parents[i] = sha.String()
  191. if err != nil {
  192. ctx.NotFound("repo.Diff", err)
  193. return
  194. }
  195. }
  196. ctx.Data["CommitID"] = commitID
  197. ctx.Data["Username"] = userName
  198. ctx.Data["Reponame"] = repoName
  199. ctx.Data["IsImageFile"] = commit.IsImageFile
  200. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  201. ctx.Data["Commit"] = commit
  202. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  203. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  204. ctx.Data["Diff"] = diff
  205. ctx.Data["Parents"] = parents
  206. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  207. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
  208. if commit.ParentCount() > 0 {
  209. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", parents[0])
  210. }
  211. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
  212. ctx.HTML(200, tplDiff)
  213. }
  214. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  215. func RawDiff(ctx *context.Context) {
  216. if err := models.GetRawDiff(
  217. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  218. ctx.Params(":sha"),
  219. models.RawDiffType(ctx.Params(":ext")),
  220. ctx.Resp,
  221. ); err != nil {
  222. ctx.ServerError("GetRawDiff", err)
  223. return
  224. }
  225. }
  226. // CompareDiff show different from one commit to another commit
  227. func CompareDiff(ctx *context.Context) {
  228. ctx.Data["IsRepoToolbarCommits"] = true
  229. ctx.Data["IsDiffCompare"] = true
  230. userName := ctx.Repo.Owner.Name
  231. repoName := ctx.Repo.Repository.Name
  232. beforeCommitID := ctx.Params(":before")
  233. afterCommitID := ctx.Params(":after")
  234. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  235. if err != nil {
  236. ctx.NotFound("GetCommit", err)
  237. return
  238. }
  239. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  240. afterCommitID, setting.Git.MaxGitDiffLines,
  241. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  242. if err != nil {
  243. ctx.NotFound("GetDiffRange", err)
  244. return
  245. }
  246. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  247. if err != nil {
  248. ctx.ServerError("CommitsBeforeUntil", err)
  249. return
  250. }
  251. commits = models.ValidateCommitsWithEmails(commits)
  252. commits = models.ParseCommitsWithSignature(commits)
  253. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  254. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  255. ctx.Data["Commits"] = commits
  256. ctx.Data["CommitCount"] = commits.Len()
  257. ctx.Data["BeforeCommitID"] = beforeCommitID
  258. ctx.Data["AfterCommitID"] = afterCommitID
  259. ctx.Data["Username"] = userName
  260. ctx.Data["Reponame"] = repoName
  261. ctx.Data["IsImageFile"] = commit.IsImageFile
  262. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  263. ctx.Data["Commit"] = commit
  264. ctx.Data["Diff"] = diff
  265. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  266. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", afterCommitID)
  267. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", beforeCommitID)
  268. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", afterCommitID)
  269. ctx.Data["RequireHighlightJS"] = true
  270. ctx.HTML(200, tplDiff)
  271. }