2020-01-21 01:31:19 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-21 01:31:19 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-05-17 05:01:38 +05:30
|
|
|
"strings"
|
2020-01-21 01:31:19 +05:30
|
|
|
"time"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-17 04:59:26 +05:30
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-21 01:31:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-06-06 13:31:49 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2020-11-28 08:12:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-21 01:31:19 +05:30
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
2022-12-16 02:14:16 +05:30
|
|
|
// GitFsckRepos calls 'git fsck' to check repository health.
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
func GitFsckRepos(ctx context.Context, timeout time.Duration, args git.TrustedCmdArgs) error {
|
2020-01-21 01:31:19 +05:30
|
|
|
log.Trace("Doing: GitFsck")
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
if err := db.Iterate(
|
2022-03-22 20:52:54 +05:30
|
|
|
ctx,
|
2020-01-21 01:31:19 +05:30
|
|
|
builder.Expr("id>0 AND is_fsck_enabled=?", true),
|
2022-10-31 21:21:14 +05:30
|
|
|
func(ctx context.Context, repo *repo_model.Repository) error {
|
2020-01-21 01:31:19 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 10:43:16 +05:30
|
|
|
return db.ErrCancelledf("before fsck of %s", repo.FullName())
|
2020-01-21 01:31:19 +05:30
|
|
|
default:
|
|
|
|
}
|
2022-12-16 02:14:16 +05:30
|
|
|
return GitFsckRepo(ctx, repo, timeout, args)
|
2020-01-21 01:31:19 +05:30
|
|
|
},
|
|
|
|
); err != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Trace("Error: GitFsck: %v", err)
|
2020-01-21 01:31:19 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Finished: GitFsck")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-16 02:14:16 +05:30
|
|
|
// GitFsckRepo calls 'git fsck' to check an individual repository's health.
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
func GitFsckRepo(ctx context.Context, repo *repo_model.Repository, timeout time.Duration, args git.TrustedCmdArgs) error {
|
2022-12-16 02:14:16 +05:30
|
|
|
log.Trace("Running health check on repository %-v", repo)
|
|
|
|
repoPath := repo.RepoPath()
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
if err := git.Fsck(ctx, repoPath, timeout, args); err != nil {
|
2022-12-16 02:14:16 +05:30
|
|
|
log.Warn("Failed to health check repository (%-v): %v", repo, err)
|
|
|
|
if err = system_model.CreateRepositoryNotice("Failed to health check repository (%s): %v", repo.FullName(), err); err != nil {
|
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:31:19 +05:30
|
|
|
// GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
func GitGcRepos(ctx context.Context, timeout time.Duration, args git.TrustedCmdArgs) error {
|
2020-01-21 01:31:19 +05:30
|
|
|
log.Trace("Doing: GitGcRepos")
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
if err := db.Iterate(
|
2022-03-22 20:52:54 +05:30
|
|
|
ctx,
|
2020-01-21 01:31:19 +05:30
|
|
|
builder.Gt{"id": 0},
|
2022-10-31 21:21:14 +05:30
|
|
|
func(ctx context.Context, repo *repo_model.Repository) error {
|
2020-01-21 01:31:19 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 10:43:16 +05:30
|
|
|
return db.ErrCancelledf("before GC of %s", repo.FullName())
|
2020-01-21 01:31:19 +05:30
|
|
|
default:
|
|
|
|
}
|
2023-01-13 07:18:48 +05:30
|
|
|
// we can ignore the error here because it will be logged in GitGCRepo
|
|
|
|
_ = GitGcRepo(ctx, repo, timeout, args)
|
|
|
|
return nil
|
2020-01-21 01:31:19 +05:30
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Finished: GitGcRepos")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-16 02:14:16 +05:30
|
|
|
// GitGcRepo calls 'git gc' to remove unnecessary files and optimize the local repository
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
func GitGcRepo(ctx context.Context, repo *repo_model.Repository, timeout time.Duration, args git.TrustedCmdArgs) error {
|
2022-12-16 02:14:16 +05:30
|
|
|
log.Trace("Running git gc on %-v", repo)
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
command := git.NewCommand(ctx, "gc").AddArguments(args...).
|
2022-12-16 02:14:16 +05:30
|
|
|
SetDescription(fmt.Sprintf("Repository Garbage Collection: %s", repo.FullName()))
|
|
|
|
var stdout string
|
|
|
|
var err error
|
|
|
|
stdout, _, err = command.RunStdString(&git.RunOpts{Timeout: timeout, Dir: repo.RepoPath()})
|
|
|
|
|
|
|
|
if err != nil {
|
2023-01-13 07:18:48 +05:30
|
|
|
log.Error("Repository garbage collection failed for %-v. Stdout: %s\nError: %v", repo, stdout, err)
|
2022-12-16 02:14:16 +05:30
|
|
|
desc := fmt.Sprintf("Repository garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
|
2023-01-13 02:27:12 +05:30
|
|
|
if err := system_model.CreateRepositoryNotice(desc); err != nil {
|
2022-12-16 02:14:16 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Repository garbage collection failed in repo: %s: Error: %w", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now update the size of the repository
|
|
|
|
if err := repo_module.UpdateRepoSize(ctx, repo); err != nil {
|
|
|
|
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)
|
2023-01-13 02:27:12 +05:30
|
|
|
if err := system_model.CreateRepositoryNotice(desc); err != nil {
|
2022-12-16 02:14:16 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Updating size as part of garbage collection failed in repo: %s: Error: %w", repo.FullName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 12:55:47 +05:30
|
|
|
func gatherMissingRepoRecords(ctx context.Context) (repo_model.RepositoryList, error) {
|
2021-12-10 06:57:50 +05:30
|
|
|
repos := make([]*repo_model.Repository, 0, 10)
|
2021-09-19 17:19:59 +05:30
|
|
|
if err := db.Iterate(
|
2022-03-22 20:52:54 +05:30
|
|
|
ctx,
|
2020-01-21 01:31:19 +05:30
|
|
|
builder.Gt{"id": 0},
|
2022-10-31 21:21:14 +05:30
|
|
|
func(ctx context.Context, repo *repo_model.Repository) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 10:43:16 +05:30
|
|
|
return db.ErrCancelledf("during gathering missing repo records before checking %s", repo.FullName())
|
2020-05-17 05:01:38 +05:30
|
|
|
default:
|
|
|
|
}
|
2020-11-28 08:12:08 +05:30
|
|
|
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-21 01:31:19 +05:30
|
|
|
repos = append(repos, repo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
); err != nil {
|
2020-10-12 01:57:20 +05:30
|
|
|
if strings.HasPrefix(err.Error(), "Aborted gathering missing repo") {
|
2020-05-17 05:01:38 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-17 04:59:26 +05:30
|
|
|
if err2 := system_model.CreateRepositoryNotice("gatherMissingRepoRecords: %v", err); err2 != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err2)
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
2020-05-17 05:01:38 +05:30
|
|
|
return nil, err
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
|
|
|
return repos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMissingRepositories deletes all repository records that lost Git files.
|
2021-11-24 15:19:20 +05:30
|
|
|
func DeleteMissingRepositories(ctx context.Context, doer *user_model.User) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
repos, err := gatherMissingRepoRecords(ctx)
|
2020-01-21 01:31:19 +05:30
|
|
|
if err != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
return err
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-05-17 05:01:38 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 10:43:16 +05:30
|
|
|
return db.ErrCancelledf("during DeleteMissingRepositories before %s", repo.FullName())
|
2020-05-17 05:01:38 +05:30
|
|
|
default:
|
|
|
|
}
|
2020-01-21 01:31:19 +05:30
|
|
|
log.Trace("Deleting %d/%d...", repo.OwnerID, repo.ID)
|
2023-09-08 10:21:15 +05:30
|
|
|
if err := DeleteRepositoryDirectly(ctx, doer, repo.OwnerID, repo.ID); err != nil {
|
2023-01-13 07:18:48 +05:30
|
|
|
log.Error("Failed to DeleteRepository %-v: Error: %v", repo, err)
|
2022-10-17 04:59:26 +05:30
|
|
|
if err2 := system_model.CreateRepositoryNotice("Failed to DeleteRepository %s [%d]: Error: %v", repo.FullName(), repo.ID, err); err2 != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err)
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReinitMissingRepositories reinitializes all repository records that lost Git files.
|
2020-05-17 05:01:38 +05:30
|
|
|
func ReinitMissingRepositories(ctx context.Context) error {
|
|
|
|
repos, err := gatherMissingRepoRecords(ctx)
|
2020-01-21 01:31:19 +05:30
|
|
|
if err != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
return err
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
2020-05-17 05:01:38 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 10:43:16 +05:30
|
|
|
return db.ErrCancelledf("during ReinitMissingRepositories before %s", repo.FullName())
|
2020-05-17 05:01:38 +05:30
|
|
|
default:
|
|
|
|
}
|
2020-01-21 01:31:19 +05:30
|
|
|
log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID)
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := git.InitRepository(ctx, repo.RepoPath(), true); err != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err)
|
2022-10-17 04:59:26 +05:30
|
|
|
if err2 := system_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil {
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Error("CreateRepositoryNotice: %v", err2)
|
2020-01-21 01:31:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|