gitech

Форк
0
/
repo_base_nogogit.go 
122 строки · 3.1 Кб
1
// Copyright 2015 The Gogs Authors. All rights reserved.
2
// Copyright 2017 The Gitea Authors. All rights reserved.
3
// SPDX-License-Identifier: MIT
4

5
//go:build !gogit
6

7
package git
8

9
import (
10
	"bufio"
11
	"context"
12
	"path/filepath"
13

14
	"code.gitea.io/gitea/modules/log"
15
	"code.gitea.io/gitea/modules/util"
16
)
17

18
func init() {
19
	isGogit = false
20
}
21

22
// Repository represents a Git repository.
23
type Repository struct {
24
	Path string
25

26
	tagCache *ObjectCache
27

28
	gpgSettings *GPGSettings
29

30
	batchInUse  bool
31
	batchCancel context.CancelFunc
32
	batchReader *bufio.Reader
33
	batchWriter WriteCloserError
34

35
	checkInUse  bool
36
	checkCancel context.CancelFunc
37
	checkReader *bufio.Reader
38
	checkWriter WriteCloserError
39

40
	Ctx             context.Context
41
	LastCommitCache *LastCommitCache
42

43
	objectFormat ObjectFormat
44
}
45

46
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
47
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
48
	return OpenRepository(DefaultContext, repoPath)
49
}
50

51
// OpenRepository opens the repository at the given path with the provided context.
52
func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
53
	repoPath, err := filepath.Abs(repoPath)
54
	if err != nil {
55
		return nil, err
56
	} else if !isDir(repoPath) {
57
		return nil, util.NewNotExistErrorf("no such file or directory")
58
	}
59

60
	// 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!
61
	if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
62
		return nil, err
63
	}
64

65
	repo := &Repository{
66
		Path:     repoPath,
67
		tagCache: newObjectCache(),
68
		Ctx:      ctx,
69
	}
70

71
	repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
72
	repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath)
73

74
	return repo, nil
75
}
76

77
// CatFileBatch obtains a CatFileBatch for this repository
78
func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
79
	if repo.batchCancel == nil || repo.batchInUse {
80
		log.Debug("Opening temporary cat file batch for: %s", repo.Path)
81
		return CatFileBatch(ctx, repo.Path)
82
	}
83
	repo.batchInUse = true
84
	return repo.batchWriter, repo.batchReader, func() {
85
		repo.batchInUse = false
86
	}
87
}
88

89
// CatFileBatchCheck obtains a CatFileBatchCheck for this repository
90
func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
91
	if repo.checkCancel == nil || repo.checkInUse {
92
		log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
93
		return CatFileBatchCheck(ctx, repo.Path)
94
	}
95
	repo.checkInUse = true
96
	return repo.checkWriter, repo.checkReader, func() {
97
		repo.checkInUse = false
98
	}
99
}
100

101
func (repo *Repository) Close() error {
102
	if repo == nil {
103
		return nil
104
	}
105
	if repo.batchCancel != nil {
106
		repo.batchCancel()
107
		repo.batchReader = nil
108
		repo.batchWriter = nil
109
		repo.batchCancel = nil
110
		repo.batchInUse = false
111
	}
112
	if repo.checkCancel != nil {
113
		repo.checkCancel()
114
		repo.checkCancel = nil
115
		repo.checkReader = nil
116
		repo.checkWriter = nil
117
		repo.checkInUse = false
118
	}
119
	repo.LastCommitCache = nil
120
	repo.tagCache = nil
121
	return nil
122
}
123

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.