8000 Refactor Git Attribute & performance optimization by lunny · Pull Request #34154 · go-gitea/gitea · GitHub
[go: up one dir, main page]

Skip to content

Refactor Git Attribute & performance optimization #34154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fine tune
  • Loading branch information
wxiaoguang committed Apr 11, 2025
commit d6f138b42b04d2b4aba22497eb0eafe16aa95650
1 change: 1 addition & 0 deletions modules/git/attribute/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
LinguistDetectable = "linguist-detectable"
LinguistLanguage = "linguist-language"
GitlabLanguage = "gitlab-language"
Lockable = "lockable"
)

var LinguistAttributes = []string{
Expand Down
19 changes: 12 additions & 7 deletions modules/git/attribute/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ type BatchChecker struct {
}

// NewBatchChecker creates a check attribute reader for the current repository and provided commit ID
func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string) (checker *BatchChecker, returnedErr error) {
func NewBatchChecker(repo *git.Repository, treeish string, attributes []string) (checker *BatchChecker, returnedErr error) {
ctx, cancel := context.WithCancel(repo.Ctx)
defer func() {
if returnedErr != nil {
cancel()
}
}()

cmd, envs, cleanup, err := checkAttrCommand(repo, treeish, nil, attributes)
if err != nil {
cancel()
return nil, err
}
defer func() {
if returnedErr != nil {
cleanup()
}
}()

cmd.AddArguments("--stdin")

Expand All @@ -47,11 +57,6 @@ func NewBatchChecker(repo *git.Repository, treeish string, attributes ...string)
cleanup()
},
}
defer func() {
if returnedErr != nil {
checker.cancel()
}
}()

stdinReader, stdinWriter, err := os.Pipe()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions modules/git/attribute/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Test_BatchChecker(t *testing.T) {

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions modules/git/languagestats/language_stats_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func GetLanguageStats(repo *git_module.Repository, commitID string) (map[string]
return nil, err
}

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

attrs, err := checker.CheckPath(f.Name)
if err == nil {
isVendored = attrs.HasVendored()
isVendored = attrs.GetVendored()
if isVendored.ValueOrDefault(false) {
return nil
}

isGenerated = attrs.HasGenerated()
isGenerated = attrs.GetGenerated()
if isGenerated.ValueOrDefault(false) {
return nil
}

isDocumentation = attrs.HasDocumentation()
isDocumentation = attrs.GetDocumentation()
if isDocumentation.ValueOrDefault(false) {
return nil
}

isDetectable = attrs.HasDetectable()
isDetectable = attrs.GetDetectable()
if !isDetectable.ValueOrDefault(true) {
return nil
}

hasLanguage := attrs.Language()
hasLanguage := attrs.GetLanguage()
if hasLanguage.Value() != "" {
language := hasLanguage.Value()

Expand Down
2 changes: 1 addition & 1 deletion modules/git/languagestats/language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func GetLanguageStats(repo *git.Repository, commitID string) (map[string]int64,
return nil, err
}

checker, err := attribute.NewBatchChecker(repo, commitID, attribute.LinguistAttributes...)
checker, err := attribute.NewBatchChecker(repo, commitID, attribute.LinguistAttributes)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/setting/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func LFSLocks(ctx *context.Context) {
}
defer gitRepo.Close()

checker, err := attribute.NewBatchChecker(gitRepo, ctx.Repo.Repository.DefaultBranch, "lockable")
checker, err := attribute.NewBatchChecker(gitRepo, ctx.Repo.Repository.DefaultBranch, []string{attribute.Lockable})
if err != nil {
log.Error("Unable to check attributes in %s (%v)", tmpBasePath, err)
ctx.ServerError("LFSLocks", err)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
}

if ctx.Repo.GitRepo != nil {
checker, err := attribute.NewBatchChecker(ctx.Repo.GitRepo, ctx.Repo.CommitID, attribute.LinguistGenerated, attribute.LinguistVendored)
checker, err := attribute.NewBatchChecker(ctx.Repo.GitRepo, ctx.Repo.CommitID, []string{attribute.LinguistGenerated, attribute.LinguistVendored})
if err != nil {
ctx.ServerError("NewAttributeChecker", err)
return
Expand Down
2 changes: 1 addition & 1 deletion services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOp
return nil, err
}

checker, err := attribute.NewBatchChecker(gitRepo, opts.AfterCommitID, attribute.LinguistVendored, attribute.LinguistGenerated, attribute.LinguistLanguage, attribute.GitlabLanguage)
checker, err := attribute.NewBatchChecker(gitRepo, opts.AfterCommitID, []string{attribute.LinguistVendored, attribute.LinguistGenerated, attribute.LinguistLanguage, attribute.GitlabLanguage})
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
var lfsMetaObject *git_model.LFSMetaObject
if setting.LFS.StartServer && hasOldBranch {
// Check there is no way this can return multiple infos
attributesMap, err := attribute.CheckAttributes(ctx, t.gitRepo, "", attribute.CheckAttributeOpts{
attributesMap, err := attribute.CheckAttributes(ctx, t.gitRepo, "" /* use temp repo's working dir */, attribute.CheckAttributeOpts{
Attributes: []string{"filter"},
Filenames: []string{file.Options.treePath},
})
Expand Down
2 changes: 1 addition & 1 deletion services/repository/files/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use

var attributesMap map[string]attribute.Attributes
if setting.LFS.StartServer {
attributesMap, err = attribute.CheckAttributes(ctx, t.gitRepo, "", attribute.CheckAttributeOpts{
attributesMap, err = attribute.CheckAttributes(ctx, t.gitRepo, "" /* use temp repo's working dir */, attribute.CheckAttributeOpts{
Attributes: []string{"filter"},
Filenames: names,
})
Expand Down
Loading
0