-
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
5 changed files
with
727 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,111 @@ | ||
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 createIgnorePattern(pattern string, basepath string) (ignorePattern, error) { | ||
var p ignorePattern | ||
var err error | ||
|
||
pattern, p.dirOnly = strings.CutSuffix(pattern, "/") | ||
pattern, p.isNegate = strings.CutPrefix(pattern, "!") | ||
|
||
if !p.isNegate && (strings.HasPrefix(pattern, "\\!") || strings.HasPrefix(pattern, "\\#")) { | ||
pattern = pattern[1:] | ||
} | ||
|
||
expr := pattern | ||
expr, found := strings.CutSuffix(expr, "/**") | ||
if found { | ||
expr = expr + "/([^/]+/)*[^/]*" | ||
} | ||
expr = strings.ReplaceAll(expr, "**/", "([^/]+/)*") | ||
expr = strings.ReplaceAll(expr, "*", "[^/]*") | ||
expr = strings.ReplaceAll(expr, "?", "[^/]") | ||
|
||
basepath, _ = strings.CutSuffix(basepath, "/") | ||
if basepath == "." { | ||
basepath = "" | ||
} | ||
|
||
if strings.Contains(pattern, "/") { | ||
expr = basepath + expr | ||
} else { | ||
expr = basepath + "/?([^/]+/)*" + expr | ||
} | ||
|
||
p.re, err = regexp.Compile("^" + expr + "$") | ||
if err != nil { | ||
return ignorePattern{}, fmt.Errorf("failed to compile expression: %w", err) | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// loadIgnorePatterns returns filter that excludes files based on the patterns. | ||
func loadIgnorePatterns(fsys fs.FS, ignoreFile string) ([]ignorePattern, error) { | ||
contents, err := fs.ReadFile(fsys, ignoreFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
basepath := filepath.Dir(ignoreFile) | ||
|
||
var patterns []ignorePattern | ||
s := bufio.NewScanner(bytes.NewReader(contents)) | ||
for s.Scan() { | ||
pattern := strings.TrimSpace(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.