-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In some cases, there are files that may be useful, but should not be part of artifact. The ability to add files and directories to the .packignore file has been added, which allows you to ignore these files and directories when packing. Closes #812 @TarantoolBot document Title: `tt pack` support `.packignore` Use `.packignore` in the same way as `.gitignore` allows to exclude unnecessary files while preparing application package with `tt pack command.
- Loading branch information
Showing
6 changed files
with
1,227 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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,132 @@ | ||
package pack | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
type ignorePattern struct { | ||
re *regexp.Regexp | ||
dirOnly bool | ||
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) { | ||
// 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>). | ||
cleanPattern = turnEscapedToHexCode(pattern, '\\') | ||
|
||
// Remove trailing spaces (unless escaped one). | ||
cleanPattern = turnEscapedToHexCode(cleanPattern, ' ') | ||
cleanPattern = strings.TrimRight(cleanPattern, " ") | ||
|
||
cleanPattern, dirOnly = strings.CutSuffix(cleanPattern, "/") | ||
cleanPattern, isNegate = strings.CutPrefix(cleanPattern, "!") | ||
return | ||
} | ||
|
||
func createIgnorePattern(pattern string, basepath string) (ignorePattern, error) { | ||
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.