Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/shp/streamer/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ func (t *Tar) skipPath(fpath string, stat fs.FileInfo) bool {
if !stat.Mode().IsRegular() {
return true
}
if strings.HasPrefix(fpath, path.Join(t.src, ".git")) {
return true
relPath, err := filepath.Rel(t.src, fpath)
if err == nil {
cleanPath := filepath.ToSlash(relPath)
if cleanPath == ".git" || strings.HasPrefix(cleanPath, ".git/") {
return true
}
Comment thread
mohit-bhandari45 marked this conversation as resolved.
Outdated
}
if t.gitIgnore == nil {
return false

if t.gitIgnore != nil {
return t.gitIgnore.MatchesPath(fpath)
}
return t.gitIgnore.MatchesPath(fpath)
return false
}

// Create the actual tar by inspecting all files in source path, skipping some.
Expand Down
16 changes: 14 additions & 2 deletions pkg/shp/streamer/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package streamer
import (
"archive/tar"
"io"
"path/filepath"
"strings"
"testing"

Expand All @@ -26,6 +27,7 @@ func Test_Tar(t *testing.T) {

tarReader := tar.NewReader(reader)
counter := 0
foundGitIgnore := false
for {
header, err := tarReader.Next()
if err != nil {
Expand All @@ -37,10 +39,20 @@ func Test_Tar(t *testing.T) {
counter++
name := header.Name

cleanName := filepath.ToSlash(name)
// On windows, trimPrefix might fail to trim the prefix cleanly due to slash mismatch, leaving ../../../ prefix.
Comment thread
mohit-bhandari45 marked this conversation as resolved.
Outdated
if cleanName == ".gitignore" || strings.HasSuffix(cleanName, "/.gitignore") {
Comment thread
mohit-bhandari45 marked this conversation as resolved.
Outdated
// Ensure it's not a vendor or nested gitignore
if !strings.Contains(cleanName, "vendor/") {
foundGitIgnore = true
}
}

// making sure that undesired entries are not present on the list of files caputured by the
// tar helper
g.Expect(strings.HasPrefix(name, ".git/")).To(o.BeFalse())
g.Expect(strings.HasPrefix(name, "_output/")).To(o.BeFalse())
g.Expect(strings.Split(cleanName, "/")).NotTo(o.ContainElement(".git"), "should not contain a .git path component")
g.Expect(strings.HasPrefix(cleanName, "_output/")).To(o.BeFalse())
}
g.Expect(foundGitIgnore).To(o.BeTrue(), "expected .gitignore to be included in the tarball")
g.Expect(counter > 10).To(o.BeTrue())
}
Loading