2020-12-17 19:30:47 +05:30
|
|
|
// 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.
|
|
|
|
|
2021-08-24 22:17:09 +05:30
|
|
|
//go:build !gogit
|
2020-12-17 19:30:47 +05:30
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-10 06:57:03 +05:30
|
|
|
"bufio"
|
|
|
|
"context"
|
2020-12-17 19:30:47 +05:30
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2021-06-25 22:24:08 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 19:30:47 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Repository represents a Git repository.
|
|
|
|
type Repository struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
tagCache *ObjectCache
|
|
|
|
|
|
|
|
gpgSettings *GPGSettings
|
2021-05-10 06:57:03 +05:30
|
|
|
|
|
|
|
batchCancel context.CancelFunc
|
|
|
|
batchReader *bufio.Reader
|
|
|
|
batchWriter WriteCloserError
|
|
|
|
|
|
|
|
checkCancel context.CancelFunc
|
|
|
|
checkReader *bufio.Reader
|
|
|
|
checkWriter WriteCloserError
|
2021-12-01 01:36:32 +05:30
|
|
|
|
|
|
|
Ctx context.Context
|
2020-12-17 19:30:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// OpenRepository opens the repository at the given path.
|
|
|
|
func OpenRepository(repoPath string) (*Repository, error) {
|
2021-12-01 01:36:32 +05:30
|
|
|
return OpenRepositoryCtx(DefaultContext, repoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenRepositoryCtx opens the repository at the given path with the provided context.
|
|
|
|
func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error) {
|
2020-12-17 19:30:47 +05:30
|
|
|
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 06:57:03 +05:30
|
|
|
|
2021-12-17 00:31:14 +05:30
|
|
|
// 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 06:57:03 +05:30
|
|
|
repo := &Repository{
|
2020-12-17 19:30:47 +05:30
|
|
|
Path: repoPath,
|
|
|
|
tagCache: newObjectCache(),
|
2021-12-01 01:36:32 +05:30
|
|
|
Ctx: ctx,
|
2021-05-10 06:57:03 +05:30
|
|
|
}
|
|
|
|
|
2021-12-01 01:36:32 +05:30
|
|
|
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
|
|
|
|
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
|
2021-05-10 06:57:03 +05:30
|
|
|
|
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch obtains a CatFileBatch for this repository
|
2021-12-01 01:36:32 +05:30
|
|
|
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
|
2021-05-10 06:57:03 +05:30
|
|
|
if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
|
2021-06-25 22:24:08 +05:30
|
|
|
log.Debug("Opening temporary cat file batch for: %s", repo.Path)
|
2021-12-01 01:36:32 +05:30
|
|
|
return CatFileBatch(ctx, repo.Path)
|
2021-05-10 06:57:03 +05:30
|
|
|
}
|
|
|
|
return repo.batchWriter, repo.batchReader, func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
|
2021-12-01 01:36:32 +05:30
|
|
|
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
|
2021-05-10 06:57:03 +05:30
|
|
|
if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
|
2021-06-25 22:24:08 +05:30
|
|
|
log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
|
2021-12-01 01:36:32 +05:30
|
|
|
return CatFileBatchCheck(ctx, repo.Path)
|
2021-05-10 06:57:03 +05:30
|
|
|
}
|
|
|
|
return repo.checkWriter, repo.checkReader, func() {}
|
2020-12-17 19:30:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Close this repository, in particular close the underlying gogitStorage if this is not nil
|
2022-01-20 04:56:57 +05:30
|
|
|
func (repo *Repository) Close() (err error) {
|
2021-05-10 06:57:03 +05:30
|
|
|
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
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
return
|
2020-12-17 19:30:47 +05:30
|
|
|
}
|