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.

188 lines
5.5 KiB

  1. // Copyright 2020 The Gitea 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 repository
  5. import (
  6. "context"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "github.com/unknwon/com"
  14. "xorm.io/builder"
  15. )
  16. // GitFsck calls 'git fsck' to check repository health.
  17. func GitFsck(ctx context.Context, timeout time.Duration, args []string) error {
  18. log.Trace("Doing: GitFsck")
  19. if err := models.Iterate(
  20. models.DefaultDBContext(),
  21. new(models.Repository),
  22. builder.Expr("id>0 AND is_fsck_enabled=?", true),
  23. func(idx int, bean interface{}) error {
  24. repo := bean.(*models.Repository)
  25. select {
  26. case <-ctx.Done():
  27. return models.ErrCancelledf("before fsck of %s", repo.FullName())
  28. default:
  29. }
  30. log.Trace("Running health check on repository %v", repo)
  31. repoPath := repo.RepoPath()
  32. if err := git.Fsck(ctx, repoPath, timeout, args...); err != nil {
  33. log.Warn("Failed to health check repository (%v): %v", repo, err)
  34. if err = models.CreateRepositoryNotice("Failed to health check repository (%s): %v", repo.FullName(), err); err != nil {
  35. log.Error("CreateRepositoryNotice: %v", err)
  36. }
  37. }
  38. return nil
  39. },
  40. ); err != nil {
  41. log.Trace("Error: GitFsck: %v", err)
  42. return err
  43. }
  44. log.Trace("Finished: GitFsck")
  45. return nil
  46. }
  47. // GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
  48. func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) error {
  49. log.Trace("Doing: GitGcRepos")
  50. args = append([]string{"gc"}, args...)
  51. if err := models.Iterate(
  52. models.DefaultDBContext(),
  53. new(models.Repository),
  54. builder.Gt{"id": 0},
  55. func(idx int, bean interface{}) error {
  56. repo := bean.(*models.Repository)
  57. select {
  58. case <-ctx.Done():
  59. return models.ErrCancelledf("before GC of %s", repo.FullName())
  60. default:
  61. }
  62. log.Trace("Running git gc on %v", repo)
  63. command := git.NewCommandContext(ctx, args...).
  64. SetDescription(fmt.Sprintf("Repository Garbage Collection: %s", repo.FullName()))
  65. var stdout string
  66. var err error
  67. if timeout > 0 {
  68. var stdoutBytes []byte
  69. stdoutBytes, err = command.RunInDirTimeout(
  70. timeout,
  71. repo.RepoPath())
  72. stdout = string(stdoutBytes)
  73. } else {
  74. stdout, err = command.RunInDir(repo.RepoPath())
  75. }
  76. if err != nil {
  77. log.Error("Repository garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
  78. desc := fmt.Sprintf("Repository garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
  79. if err = models.CreateRepositoryNotice(desc); err != nil {
  80. log.Error("CreateRepositoryNotice: %v", err)
  81. }
  82. return fmt.Errorf("Repository garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
  83. }
  84. return nil
  85. },
  86. ); err != nil {
  87. return err
  88. }
  89. log.Trace("Finished: GitGcRepos")
  90. return nil
  91. }
  92. func gatherMissingRepoRecords(ctx context.Context) ([]*models.Repository, error) {
  93. repos := make([]*models.Repository, 0, 10)
  94. if err := models.Iterate(
  95. models.DefaultDBContext(),
  96. new(models.Repository),
  97. builder.Gt{"id": 0},
  98. func(idx int, bean interface{}) error {
  99. repo := bean.(*models.Repository)
  100. select {
  101. case <-ctx.Done():
  102. return models.ErrCancelledf("during gathering missing repo records before checking %s", repo.FullName())
  103. default:
  104. }
  105. if !com.IsDir(repo.RepoPath()) {
  106. repos = append(repos, repo)
  107. }
  108. return nil
  109. },
  110. ); err != nil {
  111. if strings.HasPrefix("Aborted gathering missing repo", err.Error()) {
  112. return nil, err
  113. }
  114. if err2 := models.CreateRepositoryNotice("gatherMissingRepoRecords: %v", err); err2 != nil {
  115. log.Error("CreateRepositoryNotice: %v", err2)
  116. }
  117. return nil, err
  118. }
  119. return repos, nil
  120. }
  121. // DeleteMissingRepositories deletes all repository records that lost Git files.
  122. func DeleteMissingRepositories(ctx context.Context, doer *models.User) error {
  123. repos, err := gatherMissingRepoRecords(ctx)
  124. if err != nil {
  125. return err
  126. }
  127. if len(repos) == 0 {
  128. return nil
  129. }
  130. for _, repo := range repos {
  131. select {
  132. case <-ctx.Done():
  133. return models.ErrCancelledf("during DeleteMissingRepositories before %s", repo.FullName())
  134. default:
  135. }
  136. log.Trace("Deleting %d/%d...", repo.OwnerID, repo.ID)
  137. if err := models.DeleteRepository(doer, repo.OwnerID, repo.ID); err != nil {
  138. log.Error("Failed to DeleteRepository %s [%d]: Error: %v", repo.FullName(), repo.ID, err)
  139. if err2 := models.CreateRepositoryNotice("Failed to DeleteRepository %s [%d]: Error: %v", repo.FullName(), repo.ID, err); err2 != nil {
  140. log.Error("CreateRepositoryNotice: %v", err)
  141. }
  142. }
  143. }
  144. return nil
  145. }
  146. // ReinitMissingRepositories reinitializes all repository records that lost Git files.
  147. func ReinitMissingRepositories(ctx context.Context) error {
  148. repos, err := gatherMissingRepoRecords(ctx)
  149. if err != nil {
  150. return err
  151. }
  152. if len(repos) == 0 {
  153. return nil
  154. }
  155. for _, repo := range repos {
  156. select {
  157. case <-ctx.Done():
  158. return models.ErrCancelledf("during ReinitMissingRepositories before %s", repo.FullName())
  159. default:
  160. }
  161. log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID)
  162. if err := git.InitRepository(repo.RepoPath(), true); err != nil {
  163. log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err)
  164. if err2 := models.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil {
  165. log.Error("CreateRepositoryNotice: %v", err2)
  166. }
  167. }
  168. }
  169. return nil
  170. }