2024-08-12 17:16:55 +02:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-20 00:26:57 +01:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
2024-08-12 17:16:55 +02:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Repository represents a Git repository.
|
|
|
|
type Repository struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
tagCache *ObjectCache
|
|
|
|
|
|
|
|
gpgSettings *GPGSettings
|
|
|
|
|
2024-08-20 19:04:57 +02:00
|
|
|
batchInUse bool
|
|
|
|
batch *Batch
|
2024-08-12 17:16:55 +02:00
|
|
|
|
2024-08-20 19:04:57 +02:00
|
|
|
checkInUse bool
|
|
|
|
check *Batch
|
2024-08-12 17:16:55 +02:00
|
|
|
|
|
|
|
Ctx context.Context
|
|
|
|
LastCommitCache *LastCommitCache
|
|
|
|
|
|
|
|
objectFormat ObjectFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
|
|
|
|
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
|
|
|
|
return OpenRepository(DefaultContext, repoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenRepository opens the repository at the given path with the provided context.
|
|
|
|
func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
|
|
|
|
repoPath, err := filepath.Abs(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !isDir(repoPath) {
|
|
|
|
return nil, errors.New("no such file or directory")
|
|
|
|
}
|
|
|
|
|
2024-08-20 19:04:57 +02:00
|
|
|
return &Repository{
|
2024-08-12 17:16:55 +02:00
|
|
|
Path: repoPath,
|
|
|
|
tagCache: newObjectCache(),
|
|
|
|
Ctx: ctx,
|
2024-08-20 19:04:57 +02:00
|
|
|
}, nil
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch obtains a CatFileBatch for this repository
|
2024-08-20 19:04:57 +02:00
|
|
|
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
|
|
|
|
if repo.batch == nil {
|
|
|
|
var err error
|
|
|
|
repo.batch, err = repo.NewBatch(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
|
|
|
|
if !repo.batchInUse {
|
|
|
|
repo.batchInUse = true
|
|
|
|
return repo.batch.Writer, repo.batch.Reader, func() {
|
|
|
|
repo.batchInUse = false
|
|
|
|
}, nil
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
|
|
|
|
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
|
|
|
|
tempBatch, err := repo.NewBatch(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
return tempBatch.Writer, tempBatch.Reader, tempBatch.Close, nil
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
|
2024-08-20 19:04:57 +02:00
|
|
|
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
|
|
|
|
if repo.check == nil {
|
|
|
|
var err error
|
|
|
|
repo.check, err = repo.NewBatchCheck(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
|
|
|
|
if !repo.checkInUse {
|
|
|
|
repo.checkInUse = true
|
|
|
|
return repo.check.Writer, repo.check.Reader, func() {
|
|
|
|
repo.checkInUse = false
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
|
|
|
|
tempBatchCheck, err := repo.NewBatchCheck(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil
|
2024-08-12 17:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) Close() error {
|
|
|
|
if repo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
if repo.batch != nil {
|
|
|
|
repo.batch.Close()
|
|
|
|
repo.batch = nil
|
2024-08-12 17:16:55 +02:00
|
|
|
repo.batchInUse = false
|
|
|
|
}
|
2024-08-20 19:04:57 +02:00
|
|
|
if repo.check != nil {
|
|
|
|
repo.check.Close()
|
|
|
|
repo.check = nil
|
2024-08-12 17:16:55 +02:00
|
|
|
repo.checkInUse = false
|
|
|
|
}
|
|
|
|
repo.LastCommitCache = nil
|
|
|
|
repo.tagCache = nil
|
|
|
|
return nil
|
|
|
|
}
|