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

Conversation

saracen
Copy link
Contributor

@saracen saracen commented Apr 23, 2019

Implements gitattributes parsing, matching and attribute extraction.

I've tried to mirror the gitignore API as much as possible.

go doc

VARIABLES

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

TYPES

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

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

func LoadGlobalPatterns(fs billy.Filesystem) (attributes []MatchAttribute, err error)
    LoadGlobalPatterns loads gitattributes patterns and attributes from the
    gitattributes file declared in a user's ~/.gitconfig file. If the
    ~/.gitconfig file does not exist the function will return nil. If the
    core.attributesFile property is not declared, the function will return nil.
    If the file pointed to by the core.attributesfile property does not exist,
    the function will return nil. The function assumes fs is rooted at the root
    filesystem.

func LoadSystemPatterns(fs billy.Filesystem) (attributes []MatchAttribute, err error)
    LoadSystemPatterns loads gitattributes patterns and attributes from the
    gitattributes file declared in a system's /etc/gitconfig file. If the
    /etc/gitconfig file does not exist the function will return nil. If the
    core.attributesfile property is not declared, the function will return nil.
    If the file pointed to by the core.attributesfile property does not exist,
    the function will return nil. The function assumes fs is rooted at the root
    filesystem.

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

func ReadAttributes(r io.Reader, domain []string, allowMacro bool) (attributes []MatchAttribute, err error)
    ReadAttributes reads patterns and attributes from the gitattributes format.

func ReadAttributesFile(fs billy.Filesystem, path []string, attributesFile string, allowMacro bool) ([]MatchAttribute, error)
func ReadPatterns(fs billy.Filesystem, path []string) (attributes []MatchAttribute, err error)
    ReadPatterns reads gitattributes patterns recursively through the directory
    structure. The result is in ascending order of priority (last higher).

    The .gitattribute file in the root directory will allow custom macro
    definitions. Custom macro definitions in other directories .gitattributes
    will return an error.

type Matcher interface {
        // Match matches patterns in the order of priorities.
        Match(path []string, attributes []string) (map[string]Attribute, bool)
}
    Matcher defines a global multi-pattern matcher for gitattributes patterns

func NewMatcher(stack []MatchAttribute) Matcher
    NewMatcher constructs a new matcher. Patterns must be given in the order of
    increasing priority. That is the most generic settings files first, then the
    content of the repo .gitattributes, then content of .gitattributes down the
    path.

type MatcherOptions struct{}

type Pattern interface {
        // Match matches the given path to the pattern.
        Match(path []string) bool
}
    Pattern defines a gitattributes pattern.

func ParsePattern(p string, domain []string) Pattern
    ParsePattern parses a gitattributes pattern string into the Pattern
    structure.

@@ -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

Implements gitattributes parsing, matching and attribute extraction.

Signed-off-by: Arran Walker <arran.walker@fiveturns.org>
@mcuadros mcuadros merged commit bbca4e0 into src-d:master Apr 24, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants