From b5e46e2769f1b0b5eb2b250df95799129818950c Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 25 Jun 2021 15:09:20 +0000 Subject: [PATCH 1/2] Sync .gitattributes to repo.git/info/attributes. --- services/repository/attributes.go | 51 +++++++++++++++++++++++++++++++ services/repository/push.go | 6 ++++ 2 files changed, 57 insertions(+) create mode 100644 services/repository/attributes.go diff --git a/services/repository/attributes.go b/services/repository/attributes.go new file mode 100644 index 0000000000000..3c7f293a9db50 --- /dev/null +++ b/services/repository/attributes.go @@ -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 +} diff --git a/services/repository/push.go b/services/repository/push.go index f031073b2e0e0..3cad6e43f4602 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -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) From a7eaa72f2335b813c3b32cf23308823573efda9f Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Sat, 26 Jun 2021 11:05:39 +0000 Subject: [PATCH 2/2] Exclude files from diff. --- options/locale/locale_en-US.ini | 1 + services/gitdiff/gitdiff.go | 19 +++++++++++++++++++ templates/repo/diff/box.tmpl | 9 ++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 59ee6e48ea7f8..bfed3f2a69770 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1956,6 +1956,7 @@ diff.stats_desc = %d changed files with %d additions

@@ -111,6 +112,7 @@
{{if $showFileViewToggle}}
+ {{if $isRenderable}} {{if $isImage}} {{template "repo/diff/image_diff" dict "file" . "root" $ "blobBase" $blobBase "blobHead" $blobHead}} @@ -118,6 +120,11 @@ {{template "repo/diff/csv_diff" dict "file" . "root" $}} {{end}}
+ {{else}} +
+ {{$.i18n.Tr "repo.diff.generated_not_shown"}} +
+ {{end}}
{{end}}