Skip to content

Commit 9db6749

Browse files
committed
Some improvements
1 parent ed37f3a commit 9db6749

File tree

8 files changed

+33
-54
lines changed

8 files changed

+33
-54
lines changed

modules/git/attribute/attribute.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,27 @@ func (attrs Attributes) Get(name string) Attribute {
6060
return ""
6161
}
6262

63-
func (attrs Attributes) HasVendored() optional.Option[bool] {
63+
func (attrs Attributes) GetVendored() optional.Option[bool] {
6464
return attrs.Get(LinguistVendored).ToBool()
6565
}
6666

67-
func (attrs Attributes) HasGenerated() optional.Option[bool] {
67+
func (attrs Attributes) GetGenerated() optional.Option[bool] {
6868
return attrs.Get(LinguistGenerated).ToBool()
6969
}
7070

71-
func (attrs Attributes) HasDocumentation() optional.Option[bool] {
71+
func (attrs Attributes) GetDocumentation() optional.Option[bool] {
7272
return attrs.Get(LinguistDocumentation).ToBool()
7373
}
7474

75-
func (attrs Attributes) HasDetectable() optional.Option[bool] {
75+
func (attrs Attributes) GetDetectable() optional.Option[bool] {
7676
return attrs.Get(LinguistDetectable).ToBool()
7777
}
7878

79-
func (attrs Attributes) LinguistLanguage() optional.Option[string] {
79+
func (attrs Attributes) GetLinguistLanguage() optional.Option[string] {
8080
return attrs.Get(LinguistLanguage).ToString()
8181
}
8282

83-
func (attrs Attributes) GitlabLanguage() optional.Option[string] {
83+
func (attrs Attributes) GetGitlabLanguage() optional.Option[string] {
8484
attrStr := attrs.Get(GitlabLanguage).ToString()
8585
if attrStr.Has() {
8686
raw := attrStr.Value()
@@ -94,13 +94,13 @@ func (attrs Attributes) GitlabLanguage() optional.Option[string] {
9494
return attrStr
9595
}
9696

97-
func (attrs Attributes) Language() optional.Option[string] {
97+
func (attrs Attributes) GetLanguage() optional.Option[string] {
9898
// prefer linguist-language over gitlab-language
9999
// if linguist-language is not set, use gitlab-language
100100
// if both are not set, return none
101-
language := attrs.LinguistLanguage()
101+
language := attrs.GetLinguistLanguage()
102102
if language.Value() == "" {
103-
language = attrs.GitlabLanguage()
103+
language = attrs.GetGitlabLanguage()
104104
}
105105
return language
106106
}

modules/git/attribute/batch.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,17 @@ type BatchChecker struct {
2727
}
2828

2929
// NewBatchChecker creates a check attribute reader for the current repository and provided commit ID
30-
func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string) (*BatchChecker, error) {
30+
func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string) (checker *BatchChecker, returnedErr error) {
3131
ctx, cancel := context.WithCancel(repo.Ctx)
32-
if len(attributes) == 0 {
33-
attributes = LinguistAttributes
34-
}
3532
cmd, envs, cleanup, err := checkAttrCommand(repo, treeish, nil, attributes)
3633
if err != nil {
3734
cancel()
3835
return nil, err
3936
}
37+
4038
cmd.AddArguments("--stdin")
4139

42-
checker := &BatchChecker{
40+
checker = &BatchChecker{
4341
attributesNum: len(attributes),
4442
repo: repo,
4543
ctx: ctx,
@@ -49,16 +47,20 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
4947
cleanup()
5048
},
5149
}
50+
defer func() {
51+
if returnedErr != nil {
52+
checker.cancel()
53+
}
54+
}()
5255

5356
stdinReader, stdinWriter, err := os.Pipe()
5457
if err != nil {
55-
checker.cancel()
5658
return nil, err
5759
}
5860
checker.stdinWriter = stdinWriter
5961

6062
lw := new(nulSeparatedAttributeWriter)
61-
lw.attributes = make(chan attributeTriple, 5)
63+
lw.attributes = make(chan attributeTriple, len(attributes))
6264
lw.closed = make(chan struct{})
6365
checker.stdOut = lw
6466

@@ -124,10 +126,7 @@ func (c *BatchChecker) CheckPath(path string) (rs Attributes, err error) {
124126
for i := 0; i < c.attributesNum; i++ {
125127
select {
126128
case <-time.After(5 * time.Second):
127-
// There is a strange "hang" problem in gitdiff.GetDiff -> CheckPath
128-
// So add a timeout here to mitigate the problem, and output more logs for debug purpose
129-
// In real world, if CheckPath runs long than seconds, it blocks the end user's operation,
130-
// and at the moment the CheckPath result is not so important, so we can just ignore it.
129+
// there is no "hang" problem now. This code is just used to catch other potential problems.
131130
return nil, reportTimeout()
132131
case attr, ok := <-c.stdOut.ReadAttribute():
133132
if !ok {

modules/git/attribute/checker.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,19 @@ import (
99
"errors"
1010
"fmt"
1111
"os"
12-
"strconv"
1312

1413
"code.gitea.io/gitea/modules/git"
1514
)
1615

1716
func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attributes []string) (*git.Command, []string, func(), error) {
1817
cancel := func() {}
1918
envs := []string{"GIT_FLUSH=1"}
20-
21-
// Check if the repository is bare
22-
res, _, err := git.NewCommand("rev-parse", "--is-bare-repository").RunStdString(gitRepo.Ctx, &git.RunOpts{
23-
Dir: gitRepo.Path,
24-
})
25-
if err != nil {
26-
return nil, nil, nil, fmt.Errorf("failed to run rev-parse: %w", err)
27-
}
28-
isBare, _ := strconv.ParseBool(res)
29-
// bare repository must have a treeish
30-
if isBare && treeish == "" {
31-
return nil, nil, nil, errors.New("bare repository must have a treeish")
32-
}
33-
3419
cmd := git.NewCommand("check-attr", "-z")
3520
if len(attributes) == 0 {
3621
cmd.AddArguments("--all")
3722
}
3823

24+
// there is treeish, read from source or index
3925
if treeish != "" {
4026
if git.DefaultFeatures().SupportCheckAttrOnBare {
4127
cmd.AddArguments("--source")
@@ -53,7 +39,7 @@ func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attrib
5339
)
5440
cancel = deleteTemporaryFile
5541
}
56-
}
42+
} // no treeish, read from working directory
5743

5844
cmd.AddDynamicArguments(attributes...)
5945
if len(filenames) > 0 {

modules/git/languagestats/language_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ func GetFileLanguage(ctx context.Context, gitRepo *git.Repository, treeish, tree
6161
return "", err
6262
}
6363

64-
return attributesMap[treePath].Language().Value(), nil
64+
return attributesMap[treePath].GetLanguage().Value(), nil
6565
}

modules/git/languagestats/language_stats_nogogit.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func GetLanguageStats(repo *git.Repository, commitID string) (map[string]int64,
6464
return nil, err
6565
}
6666

67-
checker, err := attribute.NewBatchChecker(repo, commitID)
67+
checker, err := attribute.NewBatchChecker(repo, commitID, attribute.LinguistAttributes...)
6868
if err != nil {
6969
return nil, err
7070
}
@@ -103,28 +103,23 @@ func GetLanguageStats(repo *git.Repository, commitID string) (map[string]int64,
103103

104104
attrs, err := checker.CheckPath(f.Name())
105105
if err == nil {
106-
isVendored = attrs.HasVendored()
107-
if isVendored.ValueOrDefault(false) {
106+
if isVendored = attrs.GetVendored(); isVendored.ValueOrDefault(false) {
108107
continue
109108
}
110109

111-
isGenerated = attrs.HasGenerated()
112-
if isGenerated.ValueOrDefault(false) {
110+
if isGenerated = attrs.GetGenerated(); isGenerated.ValueOrDefault(false) {
113111
continue
114112
}
115113

116-
isDocumentation = attrs.HasDocumentation()
117-
if isDocumentation.ValueOrDefault(false) {
114+
if isDocumentation = attrs.GetDocumentation(); isDocumentation.ValueOrDefault(false) {
118115
continue
119116
}
120117

121-
isDetectable = attrs.HasDetectable()
122-
if !isDetectable.ValueOrDefault(true) {
118+
if isDetectable = attrs.GetDetectable(); !isDetectable.ValueOrDefault(true) {
123119
continue
124120
}
125121

126-
hasLanguage := attrs.Language()
127-
if hasLanguage.Value() != "" {
122+
if hasLanguage := attrs.GetLanguage(); hasLanguage.Value() != "" {
128123
language := hasLanguage.Value()
129124

130125
// group languages, such as Pug -> HTML; SCSS -> CSS

routers/web/repo/setting/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func LFSLocks(ctx *context.Context) {
152152
log.Error("Unable to check attributes in %s: %s (%v)", tmpBasePath, lock.Path, err)
153153
continue
154154
}
155-
lockables[i] = attrs["lockable"] == "set"
155+
lockables[i] = attrs["lockable"].ToBool().Value()
156156
}
157157
ctx.Data["Lockables"] = lockables
158158

routers/web/repo/view_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
294294

295295
attrs, err := checker.CheckPath(ctx.Repo.TreePath)
296296
if err == nil {
297-
ctx.Data["IsVendored"] = attrs.HasVendored().Value()
298-
ctx.Data["IsGenerated"] = attrs.HasGenerated().Value()
297+
ctx.Data["IsVendored"] = attrs.GetVendored().Value()
298+
ctx.Data["IsGenerated"] = attrs.GetGenerated().Value()
299299
}
300300
}
301301

services/gitdiff/gitdiff.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,9 +1249,8 @@ func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOp
12491249
isGenerated := optional.None[bool]()
12501250
attrs, err := checker.CheckPath(diffFile.Name)
12511251
if err == nil {
1252-
isVendored = attrs.HasVendored()
1253-
isGenerated = attrs.HasGenerated()
1254-
language := attrs.Language()
1252+
isVendored, isGenerated = attrs.GetVendored(), attrs.GetGenerated()
1253+
language := attrs.GetLanguage()
12551254
if language.Has() {
12561255
diffFile.Language = language.Value()
12571256
}

0 commit comments

Comments
 (0)