Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pending changes
Browse files Browse the repository at this point in the history
dpaz committed Jul 13, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 810df0a commit 590817f
Showing 5 changed files with 12 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -147,7 +147,6 @@ Add a `.gitattributes` file to the directory and use the same matchers that you

#### Vendored code


Use the `linguist-vendored` attribute to vendor or un-vendor paths.

```
1 change: 1 addition & 0 deletions cli/enry/main.go
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ func main() {
log.Println(err)
return nil
}

language := gitAttributes.GetLanguage(filepath.Base(path))
if len(language) == 0 {
language = enry.GetLanguage(filepath.Base(path), content)
5 changes: 3 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
@@ -414,8 +414,9 @@ func GetLanguagesBySpecificClassifier(content []byte, candidates []string, class
return classifier.Classify(content, mapCandidates)
}

// GetLanguagesByGitAttributes returns either a string slice with the language if the filename matches with a regExp in .gitattributes
//or returns a empty slice in case no regexp matches the filename. It complies with the signature to be a Strategy type.
// GetLanguagesByGitAttributes returns either a string slice with the language
// if the filename matches with a regExp in .gitattributes or returns a empty slice
// in case no regExp matches the filename. It complies with the signature to be a Strategy type.
func GetLanguagesByGitAttributes(filename string, content []byte, candidates []string) []string {
gitAttributes := NewGitAttributes()
reader, err := os.Open(".gitattributes")
28 changes: 8 additions & 20 deletions gitattributes.go
Original file line number Diff line number Diff line change
@@ -7,8 +7,6 @@ import (
"io/ioutil"
"regexp"
"strings"

"gopkg.in/src-d/enry.v1/data"
)

type attrType int
@@ -28,6 +26,7 @@ func (i attrType) String() string {
if i < 0 || i >= attrType(len(attrTypeIndex)-1) {
return fmt.Sprintf("attrType(%d)", i)
}

return attrTypeName[attrTypeIndex[i]:attrTypeIndex[i+1]]
}

@@ -42,7 +41,8 @@ type regExpAttribute struct {
attributes map[*regexp.Regexp]string
}

// GitAttributes is a struct that contains two maps, boolAttributes contains all the attributes that works like a boolean condition,
// GitAttributes is a struct that contains two maps:
// boolAttributes contains all the attributes that works like a boolean condition,
// regExpAttributes contains all the attributes that match a regExp to choose if an attribute is applied or not
type GitAttributes struct {
boolAttributes map[attrType]boolAttribute
@@ -64,7 +64,7 @@ func (gitAttrs *GitAttributes) IsVendor(path string) bool {
return val
}

return data.VendorMatchers.Match(path)
return IsVendor(path)
}

// Returns whether or not path is a documentation path.
@@ -73,7 +73,7 @@ func (gitAttrs *GitAttributes) IsDocumentation(path string) bool {
return val
}

return data.DocumentationMatchers.Match(path)
return IsDocumentation(path)
}

// Returns whether or not path is a generated path.
@@ -113,8 +113,8 @@ func NewGitAttributes() *GitAttributes {
return &gitAttrs
}

// LoadGitattributes reads and parses the file .gitattributes which overrides the standard strategies
// Returns slice of errors that have may ocurred in the load
// LoadGitattributes reads and parses the file .gitattributes which overrides the standard strategies.
// Returns slice of errors that have may ocurred in the load.
func (gitAttrs *GitAttributes) LoadGitAttributes(path string, reader io.Reader) []error {
rawAttributes, errArr := loadRawGitAttributes(reader)
if len(rawAttributes) == 0 {
@@ -124,19 +124,6 @@ func (gitAttrs *GitAttributes) LoadGitAttributes(path string, reader io.Reader)
return append(gitAttrs.parseAttributes(path, rawAttributes), errArr...)
}

func (gitAttrs *GitAttributes) String() string {
out := ""
for key, val := range gitAttrs.boolAttributes {
out += fmt.Sprintf("Type: %s Attributes: %v\n", key, val.attributes)
}

for key, val := range gitAttrs.regExpAttributes {
out += fmt.Sprintf("Type: %s Attributes: %v\n", key, val.attributes)
}

return out
}

func loadRawGitAttributes(reader io.Reader) (map[string][]string, []error) {
rawAttributes := map[string][]string{}
var errArr []error
@@ -216,6 +203,7 @@ func (gitAttrs *GitAttributes) processBoolAttr(kind attrType, key string, attrib
if _, ok := gitAttrs.boolAttributes[kind].attributes[key]; ok {
err = &overrideError{attribute: kind, path: key}
}

switch {
case attribute == gitAttrs.boolAttributes[kind].matchers[0]:
gitAttrs.boolAttributes[kind].attributes[key] = true
1 change: 0 additions & 1 deletion gitattributes_test.go
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@ func (s *EnryTestSuite) TestLoadGitAttributes() {
errArr := gitAttrs.LoadGitAttributes("test/", reader)
if len(errArr) != 3 {
fmt.Println(errArr)
fmt.Println(gitAttrs.String())
s.Fail("The error length it's not the expected")
}

0 comments on commit 590817f

Please sign in to comment.