aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2024-02-24 19:46:49 +0100
committerGitHub <noreply@github.com>2024-02-24 18:46:49 +0000
commit4197e2810081025a6614624e7b1731af91c8db72 (patch)
treefe1f0ca25194316bd446a95dd5946fd29fd7208a /services
parent98ab9445d1020c515c3c789f0b27d952903a2978 (diff)
downloadgitea-4197e2810081025a6614624e7b1731af91c8db72.tar.gz
gitea-4197e2810081025a6614624e7b1731af91c8db72.zip
Refactor git attributes (#29356)
Diffstat (limited to 'services')
-rw-r--r--services/gitdiff/gitdiff.go42
-rw-r--r--services/repository/files/content.go12
2 files changed, 19 insertions, 35 deletions
diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go
index 0f6e2b6c17..740c748347 100644
--- a/services/gitdiff/gitdiff.go
+++ b/services/gitdiff/gitdiff.go
@@ -29,6 +29,7 @@ import (
"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
@@ -1181,41 +1182,30 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
for _, diffFile := range diff.Files {
- gotVendor := false
- gotGenerated := false
+ isVendored := optional.None[bool]()
+ isGenerated := optional.None[bool]()
if checker != nil {
attrs, err := checker.CheckPath(diffFile.Name)
if err == nil {
- if vendored, has := attrs["linguist-vendored"]; has {
- if vendored == "set" || vendored == "true" {
- diffFile.IsVendored = true
- gotVendor = true
- } else {
- gotVendor = vendored == "false"
- }
- }
- if generated, has := attrs["linguist-generated"]; has {
- if generated == "set" || generated == "true" {
- diffFile.IsGenerated = true
- gotGenerated = true
- } else {
- gotGenerated = generated == "false"
- }
- }
- if language, has := attrs["linguist-language"]; has && language != "unspecified" && language != "" {
- diffFile.Language = language
- } else if language, has := attrs["gitlab-language"]; has && language != "unspecified" && language != "" {
- diffFile.Language = language
+ isVendored = git.AttributeToBool(attrs, git.AttributeLinguistVendored)
+ isGenerated = git.AttributeToBool(attrs, git.AttributeLinguistGenerated)
+
+ language := git.TryReadLanguageAttribute(attrs)
+ if language.Has() {
+ diffFile.Language = language.Value()
}
}
}
- if !gotVendor {
- diffFile.IsVendored = analyze.IsVendor(diffFile.Name)
+ if !isVendored.Has() {
+ isVendored = optional.Some(analyze.IsVendor(diffFile.Name))
}
- if !gotGenerated {
- diffFile.IsGenerated = analyze.IsGenerated(diffFile.Name)
+ diffFile.IsVendored = isVendored.Value()
+
+ if !isGenerated.Has() {
+ isGenerated = optional.Some(analyze.IsGenerated(diffFile.Name))
}
+ diffFile.IsGenerated = isGenerated.Value()
tailSection := diffFile.GetTailSection(gitRepo, opts.BeforeCommitID, opts.AfterCommitID)
if tailSection != nil {
diff --git a/services/repository/files/content.go b/services/repository/files/content.go
index f2a7677688..9500b8f46d 100644
--- a/services/repository/files/content.go
+++ b/services/repository/files/content.go
@@ -282,7 +282,7 @@ func TryGetContentLanguage(gitRepo *git.Repository, commitID, treePath string) (
filename2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{
CachedOnly: true,
- Attributes: []string{"linguist-language", "gitlab-language"},
+ Attributes: []string{git.AttributeLinguistLanguage, git.AttributeGitlabLanguage},
Filenames: []string{treePath},
IndexFile: indexFilename,
WorkTree: worktree,
@@ -291,13 +291,7 @@ func TryGetContentLanguage(gitRepo *git.Repository, commitID, treePath string) (
return "", err
}
- language := filename2attribute2info[treePath]["linguist-language"]
- if language == "" || language == "unspecified" {
- language = filename2attribute2info[treePath]["gitlab-language"]
- }
- if language == "unspecified" {
- language = ""
- }
+ language := git.TryReadLanguageAttribute(filename2attribute2info[treePath])
- return language, nil
+ return language.Value(), nil
}