Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

plumbing: format/gitattributes support #1130

Merged
merged 1 commit into from
Apr 24, 2019
Merged
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
214 changes: 214 additions & 0 deletions plumbing/format/gitattributes/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package gitattributes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it better simply call the package attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did wonder that, but stuck with gitattributes as go-git already has gitignore. Git's own documentation also refers to it as "gitattributes" (https://git-scm.com/docs/gitattributes).

I don't mind either way though


import (
"errors"
"io"
"io/ioutil"
"strings"
)

const (
commentPrefix = "#"
eol = "\n"
macroPrefix = "[attr]"
)

var (
ErrMacroNotAllowed = errors.New("macro not allowed")
ErrInvalidAttributeName = errors.New("Invalid attribute name")
)

type MatchAttribute struct {
Name string
Pattern Pattern
Attributes []Attribute
}

type attributeState byte

const (
attributeUnknown attributeState = 0
attributeSet attributeState = 1
attributeUnspecified attributeState = '!'
attributeUnset attributeState = '-'
attributeSetValue attributeState = '='
)

type Attribute interface {
Name() string
IsSet() bool
IsUnset() bool
IsUnspecified() bool
IsValueSet() bool
Value() string
String() string
}

type attribute struct {
name string
state attributeState
value string
}

func (a attribute) Name() string {
return a.name
}

func (a attribute) IsSet() bool {
return a.state == attributeSet
}

func (a attribute) IsUnset() bool {
return a.state == attributeUnset
}

func (a attribute) IsUnspecified() bool {
return a.state == attributeUnspecified
}

func (a attribute) IsValueSet() bool {
return a.state == attributeSetValue
}

func (a attribute) Value() string {
return a.value
}

func (a attribute) String() string {
switch a.state {
case attributeSet:
return a.name + ": set"
case attributeUnset:
return a.name + ": unset"
case attributeUnspecified:
return a.name + ": unspecified"
default:
return a.name + ": " + a.value
}
}

// ReadAttributes reads patterns and attributes from the gitattributes format.
func ReadAttributes(r io.Reader, domain []string, allowMacro bool) (attributes []MatchAttribute, err error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

for _, line := range strings.Split(string(data), eol) {
attribute, err := ParseAttributesLine(line, domain, allowMacro)
if err != nil {
return attributes, err
}
if len(attribute.Name) == 0 {
continue
}

attributes = append(attributes, attribute)
}

return attributes, nil
}

// ParseAttributesLine parses a gitattribute line, extracting path pattern and
// attributes.
func ParseAttributesLine(line string, domain []string, allowMacro bool) (m MatchAttribute, err error) {
line = strings.TrimSpace(line)

if strings.HasPrefix(line, commentPrefix) || len(line) == 0 {
return
}

name, unquoted := unquote(line)
attrs := strings.Fields(unquoted)
if len(name) == 0 {
name = attrs[0]
attrs = attrs[1:]
}

var macro bool
macro, name, err = checkMacro(name, allowMacro)
if err != nil {
return
}

m.Name = name
m.Attributes = make([]Attribute, 0, len(attrs))

for _, attrName := range attrs {
attr := attribute{
name: attrName,
state: attributeSet,
}

// ! and - prefixes
state := attributeState(attr.name[0])
if state == attributeUnspecified || state == attributeUnset {
attr.state = state
attr.name = attr.name[1:]
}

kv := strings.SplitN(attrName, "=", 2)
if len(kv) == 2 {
attr.name = kv[0]
attr.value = kv[1]
attr.state = attributeSetValue
}

if !validAttributeName(attr.name) {
return m, ErrInvalidAttributeName
}
m.Attributes = append(m.Attributes, attr)
}

if !macro {
m.Pattern = ParsePattern(name, domain)
}
return
}

func checkMacro(name string, allowMacro bool) (macro bool, macroName string, err error) {
if !strings.HasPrefix(name, macroPrefix) {
return false, name, nil
}
if !allowMacro {
return true, name, ErrMacroNotAllowed
}

macroName = name[len(macroPrefix):]
if !validAttributeName(macroName) {
return true, name, ErrInvalidAttributeName
}
return true, macroName, nil
}

func validAttributeName(name string) bool {
if len(name) == 0 || name[0] == '-' {
return false
}

for _, ch := range name {
if !(ch == '-' || ch == '.' || ch == '_' ||
('0' <= ch && ch <= '9') ||
('a' <= ch && ch <= 'z') ||
('A' <= ch && ch <= 'Z')) {
return false
}
}
return true
}

func unquote(str string) (string, string) {
if str[0] != '"' {
return "", str
}

for i := 1; i < len(str); i++ {
switch str[i] {
case '\\':
i++
case '"':
return str[1:i], str[i+1:]
}
}
return "", str
}
67 changes: 67 additions & 0 deletions plumbing/format/gitattributes/attributes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gitattributes

import (
"strings"

. "gopkg.in/check.v1"
)

type AttributesSuite struct{}

var _ = Suite(&AttributesSuite{})

func (s *AttributesSuite) TestAttributes_ReadAttributes(c *C) {
lines := []string{
"[attr]sub -a",
"[attr]add a",
"* sub a",
"* !a foo=bar -b c",
}

mas, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, true)
c.Assert(err, IsNil)
c.Assert(len(mas), Equals, 4)

c.Assert(mas[0].Name, Equals, "sub")
c.Assert(mas[0].Pattern, IsNil)
c.Assert(mas[0].Attributes[0].IsUnset(), Equals, true)

c.Assert(mas[1].Name, Equals, "add")
c.Assert(mas[1].Pattern, IsNil)
c.Assert(mas[1].Attributes[0].IsSet(), Equals, true)

c.Assert(mas[2].Name, Equals, "*")
c.Assert(mas[2].Pattern, NotNil)
c.Assert(mas[2].Attributes[0].IsSet(), Equals, true)

c.Assert(mas[3].Name, Equals, "*")
c.Assert(mas[3].Pattern, NotNil)
c.Assert(mas[3].Attributes[0].IsUnspecified(), Equals, true)
c.Assert(mas[3].Attributes[1].IsValueSet(), Equals, true)
c.Assert(mas[3].Attributes[1].Value(), Equals, "bar")
c.Assert(mas[3].Attributes[2].IsUnset(), Equals, true)
c.Assert(mas[3].Attributes[3].IsSet(), Equals, true)
c.Assert(mas[3].Attributes[0].String(), Equals, "a: unspecified")
c.Assert(mas[3].Attributes[1].String(), Equals, "foo: bar")
c.Assert(mas[3].Attributes[2].String(), Equals, "b: unset")
c.Assert(mas[3].Attributes[3].String(), Equals, "c: set")
}

func (s *AttributesSuite) TestAttributes_ReadAttributesDisallowMacro(c *C) {
lines := []string{
"[attr]sub -a",
"* a add",
}

_, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, false)
c.Assert(err, Equals, ErrMacroNotAllowed)
}

func (s *AttributesSuite) TestAttributes_ReadAttributesInvalidName(c *C) {
lines := []string{
"[attr]foo!bar -a",
}

_, err := ReadAttributes(strings.NewReader(strings.Join(lines, "\n")), nil, true)
c.Assert(err, Equals, ErrInvalidAttributeName)
}
Loading