2020-12-17 22:00:47 +08:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-10 09:27:03 +08:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2020-12-17 22:00:47 +08:00
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2021-06-26 00:54:08 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 22:00:47 +08:00
|
|
|
)
|
|
|
|
|
2024-01-20 00:05:02 +08:00
|
|
|
func init() {
|
|
|
|
isGogit = false
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:00:47 +08:00
|
|
|
// Repository represents a Git repository.
|
|
|
|
type Repository struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
tagCache *ObjectCache
|
|
|
|
|
|
|
|
gpgSettings *GPGSettings
|
2021-05-10 09:27:03 +08:00
|
|
|
|
|
|
|
batchCancel context.CancelFunc
|
|
|
|
batchReader *bufio.Reader
|
|
|
|
batchWriter WriteCloserError
|
|
|
|
|
|
|
|
checkCancel context.CancelFunc
|
|
|
|
checkReader *bufio.Reader
|
|
|
|
checkWriter WriteCloserError
|
2021-12-01 04:06:32 +08:00
|
|
|
|
2022-07-25 23:39:42 +08:00
|
|
|
Ctx context.Context
|
|
|
|
LastCommitCache *LastCommitCache
|
2023-12-14 05:02:00 +08:00
|
|
|
|
|
|
|
objectFormat ObjectFormat
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
|
|
|
|
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
|
|
|
|
return OpenRepository(DefaultContext, repoPath)
|
2021-12-01 04:06:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-30 03:13:41 +08:00
|
|
|
// OpenRepository opens the repository at the given path with the provided context.
|
|
|
|
func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
|
2020-12-17 22:00:47 +08:00
|
|
|
repoPath, err := filepath.Abs(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !isDir(repoPath) {
|
|
|
|
return nil, errors.New("no such file or directory")
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
|
2021-12-17 03:01:14 +08:00
|
|
|
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
|
|
|
|
if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
repo := &Repository{
|
2020-12-17 22:00:47 +08:00
|
|
|
Path: repoPath,
|
|
|
|
tagCache: newObjectCache(),
|
2021-12-01 04:06:32 +08:00
|
|
|
Ctx: ctx,
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 04:06:32 +08:00
|
|
|
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
|
2023-04-19 21:40:42 +08:00
|
|
|
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath)
|
2021-05-10 09:27:03 +08:00
|
|
|
|
2023-12-14 05:02:00 +08:00
|
|
|
repo.objectFormat, err = repo.GetObjectFormat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-10 09:27:03 +08:00
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch obtains a CatFileBatch for this repository
|
2021-12-01 04:06:32 +08:00
|
|
|
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
|
2021-05-10 09:27:03 +08:00
|
|
|
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
|
2021-06-26 00:54:08 +08:00
|
|
|
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
|
2021-12-01 04:06:32 +08:00
|
|
|
return CatFileBatch(ctx, repo.Path)
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
return repo.batchWriter, repo.batchReader, func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
|
2021-12-01 04:06:32 +08:00
|
|
|
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
|
2021-05-10 09:27:03 +08:00
|
|
|
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
|
2021-06-26 00:54:08 +08:00
|
|
|
log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
|
2021-12-01 04:06:32 +08:00
|
|
|
return CatFileBatchCheck(ctx, repo.Path)
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
return repo.checkWriter, repo.checkReader, func() {}
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close this repository, in particular close the underlying gogitStorage if this is not nil
|
2022-01-20 07:26:57 +08:00
|
|
|
func (repo *Repository) Close() (err error) {
|
2021-05-10 09:27:03 +08:00
|
|
|
if repo == nil {
|
2023-07-09 19:58:06 +08:00
|
|
|
return nil
|
2021-05-10 09:27:03 +08:00
|
|
|
}
|
|
|
|
if repo.batchCancel != nil {
|
|
|
|
repo.batchCancel()
|
|
|
|
repo.batchReader = nil
|
|
|
|
repo.batchWriter = nil
|
|
|
|
repo.batchCancel = nil
|
|
|
|
}
|
|
|
|
if repo.checkCancel != nil {
|
|
|
|
repo.checkCancel()
|
|
|
|
repo.checkCancel = nil
|
|
|
|
repo.checkReader = nil
|
|
|
|
repo.checkWriter = nil
|
|
|
|
}
|
2022-07-25 23:39:42 +08:00
|
|
|
repo.LastCommitCache = nil
|
|
|
|
repo.tagCache = nil
|
2022-06-20 18:02:49 +08:00
|
|
|
return err
|
2020-12-17 22:00:47 +08:00
|
|
|
}
|