2020-01-20 13:01:19 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-05-16 17:31:38 -06:00
|
|
|
"strings"
|
2020-01-20 13:01:19 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-17 22:58:42 -07:00
|
|
|
admin_model "code.gitea.io/gitea/models/admin"
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-20 13:01:19 -07:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-11-27 19:42:08 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-20 13:01:19 -07:00
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GitFsck calls 'git fsck' to check repository health.
|
2020-05-16 17:31:38 -06:00
|
|
|
func GitFsck(ctx context.Context, timeout time.Duration, args []string) error {
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Trace("Doing: GitFsck")
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
if err := db.Iterate(
|
2021-09-23 09:45:36 -06:00
|
|
|
db.DefaultContext,
|
2021-12-09 18:27:50 -07:00
|
|
|
new(repo_model.Repository),
|
2020-01-20 13:01:19 -07:00
|
|
|
builder.Expr("id>0 AND is_fsck_enabled=?", true),
|
|
|
|
func(idx int, bean interface{}) error {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := bean.(*repo_model.Repository)
|
2020-01-20 13:01:19 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("before fsck of %s", repo.FullName())
|
2020-01-20 13:01:19 -07:00
|
|
|
default:
|
|
|
|
}
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Trace("Running health check on repository %v", repo)
|
2020-01-20 13:01:19 -07:00
|
|
|
repoPath := repo.RepoPath()
|
2020-05-16 17:31:38 -06:00
|
|
|
if err := git.Fsck(ctx, repoPath, timeout, args...); err != nil {
|
|
|
|
log.Warn("Failed to health check repository (%v): %v", repo, err)
|
2021-11-17 22:58:42 -07:00
|
|
|
if err = admin_model.CreateRepositoryNotice("Failed to health check repository (%s): %v", repo.FullName(), err); err != nil {
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Trace("Error: GitFsck: %v", err)
|
2020-01-20 13:01:19 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Finished: GitFsck")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
|
2020-05-16 17:31:38 -06:00
|
|
|
func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) error {
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Trace("Doing: GitGcRepos")
|
2020-05-16 17:31:38 -06:00
|
|
|
args = append([]string{"gc"}, args...)
|
2020-01-20 13:01:19 -07:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
if err := db.Iterate(
|
2021-09-23 09:45:36 -06:00
|
|
|
db.DefaultContext,
|
2021-12-09 18:27:50 -07:00
|
|
|
new(repo_model.Repository),
|
2020-01-20 13:01:19 -07:00
|
|
|
builder.Gt{"id": 0},
|
|
|
|
func(idx int, bean interface{}) error {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := bean.(*repo_model.Repository)
|
2020-01-20 13:01:19 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("before GC of %s", repo.FullName())
|
2020-01-20 13:01:19 -07:00
|
|
|
default:
|
|
|
|
}
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Trace("Running git gc on %v", repo)
|
2022-02-06 12:01:47 -07:00
|
|
|
command := git.NewCommand(ctx, args...).
|
2020-05-16 17:31:38 -06:00
|
|
|
SetDescription(fmt.Sprintf("Repository Garbage Collection: %s", repo.FullName()))
|
|
|
|
var stdout string
|
|
|
|
var err error
|
|
|
|
if timeout > 0 {
|
|
|
|
var stdoutBytes []byte
|
|
|
|
stdoutBytes, err = command.RunInDirTimeout(
|
|
|
|
timeout,
|
|
|
|
repo.RepoPath())
|
|
|
|
stdout = string(stdoutBytes)
|
|
|
|
} else {
|
|
|
|
stdout, err = command.RunInDir(repo.RepoPath())
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
2020-05-16 17:31:38 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Error("Repository garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
|
2020-05-16 17:31:38 -06:00
|
|
|
desc := fmt.Sprintf("Repository garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
|
2021-11-17 22:58:42 -07:00
|
|
|
if err = admin_model.CreateRepositoryNotice(desc); err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Repository garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
2021-03-27 21:56:28 -06:00
|
|
|
|
|
|
|
// Now update the size of the repository
|
2021-12-09 18:27:50 -07:00
|
|
|
if err := models.UpdateRepoSize(db.DefaultContext, repo); err != nil {
|
2021-03-27 21:56:28 -06:00
|
|
|
log.Error("Updating size as part of garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
|
|
|
|
desc := fmt.Sprintf("Updating size as part of garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
|
2021-11-17 22:58:42 -07:00
|
|
|
if err = admin_model.CreateRepositoryNotice(desc); err != nil {
|
2021-03-27 21:56:28 -06:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Updating size as part of garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:01:19 -07:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Finished: GitGcRepos")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
func gatherMissingRepoRecords(ctx context.Context) ([]*repo_model.Repository, error) {
|
|
|
|
repos := make([]*repo_model.Repository, 0, 10)
|
2021-09-19 05:49:59 -06:00
|
|
|
if err := db.Iterate(
|
2021-09-23 09:45:36 -06:00
|
|
|
db.DefaultContext,
|
2021-12-09 18:27:50 -07:00
|
|
|
new(repo_model.Repository),
|
2020-01-20 13:01:19 -07:00
|
|
|
builder.Gt{"id": 0},
|
|
|
|
func(idx int, bean interface{}) error {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := bean.(*repo_model.Repository)
|
2020-05-16 17:31:38 -06:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("during gathering missing repo records before checking %s", repo.FullName())
|
2020-05-16 17:31:38 -06:00
|
|
|
default:
|
|
|
|
}
|
2020-11-27 19:42:08 -07:00
|
|
|
isDir, err := util.IsDir(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to check dir for %s. %w", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
if !isDir {
|
2020-01-20 13:01:19 -07:00
|
|
|
repos = append(repos, repo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
2020-10-11 14:27:20 -06:00
|
|
|
if strings.HasPrefix(err.Error(), "Aborted gathering missing repo") {
|
2020-05-16 17:31:38 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-17 22:58:42 -07:00
|
|
|
if err2 := admin_model.CreateRepositoryNotice("gatherMissingRepoRecords: %v", err); err2 != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err2)
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
2020-05-16 17:31:38 -06:00
|
|
|
return nil, err
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
|
|
|
return repos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMissingRepositories deletes all repository records that lost Git files.
|
2021-11-24 02:49:20 -07:00
|
|
|
func DeleteMissingRepositories(ctx context.Context, doer *user_model.User) error {
|
2020-05-16 17:31:38 -06:00
|
|
|
repos, err := gatherMissingRepoRecords(ctx)
|
2020-01-20 13:01:19 -07:00
|
|
|
if err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
return err
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-05-16 17:31:38 -06:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("during DeleteMissingRepositories before %s", repo.FullName())
|
2020-05-16 17:31:38 -06:00
|
|
|
default:
|
|
|
|
}
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Trace("Deleting %d/%d...", repo.OwnerID, repo.ID)
|
|
|
|
if err := models.DeleteRepository(doer, repo.OwnerID, repo.ID); err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("Failed to DeleteRepository %s [%d]: Error: %v", repo.FullName(), repo.ID, err)
|
2021-11-17 22:58:42 -07:00
|
|
|
if err2 := admin_model.CreateRepositoryNotice("Failed to DeleteRepository %s [%d]: Error: %v", repo.FullName(), repo.ID, err); err2 != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReinitMissingRepositories reinitializes all repository records that lost Git files.
|
2020-05-16 17:31:38 -06:00
|
|
|
func ReinitMissingRepositories(ctx context.Context) error {
|
|
|
|
repos, err := gatherMissingRepoRecords(ctx)
|
2020-01-20 13:01:19 -07:00
|
|
|
if err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
return err
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-05-16 17:31:38 -06:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("during ReinitMissingRepositories before %s", repo.FullName())
|
2020-05-16 17:31:38 -06:00
|
|
|
default:
|
|
|
|
}
|
2020-01-20 13:01:19 -07:00
|
|
|
log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID)
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := git.InitRepository(ctx, repo.RepoPath(), true); err != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err)
|
2021-11-17 22:58:42 -07:00
|
|
|
if err2 := admin_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil {
|
2020-05-16 17:31:38 -06:00
|
|
|
log.Error("CreateRepositoryNotice: %v", err2)
|
2020-01-20 13:01:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|