aboutsummaryrefslogtreecommitdiffstats
path: root/modules/context
diff options
context:
space:
mode:
authorAndrey Nering <andrey.nering@gmail.com>2016-08-11 21:07:09 -0300
committer无闻 <u@gogs.io>2016-08-11 17:07:09 -0700
commitdbed39ba05fd498dad6cd3bf127c1f68056bf2ab (patch)
tree5f57a18ecdb444bbf52e583894fc38f2d8d50280 /modules/context
parentaa1fc30b898a928ddafbfd0fddc47547c04f96e5 (diff)
downloadgitea-dbed39ba05fd498dad6cd3bf127c1f68056bf2ab.tar.gz
gitea-dbed39ba05fd498dad6cd3bf127c1f68056bf2ab.zip
On showing diff/file, use the tab_width specified on .editorconfig, if any (#3241)
Closes #3182
Diffstat (limited to 'modules/context')
-rw-r--r--modules/context/repo.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index af18881486..2f6a5de7a1 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -6,10 +6,12 @@ package context
import (
"fmt"
+ "io/ioutil"
"path"
"strings"
"github.com/Unknwon/com"
+ "gopkg.in/editorconfig/editorconfig-core-go.v1"
"gopkg.in/macaron.v1"
"github.com/gogits/git-module"
@@ -69,6 +71,28 @@ func (r *Repository) HasAccess() bool {
return r.AccessMode >= models.ACCESS_MODE_READ
}
+// GetEditorconfig returns the .editorconfig definition if found in the
+// HEAD of the default repo branch.
+func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
+ commit, err := r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch)
+ if err != nil {
+ return nil, err
+ }
+ treeEntry, err := commit.GetTreeEntryByPath(".editorconfig")
+ if err != nil {
+ return nil, err
+ }
+ reader, err := treeEntry.Blob().Data()
+ if err != nil {
+ return nil, err
+ }
+ data, err := ioutil.ReadAll(reader)
+ if err != nil {
+ return nil, err
+ }
+ return editorconfig.ParseBytes(data)
+}
+
func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
// Non-fork repository will not return error in this method.
if err := repo.GetBaseRepo(); err != nil {