Skip to content

Ignore files in diff with check-attr #16262

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,7 @@ diff.stats_desc = <strong> %d changed files</strong> with <strong>%d additions</
diff.stats_desc_file = %d changes: %d additions and %d deletions
diff.bin = BIN
diff.bin_not_shown = Binary file not shown.
diff.generated_not_shown = Generated file not shown.
diff.view_file = View File
diff.file_before = Before
diff.file_after = After
Expand Down
19 changes: 19 additions & 0 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ type DiffFile struct {
IsIncomplete bool
IsIncompleteLineTooLong bool
IsProtected bool
IsGenerated bool
}

// GetType returns type of diff file.
Expand Down Expand Up @@ -1248,13 +1249,31 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
if err != nil {
return nil, fmt.Errorf("ParsePatch: %v", err)
}

var filenames []string
for _, diffFile := range diff.Files {
filenames = append(filenames, diffFile.Name)

tailSection := diffFile.GetTailSection(gitRepo, beforeCommitID, afterCommitID)
if tailSection != nil {
diffFile.Sections = append(diffFile.Sections, tailSection)
}
}

filename2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{
Attributes: []string{"linguist-generated"},
Filenames: filenames,
})
if err != nil {
log.Error("CheckAttribute: %v", err)
}
for _, diffFile := range diff.Files {
attr := filename2attribute2info[diffFile.Name]["linguist-generated"]
if attr == "set" || attr == "true" {
diffFile.IsGenerated = true
}
}

if err = cmd.Wait(); err != nil {
return nil, fmt.Errorf("Wait: %v", err)
}
Expand Down
51 changes: 51 additions & 0 deletions services/repository/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repository

import (
"fmt"
"io"
"os"
"path/filepath"

"code.gitea.io/gitea/modules/git"
)

// SyncGitAttributes copies the content of the .gitattributes file from the default branch into repo.git/info/attributes.
func SyncGitAttributes(gitRepo *git.Repository, sourceBranch string) error {
commit, err := gitRepo.GetBranchCommit(sourceBranch)
if err != nil {
return err
}

attributesBlob, err := commit.GetBlobByPath("/.gitattributes")
if err != nil {
if git.IsErrNotExist(err) {
return nil
}
return err
}

infoPath := filepath.Join(gitRepo.Path, "info")
if err := os.MkdirAll(infoPath, 0700); err != nil {
return fmt.Errorf("Error creating directory [%s]: %v", infoPath, err)
}
attributesPath := filepath.Join(infoPath, "attributes")

attributesFile, err := os.OpenFile(attributesPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Error creating file [%s]: %v", attributesPath, err)
}
defer attributesFile.Close()

blobReader, err := attributesBlob.DataAsync()
if err != nil {
return err
}
defer blobReader.Close()

_, err = io.Copy(attributesFile, blobReader)
return err
}
6 changes: 6 additions & 0 deletions services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
}

if branch == repo.DefaultBranch {
if err := SyncGitAttributes(gitRepo, repo.DefaultBranch); err != nil {
log.Error("SyncGitAttributes for %s failed: %v", repo.ID, err)
}
}

// Cache for big repository
if err := repo_module.CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil {
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
Expand Down
9 changes: 8 additions & 1 deletion templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
{{$blobHead := call $.GetBlobByPathForCommit $.HeadCommit $file.Name}}
{{$isImage := or (call $.IsBlobAnImage $blobBase) (call $.IsBlobAnImage $blobHead)}}
{{$isCsv := (call $.IsCsvFile $file)}}
{{$showFileViewToggle := or $isImage (and (not $file.IsIncomplete) $isCsv)}}
{{$isRenderable := or $isImage (and (not $file.IsIncomplete) $isCsv)}}
{{$showFileViewToggle := or $isRenderable $file.IsGenerated}}
<div class="diff-file-box diff-box file-content {{TabSizeClass $.Editorconfig $file.Name}} mt-3" id="diff-{{.Index}}">
<h4 class="diff-file-header sticky-2nd-row ui top attached normal header df ac sb">
<div class="df ac">
Expand Down Expand Up @@ -111,13 +112,19 @@
</div>
{{if $showFileViewToggle}}
<div id="diff-rendered-{{$i}}" class="file-body file-code {{if $.IsSplitStyle}} code-diff-split{{else}} code-diff-unified{{end}}">
{{if $isRenderable}}
<table class="chroma w-100">
{{if $isImage}}
{{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}}
{{else}}
{{template "repo/diff/csv_diff" dict "file" . "root" $}}
{{end}}
</table>
{{else}}
<div class="diff-file-body binary" style="padding: 5px 10px;">
{{$.i18n.Tr "repo.diff.generated_not_shown"}}
</div>
{{end}}
</div>
{{end}}
</div>
Expand Down