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.

276 lines
6.9 KiB

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
5 years ago
10 years ago
  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 models
  5. import (
  6. "container/list"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/modules/cache"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. // env keys for git hooks need
  16. const (
  17. EnvRepoName = "GITEA_REPO_NAME"
  18. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  19. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  20. EnvPusherName = "GITEA_PUSHER_NAME"
  21. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  22. EnvPusherID = "GITEA_PUSHER_ID"
  23. )
  24. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  25. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  26. return &PushCommit{
  27. Sha1: commit.ID.String(),
  28. Message: commit.Message(),
  29. AuthorEmail: commit.Author.Email,
  30. AuthorName: commit.Author.Name,
  31. CommitterEmail: commit.Committer.Email,
  32. CommitterName: commit.Committer.Name,
  33. Timestamp: commit.Author.When,
  34. }
  35. }
  36. // ListToPushCommits transforms a list.List to PushCommits type.
  37. func ListToPushCommits(l *list.List) *PushCommits {
  38. var commits []*PushCommit
  39. var actEmail string
  40. for e := l.Front(); e != nil; e = e.Next() {
  41. commit := e.Value.(*git.Commit)
  42. if actEmail == "" {
  43. actEmail = commit.Committer.Email
  44. }
  45. commits = append(commits, CommitToPushCommit(commit))
  46. }
  47. return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
  48. }
  49. // PushUpdateOptions defines the push update options
  50. type PushUpdateOptions struct {
  51. PusherID int64
  52. PusherName string
  53. RepoUserName string
  54. RepoName string
  55. RefFullName string
  56. OldCommitID string
  57. NewCommitID string
  58. }
  59. // PushUpdate must be called for any push actions in order to
  60. // generates necessary push action history feeds.
  61. func PushUpdate(branch string, opt PushUpdateOptions) error {
  62. repo, err := pushUpdate(opt)
  63. if err != nil {
  64. return err
  65. }
  66. pusher, err := GetUserByID(opt.PusherID)
  67. if err != nil {
  68. return err
  69. }
  70. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  71. go AddTestPullRequestTask(pusher, repo.ID, branch, true)
  72. return nil
  73. }
  74. func pushUpdateDeleteTag(repo *Repository, tagName string) error {
  75. rel, err := GetRelease(repo.ID, tagName)
  76. if err != nil {
  77. if IsErrReleaseNotExist(err) {
  78. return nil
  79. }
  80. return fmt.Errorf("GetRelease: %v", err)
  81. }
  82. if rel.IsTag {
  83. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  84. return fmt.Errorf("Delete: %v", err)
  85. }
  86. } else {
  87. rel.IsDraft = true
  88. rel.NumCommits = 0
  89. rel.Sha1 = ""
  90. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  91. return fmt.Errorf("Update: %v", err)
  92. }
  93. }
  94. return nil
  95. }
  96. func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
  97. rel, err := GetRelease(repo.ID, tagName)
  98. if err != nil && !IsErrReleaseNotExist(err) {
  99. return fmt.Errorf("GetRelease: %v", err)
  100. }
  101. tag, err := gitRepo.GetTag(tagName)
  102. if err != nil {
  103. return fmt.Errorf("GetTag: %v", err)
  104. }
  105. commit, err := tag.Commit()
  106. if err != nil {
  107. return fmt.Errorf("Commit: %v", err)
  108. }
  109. sig := tag.Tagger
  110. if sig == nil {
  111. sig = commit.Author
  112. }
  113. if sig == nil {
  114. sig = commit.Committer
  115. }
  116. var author *User
  117. var createdAt = time.Unix(1, 0)
  118. if sig != nil {
  119. author, err = GetUserByEmail(sig.Email)
  120. if err != nil && !IsErrUserNotExist(err) {
  121. return fmt.Errorf("GetUserByEmail: %v", err)
  122. }
  123. createdAt = sig.When
  124. }
  125. commitsCount, err := commit.CommitsCount()
  126. if err != nil {
  127. return fmt.Errorf("CommitsCount: %v", err)
  128. }
  129. if rel == nil {
  130. rel = &Release{
  131. RepoID: repo.ID,
  132. Title: "",
  133. TagName: tagName,
  134. LowerTagName: strings.ToLower(tagName),
  135. Target: "",
  136. Sha1: commit.ID.String(),
  137. NumCommits: commitsCount,
  138. Note: "",
  139. IsDraft: false,
  140. IsPrerelease: false,
  141. IsTag: true,
  142. CreatedUnix: util.TimeStamp(createdAt.Unix()),
  143. }
  144. if author != nil {
  145. rel.PublisherID = author.ID
  146. }
  147. if _, err = x.InsertOne(rel); err != nil {
  148. return fmt.Errorf("InsertOne: %v", err)
  149. }
  150. } else {
  151. rel.Sha1 = commit.ID.String()
  152. rel.CreatedUnix = util.TimeStamp(createdAt.Unix())
  153. rel.NumCommits = commitsCount
  154. rel.IsDraft = false
  155. if rel.IsTag && author != nil {
  156. rel.PublisherID = author.ID
  157. }
  158. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  159. return fmt.Errorf("Update: %v", err)
  160. }
  161. }
  162. return nil
  163. }
  164. func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
  165. isNewRef := opts.OldCommitID == git.EmptySHA
  166. isDelRef := opts.NewCommitID == git.EmptySHA
  167. if isNewRef && isDelRef {
  168. return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  169. }
  170. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  171. _, err = git.NewCommand("update-server-info").RunInDir(repoPath)
  172. if err != nil {
  173. return nil, fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  174. }
  175. owner, err := GetUserByName(opts.RepoUserName)
  176. if err != nil {
  177. return nil, fmt.Errorf("GetUserByName: %v", err)
  178. }
  179. repo, err = GetRepositoryByName(owner.ID, opts.RepoName)
  180. if err != nil {
  181. return nil, fmt.Errorf("GetRepositoryByName: %v", err)
  182. }
  183. gitRepo, err := git.OpenRepository(repoPath)
  184. if err != nil {
  185. return nil, fmt.Errorf("OpenRepository: %v", err)
  186. }
  187. if err = repo.UpdateSize(); err != nil {
  188. log.Error("Failed to update size for repository: %v", err)
  189. }
  190. var commits = &PushCommits{}
  191. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  192. // If is tag reference
  193. tagName := opts.RefFullName[len(git.TagPrefix):]
  194. if isDelRef {
  195. err = pushUpdateDeleteTag(repo, tagName)
  196. if err != nil {
  197. return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err)
  198. }
  199. } else {
  200. // Clear cache for tag commit count
  201. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  202. err = pushUpdateAddTag(repo, gitRepo, tagName)
  203. if err != nil {
  204. return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
  205. }
  206. }
  207. } else if !isDelRef {
  208. // If is branch reference
  209. // Clear cache for branch commit count
  210. cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true))
  211. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  212. if err != nil {
  213. return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
  214. }
  215. // Push new branch.
  216. var l *list.List
  217. if isNewRef {
  218. l, err = newCommit.CommitsBeforeLimit(10)
  219. if err != nil {
  220. return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  221. }
  222. } else {
  223. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  224. if err != nil {
  225. return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  226. }
  227. }
  228. commits = ListToPushCommits(l)
  229. }
  230. if err := CommitRepoAction(CommitRepoActionOptions{
  231. PusherName: opts.PusherName,
  232. RepoOwnerID: owner.ID,
  233. RepoName: repo.Name,
  234. RefFullName: opts.RefFullName,
  235. OldCommitID: opts.OldCommitID,
  236. NewCommitID: opts.NewCommitID,
  237. Commits: commits,
  238. }); err != nil {
  239. return nil, fmt.Errorf("CommitRepoAction: %v", err)
  240. }
  241. return repo, nil
  242. }