2020-12-17 07:00:47 -07:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 07:00:47 -07:00
|
|
|
|
2021-08-24 10:47:09 -06:00
|
|
|
//go:build !gogit
|
2020-12-17 07:00:47 -07:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-05-09 19:27:03 -06:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2020-12-17 07:00:47 -07:00
|
|
|
"path/filepath"
|
2021-06-25 10:54:08 -06:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-05 23:26:32 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-12-17 07:00:47 -07:00
|
|
|
)
|
|
|
|
|
2024-05-06 10:34:16 -06:00
|
|
|
const isGogit = false
|
2024-01-19 09:05:02 -07:00
|
|
|
|
2020-12-17 07:00:47 -07:00
|
|
|
// Repository represents a Git repository.
|
|
|
|
type Repository struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
tagCache *ObjectCache
|
|
|
|
|
|
|
|
gpgSettings *GPGSettings
|
2021-05-09 19:27:03 -06:00
|
|
|
|
2024-08-20 11:04:57 -06:00
|
|
|
batchInUse bool
|
|
|
|
batch *Batch
|
2021-05-09 19:27:03 -06:00
|
|
|
|
2024-08-20 11:04:57 -06:00
|
|
|
checkInUse bool
|
|
|
|
check *Batch
|
2021-11-30 13:06:32 -07:00
|
|
|
|
2022-07-25 09:39:42 -06:00
|
|
|
Ctx context.Context
|
|
|
|
LastCommitCache *LastCommitCache
|
2023-12-13 14:02:00 -07:00
|
|
|
|
|
|
|
objectFormat ObjectFormat
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|
|
|
|
|
2022-03-29 13:13:41 -06:00
|
|
|
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
|
|
|
|
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
|
|
|
|
return OpenRepository(DefaultContext, repoPath)
|
2021-11-30 13:06:32 -07:00
|
|
|
}
|
|
|
|
|
2022-03-29 13:13:41 -06: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 07:00:47 -07:00
|
|
|
repoPath, err := filepath.Abs(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !isDir(repoPath) {
|
2024-03-05 23:26:32 -07:00
|
|
|
return nil, util.NewNotExistErrorf("no such file or directory")
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
|
2024-08-20 11:04:57 -06:00
|
|
|
return &Repository{
|
2020-12-17 07:00:47 -07:00
|
|
|
Path: repoPath,
|
|
|
|
tagCache: newObjectCache(),
|
2021-11-30 13:06:32 -07:00
|
|
|
Ctx: ctx,
|
2024-08-20 11:04:57 -06:00
|
|
|
}, nil
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatch obtains a CatFileBatch for this repository
|
2024-08-20 11:04:57 -06: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
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2024-08-20 11:04:57 -06:00
|
|
|
|
|
|
|
if !repo.batchInUse {
|
|
|
|
repo.batchInUse = true
|
|
|
|
return repo.batch.Writer, repo.batch.Reader, func() {
|
|
|
|
repo.batchInUse = false
|
|
|
|
}, nil
|
Prevent double use of `git cat-file` session. (#29298)
Fixes the reason why #29101 is hard to replicate.
Related #29297
Create a repo with a file with minimum size 4097 bytes (I use 10000) and
execute the following code:
```go
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, <repo>)
assert.NoError(t, err)
commit, err := gitRepo.GetCommit(<sha>)
assert.NoError(t, err)
entry, err := commit.GetTreeEntryByPath(<file>)
assert.NoError(t, err)
b := entry.Blob()
// Create a reader
r, err := b.DataAsync()
assert.NoError(t, err)
defer r.Close()
// Create a second reader
r2, err := b.DataAsync()
assert.NoError(t, err) // Should be no error but is ErrNotExist
defer r2.Close()
```
The problem is the check in `CatFileBatch`:
https://github.com/go-gitea/gitea/blob/79217ea63c1f77de7ca79813ae45950724e63d02/modules/git/repo_base_nogogit.go#L81-L87
`Buffered() > 0` is used to check if there is a "operation" in progress
at the moment. This is a problem because we can't control the internal
buffer in the `bufio.Reader`. The code above demonstrates a sequence
which initiates an operation for which the code thinks there is no
active processing. The second call to `DataAsync()` therefore reuses the
existing instances instead of creating a new batch reader.
2024-02-21 11:54:17 -07:00
|
|
|
}
|
2024-08-20 11:04:57 -06: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
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
|
2024-08-20 11:04:57 -06: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
|
|
|
|
}
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2024-08-20 11:04:57 -06: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
|
Prevent double use of `git cat-file` session. (#29298)
Fixes the reason why #29101 is hard to replicate.
Related #29297
Create a repo with a file with minimum size 4097 bytes (I use 10000) and
execute the following code:
```go
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, <repo>)
assert.NoError(t, err)
commit, err := gitRepo.GetCommit(<sha>)
assert.NoError(t, err)
entry, err := commit.GetTreeEntryByPath(<file>)
assert.NoError(t, err)
b := entry.Blob()
// Create a reader
r, err := b.DataAsync()
assert.NoError(t, err)
defer r.Close()
// Create a second reader
r2, err := b.DataAsync()
assert.NoError(t, err) // Should be no error but is ErrNotExist
defer r2.Close()
```
The problem is the check in `CatFileBatch`:
https://github.com/go-gitea/gitea/blob/79217ea63c1f77de7ca79813ae45950724e63d02/modules/git/repo_base_nogogit.go#L81-L87
`Buffered() > 0` is used to check if there is a "operation" in progress
at the moment. This is a problem because we can't control the internal
buffer in the `bufio.Reader`. The code above demonstrates a sequence
which initiates an operation for which the code thinks there is no
active processing. The second call to `DataAsync()` therefore reuses the
existing instances instead of creating a new batch reader.
2024-02-21 11:54:17 -07:00
|
|
|
}
|
2024-08-20 11:04:57 -06:00
|
|
|
return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:05:23 -07:00
|
|
|
func (repo *Repository) Close() error {
|
2021-05-09 19:27:03 -06:00
|
|
|
if repo == nil {
|
2023-07-09 05:58:06 -06:00
|
|
|
return nil
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2024-08-20 11:04:57 -06:00
|
|
|
if repo.batch != nil {
|
|
|
|
repo.batch.Close()
|
|
|
|
repo.batch = nil
|
Prevent double use of `git cat-file` session. (#29298)
Fixes the reason why #29101 is hard to replicate.
Related #29297
Create a repo with a file with minimum size 4097 bytes (I use 10000) and
execute the following code:
```go
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, <repo>)
assert.NoError(t, err)
commit, err := gitRepo.GetCommit(<sha>)
assert.NoError(t, err)
entry, err := commit.GetTreeEntryByPath(<file>)
assert.NoError(t, err)
b := entry.Blob()
// Create a reader
r, err := b.DataAsync()
assert.NoError(t, err)
defer r.Close()
// Create a second reader
r2, err := b.DataAsync()
assert.NoError(t, err) // Should be no error but is ErrNotExist
defer r2.Close()
```
The problem is the check in `CatFileBatch`:
https://github.com/go-gitea/gitea/blob/79217ea63c1f77de7ca79813ae45950724e63d02/modules/git/repo_base_nogogit.go#L81-L87
`Buffered() > 0` is used to check if there is a "operation" in progress
at the moment. This is a problem because we can't control the internal
buffer in the `bufio.Reader`. The code above demonstrates a sequence
which initiates an operation for which the code thinks there is no
active processing. The second call to `DataAsync()` therefore reuses the
existing instances instead of creating a new batch reader.
2024-02-21 11:54:17 -07:00
|
|
|
repo.batchInUse = false
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2024-08-20 11:04:57 -06:00
|
|
|
if repo.check != nil {
|
|
|
|
repo.check.Close()
|
|
|
|
repo.check = nil
|
Prevent double use of `git cat-file` session. (#29298)
Fixes the reason why #29101 is hard to replicate.
Related #29297
Create a repo with a file with minimum size 4097 bytes (I use 10000) and
execute the following code:
```go
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, <repo>)
assert.NoError(t, err)
commit, err := gitRepo.GetCommit(<sha>)
assert.NoError(t, err)
entry, err := commit.GetTreeEntryByPath(<file>)
assert.NoError(t, err)
b := entry.Blob()
// Create a reader
r, err := b.DataAsync()
assert.NoError(t, err)
defer r.Close()
// Create a second reader
r2, err := b.DataAsync()
assert.NoError(t, err) // Should be no error but is ErrNotExist
defer r2.Close()
```
The problem is the check in `CatFileBatch`:
https://github.com/go-gitea/gitea/blob/79217ea63c1f77de7ca79813ae45950724e63d02/modules/git/repo_base_nogogit.go#L81-L87
`Buffered() > 0` is used to check if there is a "operation" in progress
at the moment. This is a problem because we can't control the internal
buffer in the `bufio.Reader`. The code above demonstrates a sequence
which initiates an operation for which the code thinks there is no
active processing. The second call to `DataAsync()` therefore reuses the
existing instances instead of creating a new batch reader.
2024-02-21 11:54:17 -07:00
|
|
|
repo.checkInUse = false
|
2021-05-09 19:27:03 -06:00
|
|
|
}
|
2022-07-25 09:39:42 -06:00
|
|
|
repo.LastCommitCache = nil
|
|
|
|
repo.tagCache = nil
|
2024-02-25 06:05:23 -07:00
|
|
|
return nil
|
2020-12-17 07:00:47 -07:00
|
|
|
}
|