diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/context.go | 2 | ||||
-rw-r--r-- | modules/context/repo.go | 37 | ||||
-rw-r--r-- | modules/git/command.go | 16 | ||||
-rw-r--r-- | modules/git/repo.go | 2 | ||||
-rw-r--r-- | modules/git/repo_base_nogogit.go | 2 | ||||
-rw-r--r-- | modules/indexer/code/indexer.go | 4 | ||||
-rw-r--r-- | modules/indexer/stats/db.go | 7 | ||||
-rw-r--r-- | modules/indexer/stats/queue.go | 5 | ||||
-rw-r--r-- | modules/setting/setting.go | 15 |
9 files changed, 62 insertions, 28 deletions
diff --git a/modules/context/context.go b/modules/context/context.go index 2507cc10c0..21bae91129 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -301,7 +301,7 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) { // it's safe to show internal error to admin users, and it helps if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) { - ctx.Data["ErrorMsg"] = logErr + ctx.Data["ErrorMsg"] = fmt.Sprintf("%s, %s", logMsg, logErr) } } diff --git a/modules/context/repo.go b/modules/context/repo.go index 8b4d0c1bf4..1736de2e4b 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -184,6 +184,9 @@ func (r *Repository) CanCreateIssueDependencies(user *user_model.User, isPull bo // GetCommitsCount returns cached commit count for current view func (r *Repository) GetCommitsCount() (int64, error) { + if r.Commit == nil { + return 0, nil + } var contextName string if r.IsViewBranch { contextName = r.BranchName @@ -642,8 +645,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { if err != nil { if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") { log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err) - ctx.Repo.Repository.Status = repo_model.RepositoryBroken - ctx.Repo.Repository.IsEmpty = true + ctx.Repo.Repository.MarkAsBrokenEmpty() ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch // Only allow access to base of repo or settings if !isHomeOrSettings { @@ -689,7 +691,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["BranchesCount"] = len(brs) // If not branch selected, try default one. - // If default branch doesn't exists, fall back to some other branch. + // If default branch doesn't exist, fall back to some other branch. if len(ctx.Repo.BranchName) == 0 { if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) { ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch @@ -878,6 +880,10 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context return func(ctx *Context) (cancel context.CancelFunc) { // Empty repository does not have reference information. if ctx.Repo.Repository.IsEmpty { + // assume the user is viewing the (non-existent) default branch + ctx.Repo.IsViewBranch = true + ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch + ctx.Data["TreePath"] = "" return } @@ -907,27 +913,30 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context refName = ctx.Repo.Repository.DefaultBranch if !ctx.Repo.GitRepo.IsBranchExist(refName) { brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) - if err != nil { - ctx.ServerError("GetBranches", err) - return + if err == nil && len(brs) != 0 { + refName = brs[0] } else if len(brs) == 0 { - err = fmt.Errorf("No branches in non-empty repository %s", - ctx.Repo.GitRepo.Path) - ctx.ServerError("GetBranches", err) - return + log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path) + ctx.Repo.Repository.MarkAsBrokenEmpty() + } else { + log.Error("GetBranches error: %v", err) + ctx.Repo.Repository.MarkAsBrokenEmpty() } - refName = brs[0] } ctx.Repo.RefName = refName ctx.Repo.BranchName = refName ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) - if err != nil { + if err == nil { + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + } else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") { + // if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users + log.Error("GetBranchCommit: %v", err) + ctx.Repo.Repository.MarkAsBrokenEmpty() + } else { ctx.ServerError("GetBranchCommit", err) return } - ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() ctx.Repo.IsViewBranch = true - } else { refName = getRefName(ctx, refType) ctx.Repo.RefName = refName diff --git a/modules/git/command.go b/modules/git/command.go index a42d859f55..ac013d4ea1 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -211,10 +211,18 @@ type RunOpts struct { Env []string Timeout time.Duration UseContextTimeout bool - Dir string - Stdout, Stderr io.Writer - Stdin io.Reader - PipelineFunc func(context.Context, context.CancelFunc) error + + // Dir is the working dir for the git command, however: + // FIXME: this could be incorrect in many cases, for example: + // * /some/path/.git + // * /some/path/.git/gitea-data/data/repositories/user/repo.git + // If "user/repo.git" is invalid/broken, then running git command in it will use "/some/path/.git", and produce unexpected results + // The correct approach is to use `--git-dir" global argument + Dir string + + Stdout, Stderr io.Writer + Stdin io.Reader + PipelineFunc func(context.Context, context.CancelFunc) error } func commonBaseEnvs() []string { diff --git a/modules/git/repo.go b/modules/git/repo.go index d29ec40ae2..3637aa47c4 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -80,7 +80,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error { // IsEmpty Check if repository is empty. func (repo *Repository) IsEmpty() (bool, error) { var errbuf, output strings.Builder - if err := NewCommand(repo.Ctx, "show-ref", "--head", "^HEAD$"). + if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("show-ref", "--head", "^HEAD$"). Run(&RunOpts{ Dir: repo.Path, Stdout: &output, diff --git a/modules/git/repo_base_nogogit.go b/modules/git/repo_base_nogogit.go index a0216d14a6..e0f2d563b3 100644 --- a/modules/git/repo_base_nogogit.go +++ b/modules/git/repo_base_nogogit.go @@ -61,7 +61,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { } repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath) - repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path) + repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath) return repo, nil } diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index 027d13555c..2c493ccf94 100644 --- a/modules/indexer/code/indexer.go +++ b/modules/indexer/code/indexer.go @@ -154,7 +154,9 @@ func Init() { log.Trace("IndexerData Process Repo: %d", indexerData.RepoID) if err := index(ctx, indexer, indexerData.RepoID); err != nil { - log.Error("index: %v", err) + if !setting.IsInTesting { + log.Error("indexer index error for repo %v: %v", indexerData.RepoID, err) + } if indexer.Ping() { continue } diff --git a/modules/indexer/stats/db.go b/modules/indexer/stats/db.go index 9bbdcad60d..2a0475dea6 100644 --- a/modules/indexer/stats/db.go +++ b/modules/indexer/stats/db.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" + "code.gitea.io/gitea/modules/setting" ) // DBIndexer implements Indexer interface to use database's like search @@ -46,7 +47,7 @@ func (db *DBIndexer) Index(id int64) error { // Get latest commit for default branch commitID, err := gitRepo.GetBranchCommitID(repo.DefaultBranch) if err != nil { - if git.IsErrBranchNotExist(err) || git.IsErrNotExist(err) { + if git.IsErrBranchNotExist(err) || git.IsErrNotExist(err) || setting.IsInTesting { log.Debug("Unable to get commit ID for default branch %s in %s ... skipping this repository", repo.DefaultBranch, repo.RepoPath()) return nil } @@ -62,7 +63,9 @@ func (db *DBIndexer) Index(id int64) error { // Calculate and save language statistics to database stats, err := gitRepo.GetLanguageStats(commitID) if err != nil { - log.Error("Unable to get language stats for ID %s for default branch %s in %s. Error: %v", commitID, repo.DefaultBranch, repo.RepoPath(), err) + if !setting.IsInTesting { + log.Error("Unable to get language stats for ID %s for default branch %s in %s. Error: %v", commitID, repo.DefaultBranch, repo.RepoPath(), err) + } return err } err = repo_model.UpdateLanguageStats(repo, commitID, stats) diff --git a/modules/indexer/stats/queue.go b/modules/indexer/stats/queue.go index 32379f2859..a57338e07d 100644 --- a/modules/indexer/stats/queue.go +++ b/modules/indexer/stats/queue.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" + "code.gitea.io/gitea/modules/setting" ) // statsQueue represents a queue to handle repository stats updates @@ -20,7 +21,9 @@ func handle(data ...queue.Data) []queue.Data { for _, datum := range data { opts := datum.(int64) if err := indexer.Index(opts); err != nil { - log.Error("stats queue indexer.Index(%d) failed: %v", opts, err) + if !setting.IsInTesting { + log.Error("stats queue indexer.Index(%d) failed: %v", opts, err) + } } } return nil diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 4d7a7caab8..e1a57615a8 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -27,7 +27,7 @@ import ( var ( // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version. AppVer string - // AppBuiltWith represents a human readable version go runtime build version and build tags. (See main.go formatBuiltWith().) + // AppBuiltWith represents a human-readable version go runtime build version and build tags. (See main.go formatBuiltWith().) AppBuiltWith string // AppStartTime store time gitea has started AppStartTime time.Time @@ -40,7 +40,8 @@ var ( // AppWorkPath is used as the base path for several other paths. AppWorkPath string - // Global setting objects + // Other global setting objects + CfgProvider ConfigProvider CustomPath string // Custom directory path CustomConf string @@ -48,6 +49,10 @@ var ( RunUser string IsProd bool IsWindows bool + + // IsInTesting indicates whether the testing is running. A lot of unreliable code causes a lot of nonsense error logs during testing + // TODO: this is only a temporary solution, we should make the test code more reliable + IsInTesting = false ) func getAppPath() (string, error) { @@ -108,8 +113,12 @@ func getWorkPath(appPath string) string { func init() { IsWindows = runtime.GOOS == "windows" + if AppVer == "" { + AppVer = "dev" + } + // We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically - // By default set this logger at Info - we'll change it later but we need to start with something. + // By default set this logger at Info - we'll change it later, but we need to start with something. log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "info", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout)) var err error |