Skip to content

Incorrect parsing of gitignore files when trying to re-include subdirectories #694

Closed as not planned

Description

@civts

Hi, first of all thank you for the package 🙏

I was doing some tests with the gitignore parser/matcher for using it in another repo, and found an edge case in which it does not comply with the specs.

Use-case

Say I have the following directory structure:

📁 test
├── 📄 .gitignore
└── 📁 foo
    ├── 📄 ignoreme
    └── 📁 bar
        └── 📄 ignoreme

The content of test/.gitignore is the following:

# Ignore foo as a directory (could also have been `foo`)
foo/
# Try to re-include foo/bar (this should fail)
!foo/bar

The two ignoreme files are empty, since in this case we just care about them not ending up in the version control rather than their content.

The discrepancy

According to the specification, line 1 of test/.gitignore excludes the directory foo, so line 2 cannot re-include it -since we excluded the directory itself-.

If line 1 was foo/*, then line 2 would succeed in re-including foo/bar.

I have confirmed with git (v2.39.1) that this is indeed the case (only test/.gitignore gets tracked).

Go-git, successfully marks /test/foo/ignoreme as excluded from git, but wrongfully marks test/foo/bar/ignoreme as included.

Reproducing the bug

Here is the code I used to test this.

package main

import (
	"path/filepath"
	"strings"

	"gopkg.in/src-d/go-billy.v4/osfs"
	"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
)

func main() {

	//Change this with the path to your test directory
	target := "/path/to/the/directory/test"

	fs := osfs.New("/")

	parts, err := pathToArray(target)
	if err != nil {
		return
	}

	patterns, err := gitignore.ReadPatterns(fs, parts)
	if err != nil {
		return
	}

	matcher := gitignore.NewMatcher(patterns)

	tests := []struct {
		path             string
		shouldBeExcluded bool
	}{
		{target + string(filepath.Separator) + ".gitignore", false},
		{target + string(filepath.Separator) + "foo/ignoreme", true},
		{target + string(filepath.Separator) + "foo/bar/ignoreme", true},
	}

	for _, test := range tests {
		p, err := pathToArray(test.path)
		if err != nil {
			println("Error!")
			return
		}
		match := matcher.Match(p, false)
		if match == test.shouldBeExcluded {
			println("Ok")
		} else {
			msg := ""
			if match {
				msg = "excluded"
			} else {
				msg = "included"
			}
			println("File", test.path, "was incorrectly", msg)
		}
	}
}

func pathToArray(path string) ([]string, error) {
	absolute, err := filepath.Abs(path)
	if err != nil {
		return nil, err
	}
	parts := strings.Split(absolute, string(filepath.Separator))

	//Filter out empty strings
	result := []string{}
	for _, p := range parts {
		if p != "" {
			result = append(result, p)
		}
	}

	return result, nil
}

Activity

  1. github-actions commented on Nov 13, 2024

    @github-actions

    To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

    This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

    We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

    Thanks for your understanding and cooperation!

  2. added
    stale
    on Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions