-
Notifications
You must be signed in to change notification settings - Fork 15
pack: support .packignore #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package pack | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
// ignorePattern corresponds to a single ignore pattern from .packignore file. | ||
type ignorePattern struct { | ||
// re holds the "matching" part of ignore pattern (i.e. w/o trailing spaces, directory and | ||
// negate markers) in the form of regular expression. | ||
re *regexp.Regexp | ||
// dirOnly defines whether this pattern should be applied to all entries (false) or | ||
// to directory entries only (true). | ||
dirOnly bool | ||
// isNegate defines how to interpret the match (false means that it's an ordinary pattern | ||
// that excludes entry, true - it's a negate pattern that includes entry). | ||
isNegate bool | ||
} | ||
|
||
func turnEscapedToHexCode(s string, c rune) string { | ||
return strings.ReplaceAll(s, `\`+string(c), fmt.Sprintf(`\x%x`, c)) | ||
} | ||
|
||
func splitIgnorePattern(pattern string) (cleanPattern string, dirOnly bool, isNegate bool) { | ||
// Remove trailing spaces (unless escaped one). | ||
cleanPattern = turnEscapedToHexCode(pattern, ' ') | ||
cleanPattern = strings.TrimRight(cleanPattern, " ") | ||
// Parse negate and directory markers. | ||
cleanPattern, dirOnly = strings.CutSuffix(cleanPattern, "/") | ||
cleanPattern, isNegate = strings.CutPrefix(cleanPattern, "!") | ||
return | ||
} | ||
|
||
func createIgnorePattern(pattern string, basepath string) (ignorePattern, error) { | ||
// First, get rid of `\\` to simplify further handling of escaped sequences. | ||
// From now on any `\c` always means escaped 'c' (previously it might also | ||
// occur as a part of `\\c` sequence which denotes '\' followed by <c>). | ||
pattern = turnEscapedToHexCode(pattern, '\\') | ||
|
||
cleanPattern, dirOnly, isNegate := splitIgnorePattern(pattern) | ||
|
||
// Translate pattern to regex expression. | ||
expr := cleanPattern | ||
// Turn escaped '*' and '?' to their hex representation to simplify the translation. | ||
expr = turnEscapedToHexCode(expr, '*') | ||
expr = turnEscapedToHexCode(expr, '?') | ||
// Escape symbols that designate themselves in pattern, but have special meaning in regex. | ||
for _, s := range []string{"(", ")", "{", "}", "+"} { | ||
// Do unescape first to avoid double escaping of the ones that are already escaped. | ||
expr = strings.ReplaceAll(expr, "\\"+s, s) | ||
expr = strings.ReplaceAll(expr, s, "\\"+s) | ||
} | ||
// Replace wildcards with the corresponding regex representation. | ||
// Note that '{0,}' (not '*') is used while replacing '**' to avoid confusing | ||
// in the subsequent replacement of a single '*'. | ||
expr = strings.ReplaceAll(expr, "/**/", "/([^/]+/){0,}") | ||
expr, found := strings.CutPrefix(expr, "**/") | ||
if found || !strings.Contains(cleanPattern, "/") { | ||
expr = "([^/]+/){0,}" + expr | ||
} | ||
expr, found = strings.CutSuffix(expr, "/**") | ||
if found { | ||
expr = expr + "/([^/]+/){0,}[^/]+" | ||
} | ||
expr = strings.ReplaceAll(expr, "*", "[^/]*") | ||
expr = strings.ReplaceAll(expr, "?", "[^/]") | ||
|
||
re, err := regexp.Compile("^" + basepath + expr + "$") | ||
if err != nil { | ||
return ignorePattern{}, fmt.Errorf("failed to compile expression: %w", err) | ||
} | ||
|
||
return ignorePattern{ | ||
re: re, | ||
dirOnly: dirOnly, | ||
isNegate: isNegate, | ||
}, nil | ||
} | ||
|
||
// loadIgnorePatterns reads ignore patterns from the patternsFile. | ||
func loadIgnorePatterns(fsys fs.FS, patternsFile string) ([]ignorePattern, error) { | ||
contents, err := fs.ReadFile(fsys, patternsFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
basepath, _ := filepath.Split(patternsFile) | ||
|
||
var patterns []ignorePattern | ||
s := bufio.NewScanner(bytes.NewReader(contents)) | ||
for s.Scan() { | ||
pattern := s.Text() | ||
if pattern == "" || strings.HasPrefix(pattern, "#") { | ||
continue | ||
} | ||
|
||
p, err := createIgnorePattern(pattern, basepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patterns = append(patterns, p) | ||
} | ||
return patterns, nil | ||
} | ||
|
||
// ignoreFilter returns filter function that implements .gitignore approach of filtering files. | ||
func ignoreFilter(fsys fs.FS, patternsFile string) (skipFilter, error) { | ||
patterns, err := loadIgnorePatterns(fsys, patternsFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// According to .gitignore documentation "the last matching pattern decides the outcome" | ||
// so we need to iterate in reverse order until the first match. | ||
slices.Reverse(patterns) | ||
|
||
return func(srcInfo os.FileInfo, src string) bool { | ||
// Skip ignore file itself. | ||
if src == patternsFile { | ||
return true | ||
} | ||
for _, p := range patterns { | ||
isApplicable := srcInfo.IsDir() || !p.dirOnly | ||
if isApplicable && p.re.MatchString(src) { | ||
return !p.isNegate | ||
} | ||
} | ||
return false | ||
}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.