Skip to content

Commit 5e2ca12

Browse files
committed
pack: added .packignore
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](tarantool#812)
1 parent f87a566 commit 5e2ca12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+360
-0
lines changed

cli/pack/common.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const (
3333
versionLuaFileName = "VERSION.lua"
3434

3535
rocksManifestPath = ".rocks/share/tarantool/rocks/manifest"
36+
37+
ignoreFile = ".packignore"
3638
)
3739

3840
var (
@@ -285,6 +287,17 @@ func copyApplications(bundleEnvPath string, packCtx *PackCtx,
285287
}
286288
inst := instances[0]
287289
appPath := inst.AppDir
290+
if !inst.IsFileApp {
291+
ignorePatterns, err := readPackIgnore(appPath)
292+
if err != nil {
293+
return fmt.Errorf("failed to read %s: %s", ignoreFile, err)
294+
}
295+
296+
if err = removeIgnoredFiles(appPath, ignorePatterns); err != nil {
297+
return fmt.Errorf("failed to remove ignored files: %s", err)
298+
}
299+
}
300+
288301
if inst.IsFileApp {
289302
appPath = inst.InstanceScript
290303
resolvedAppPath, err := filepath.EvalSymlinks(appPath)
@@ -383,6 +396,16 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
383396
}
384397
}
385398

399+
projectPath := cmdCtx.Cli.ConfigDir
400+
ignorePatterns, err := readPackIgnore(projectPath)
401+
if err != nil {
402+
return "", fmt.Errorf("failed to read .packignore: %s", err)
403+
}
404+
405+
if err = removeIgnoredFiles(bundleEnvPath, ignorePatterns); err != nil {
406+
return "", fmt.Errorf("failed to remove ignored files: %s", err)
407+
}
408+
386409
if buildRocks {
387410
err = buildAppRocks(cmdCtx, packCtx, cliOpts, bundleEnvPath)
388411
if err != nil && !os.IsNotExist(err) {

cli/pack/common_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,26 @@ func Test_prepareBundle(t *testing.T) {
10681068
{assert.FileExists, "app/tt.yaml"},
10691069
},
10701070
},
1071+
{
1072+
name: "Packing env with packignore",
1073+
params: params{
1074+
configPath: "testdata/bundle_with_packignore/tt.yaml",
1075+
tntExecutable: tntExecutable,
1076+
packCtx: PackCtx{Name: "app"},
1077+
},
1078+
wantErr: false,
1079+
checks: []check{
1080+
{assert.DirExists, "instances.enabled"},
1081+
{assert.NoFileExists, "instances.enabled/app"},
1082+
1083+
{assert.NoDirExists, "modules"},
1084+
1085+
{assert.DirExists, "app2"},
1086+
{assert.NoDirExists, "app2/var"},
1087+
1088+
{assert.NoFileExists, "app.lua"},
1089+
},
1090+
},
10711091
}
10721092

10731093
for _, tt := range tests {

cli/pack/ignore.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package pack
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// readPackIgnore reads the .packignore file and returns a slice of ignore patterns.
12+
func readPackIgnore(projectPath string) (map[string]struct{}, error) {
13+
ignoreFilePath := filepath.Join(projectPath, ".packignore")
14+
file, err := os.Open(ignoreFilePath)
15+
if err != nil {
16+
if os.IsNotExist(err) {
17+
return map[string]struct{}{}, nil
18+
}
19+
return nil, err
20+
}
21+
defer file.Close()
22+
23+
patterns := make(map[string]struct{})
24+
scanner := bufio.NewScanner(file)
25+
for scanner.Scan() {
26+
line := scanner.Text()
27+
line = strings.TrimSpace(line)
28+
if line == "" || strings.HasPrefix(line, "#") {
29+
continue
30+
}
31+
patterns[line] = struct{}{}
32+
}
33+
34+
if err := scanner.Err(); err != nil {
35+
return nil, err
36+
}
37+
return patterns, nil
38+
}
39+
40+
// shouldIgnore checks if the given file path matches any of the ignore patterns.
41+
func shouldIgnore(path string, patterns map[string]struct{}) (bool, error) {
42+
for pattern := range patterns {
43+
pattern = filepath.ToSlash(pattern)
44+
filePath := filepath.ToSlash(path)
45+
46+
if strings.HasPrefix(filePath, pattern) {
47+
return true, nil
48+
}
49+
50+
match, err := filepath.Match(pattern, filePath)
51+
if err != nil {
52+
return false, err
53+
}
54+
if match {
55+
return true, nil
56+
}
57+
}
58+
return false, nil
59+
}
60+
61+
// removeIgnoredFiles walks through the directory and removes files or directories
62+
// that match the ignore patterns.
63+
func removeIgnoredFiles(bundleEnvPath string, patterns map[string]struct{}) error {
64+
return filepath.Walk(bundleEnvPath, func(path string, info os.FileInfo, err error) error {
65+
if err != nil {
66+
return err
67+
}
68+
69+
relPath, err := filepath.Rel(bundleEnvPath, path)
70+
if err != nil {
71+
return err
72+
}
73+
74+
relPathUnix := filepath.ToSlash(relPath)
75+
76+
ignore, err := shouldIgnore(relPathUnix, patterns)
77+
if err != nil {
78+
return err
79+
}
80+
81+
if ignore {
82+
if info.IsDir() {
83+
err = os.RemoveAll(path)
84+
if err != nil {
85+
return fmt.Errorf("failed to remove directory %q: %s", path, err)
86+
}
87+
return filepath.SkipDir
88+
} else {
89+
err = os.Remove(path)
90+
if err != nil {
91+
return fmt.Errorf("failed to remove file %q: %s", path, err)
92+
}
93+
}
94+
}
95+
return nil
96+
})
97+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
instances.enabled/app
2+
modules
3+
app2/var
4+
app.lua

cli/pack/testdata/bundle_with_packignore/app.lua

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/app2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/instances_enabled
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../test/integration/pack/test_bundles/bundle1/modules
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
env:
2+
bin_dir: bin
3+
inc_dir: include
4+
instances_enabled: .
5+
tarantoolctl_layout: false
6+
app:
7+
run_dir: var/run
8+
log_dir: var/log
9+
wal_dir: var/lib
10+
memtx_dir: var/lib
11+
vinyl_dir: var/lib

test/integration/pack/test_bundles/bundle_with_app_packignore/app.lua

Whitespace-only changes.

0 commit comments

Comments
 (0)