2020-12-17 09:00:47 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-09 21:27:03 -04:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2020-12-17 09:00:47 -05:00
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2021-06-25 12:54:08 -04:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 09:00:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Repository represents a Git repository.
|
|
|
|
type Repository struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
tagCache *ObjectCache
|
|
|
|
|
|
|
|
gpgSettings *GPGSettings
|
2021-05-09 21:27:03 -04:00
|
|
|
|
|
|
|
batchCancel context.CancelFunc
|
|
|
|
batchReader *bufio.Reader
|
|
|
|
batchWriter WriteCloserError
|
|
|
|
|
|
|
|
checkCancel context.CancelFunc
|
|
|
|
checkReader *bufio.Reader
|
|
|
|
checkWriter WriteCloserError
|
2020-12-17 09:00:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenRepository opens the repository at the given path.
|
|
|
|
func OpenRepository(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")
|
|
|
|
}
|
2021-05-09 21:27:03 -04:00
|
|
|
|
|
|
|
repo := &Repository{
|
2020-12-17 09:00:47 -05:00
|
|
|
Path: repoPath,
|
|
|
|
tagCache: newObjectCache(),
|
2021-05-09 21:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(repoPath)
|
|
|
|
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(repo.Path)
|
|
|
|
|
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch obtains a CatFileBatch for this repository
|
|
|
|
func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) {
|
|
|
|
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
|
2021-06-25 12:54:08 -04:00
|
|
|
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
|
2021-05-09 21:27:03 -04:00
|
|
|
return CatFileBatch(repo.Path)
|
|
|
|
}
|
|
|
|
return repo.batchWriter, repo.batchReader, func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
|
|
|
|
func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) {
|
|
|
|
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
|
2021-06-25 12:54:08 -04:00
|
|
|
log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
|
2021-05-09 21:27:03 -04:00
|
|
|
return CatFileBatchCheck(repo.Path)
|
|
|
|
}
|
|
|
|
return repo.checkWriter, repo.checkReader, func() {}
|
2020-12-17 09:00:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close this repository, in particular close the underlying gogitStorage if this is not nil
|
|
|
|
func (repo *Repository) Close() {
|
2021-05-09 21:27:03 -04:00
|
|
|
if repo == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-12-17 09:00:47 -05:00
|
|
|
}
|