Skip to content

Commit ad4af2d

Browse files
committed
Merge branch 'lunny/attribute' of github.com:lunny/gitea into lunny/attribute
2 parents 1a52244 + 2391070 commit ad4af2d

File tree

10 files changed

+29
-21
lines changed

10 files changed

+29
-21
lines changed

modules/git/attribute/attribute.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
LinguistDetectable = "linguist-detectable"
1919
LinguistLanguage = "linguist-language"
2020
GitlabLanguage = "gitlab-language"
21+
Lockable = "lockable"
2122
)
2223

2324
var LinguistAttributes = []string{

modules/git/attribute/batch.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ 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) (checker *BatchChecker, returnedErr error) {
30+
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
31+
func NewBatchChecker(repo *git.Repository, treeish string, attributes []string) (checker *BatchChecker, returnedErr error) {
3132
ctx, cancel := context.WithCancel(repo.Ctx)
33+
defer func() {
34+
if returnedErr != nil {
35+
cancel()
36+
}
37+
}()
38+
3239
cmd, envs, cleanup, err := checkAttrCommand(repo, treeish, nil, attributes)
3340
if err != nil {
34-
cancel()
3541
return nil, err
3642
}
43+
defer func() {
44+
if returnedErr != nil {
45+
cleanup()
46+
}
47+
}()
3748

3849
cmd.AddArguments("--stdin")
3950

@@ -47,11 +58,6 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
4758
cleanup()
4859
},
4960
}
50-
defer func() {
51-
if returnedErr != nil {
52-
checker.cancel()
53-
}
54-
}()
5561

5662
stdinReader, stdinWriter, err := os.Pipe()
5763
if err != nil {

modules/git/attribute/batch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func Test_BatchChecker(t *testing.T) {
122122

123123
t.Run("Create index file to run git check-attr", func(t *testing.T) {
124124
defer test.MockVariableValue(&git.DefaultFeatures().SupportCheckAttrOnBare, false)()
125-
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes...)
125+
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
126126
assert.NoError(t, err)
127127
defer checker.Close()
128128
attributes, err := checker.CheckPath("i-am-a-python.p")
@@ -143,7 +143,7 @@ func Test_BatchChecker(t *testing.T) {
143143
assert.NoError(t, err)
144144
defer tempRepo.Close()
145145

146-
checker, err := NewBatchChecker(tempRepo, "", LinguistAttributes...)
146+
checker, err := NewBatchChecker(tempRepo, "", LinguistAttributes)
147147
assert.NoError(t, err)
148148
defer checker.Close()
149149
attributes, err := checker.CheckPath("i-am-a-python.p")
@@ -157,7 +157,7 @@ func Test_BatchChecker(t *testing.T) {
157157
}
158158

159159
t.Run("Run git check-attr in bare repository", func(t *testing.T) {
160-
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes...)
160+
checker, err := NewBatchChecker(gitRepo, commitID, LinguistAttributes)
161161
assert.NoError(t, err)
162162
defer checker.Close()
163163

modules/git/attribute/checker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type CheckAttributeOpts struct {
5454
}
5555

5656
// CheckAttributes return the attributes of the given filenames and attributes in the given treeish.
57+
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
5758
func CheckAttributes(ctx context.Context, gitRepo *git.Repository, treeish string, opts CheckAttributeOpts) (map[string]Attributes, error) {
5859
cmd, envs, cancel, err := checkAttrCommand(gitRepo, treeish, opts.Filenames, opts.Attributes)
5960
if err != nil {

modules/git/languagestats/language_stats_gogit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func GetLanguageStats(repo *git_module.Repository, commitID string) (map[string]
4242
return nil, err
4343
}
4444

45-
checker, err := attribute.NewBatchChecker(repo, commitID)
45+
checker, err := attribute.NewBatchChecker(repo, commitID, attribute.LinguistAttributes)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -69,27 +69,27 @@ func GetLanguageStats(repo *git_module.Repository, commitID string) (map[string]
6969

7070
attrs, err := checker.CheckPath(f.Name)
7171
if err == nil {
72-
isVendored = attrs.HasVendored()
72+
isVendored = attrs.GetVendored()
7373
if isVendored.ValueOrDefault(false) {
7474
return nil
7575
}
7676

77-
isGenerated = attrs.HasGenerated()
77+
isGenerated = attrs.GetGenerated()
7878
if isGenerated.ValueOrDefault(false) {
7979
return nil
8080
}
8181

82-
isDocumentation = attrs.HasDocumentation()
82+
isDocumentation = attrs.GetDocumentation()
8383
if isDocumentation.ValueOrDefault(false) {
8484
return nil
8585
}
8686

87-
isDetectable = attrs.HasDetectable()
87+
isDetectable = attrs.GetDetectable()
8888
if !isDetectable.ValueOrDefault(true) {
8989
return nil
9090
}
9191

92-
hasLanguage := attrs.Language()
92+
hasLanguage := attrs.GetLanguage()
9393
if hasLanguage.Value() != "" {
9494
language := hasLanguage.Value()
9595

modules/git/languagestats/language_stats_nogogit.go

Lines changed: 1 addition & 1 deletion
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, attribute.LinguistAttributes...)
67+
checker, err := attribute.NewBatchChecker(repo, commitID, attribute.LinguistAttributes)
6868
if err != nil {
6969
return nil, err
7070
}

routers/web/repo/setting/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func LFSLocks(ctx *context.Context) {
135135
}
136136
defer gitRepo.Close()
137137

138-
checker, err := attribute.NewBatchChecker(gitRepo, ctx.Repo.Repository.DefaultBranch, "lockable")
138+
checker, err := attribute.NewBatchChecker(gitRepo, ctx.Repo.Repository.DefaultBranch, []string{attribute.Lockable})
139139
if err != nil {
140140
log.Error("Unable to check attributes in %s (%v)", tmpBasePath, err)
141141
ctx.ServerError("LFSLocks", err)

services/gitdiff/gitdiff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOp
12381238
return nil, err
12391239
}
12401240

1241-
checker, err := attribute.NewBatchChecker(gitRepo, opts.AfterCommitID, attribute.LinguistVendored, attribute.LinguistGenerated, attribute.LinguistLanguage, attribute.GitlabLanguage)
1241+
checker, err := attribute.NewBatchChecker(gitRepo, opts.AfterCommitID, []string{attribute.LinguistVendored, attribute.LinguistGenerated, attribute.LinguistLanguage, attribute.GitlabLanguage})
12421242
if err != nil {
12431243
return nil, err
12441244
}

services/repository/files/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
489489
var lfsMetaObject *git_model.LFSMetaObject
490490
if setting.LFS.StartServer && hasOldBranch {
491491
// Check there is no way this can return multiple infos
492-
attributesMap, err := attribute.CheckAttributes(ctx, t.gitRepo, "", attribute.CheckAttributeOpts{
492+
attributesMap, err := attribute.CheckAttributes(ctx, t.gitRepo, "" /* use temp repo's working dir */, attribute.CheckAttributeOpts{
493493
Attributes: []string{"filter"},
494494
Filenames: []string{file.Options.treePath},
495495
})

services/repository/files/upload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
108108

109109
var attributesMap map[string]attribute.Attributes
110110
if setting.LFS.StartServer {
111-
attributesMap, err = attribute.CheckAttributes(ctx, t.gitRepo, "", attribute.CheckAttributeOpts{
111+
attributesMap, err = attribute.CheckAttributes(ctx, t.gitRepo, "" /* use temp repo's working dir */, attribute.CheckAttributeOpts{
112112
Attributes: []string{"filter"},
113113
Filenames: names,
114114
})

0 commit comments

Comments
 (0)