Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 strings.Contains(cleanPath, "/.git/") {
return true
}
}
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
12 changes: 10 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,16 @@ func Test_Tar(t *testing.T) {
counter++
name := header.Name

cleanName := filepath.ToSlash(name)
if cleanName == ".gitignore" {
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())
}
11 changes: 5 additions & 6 deletions pkg/shp/streamer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
)

type writeCounter struct{ total int }
Expand All @@ -17,17 +16,17 @@ func (wc *writeCounter) Write(p []byte) (int, error) {
return n, nil
}

func trimPrefix(prefix, fpath string) string {
return strings.TrimPrefix(strings.ReplaceAll(fpath, prefix, ""), string(filepath.Separator))
}

func writeFileToTar(tw *tar.Writer, src, fpath string, stat fs.FileInfo) error {
header, err := tar.FileInfoHeader(stat, stat.Name())
if err != nil {
return err
}

header.Name = trimPrefix(src, fpath)
relPath, err := filepath.Rel(src, fpath)
if err != nil {
return err
}
header.Name = filepath.ToSlash(relPath)
if err := tw.WriteHeader(header); err != nil {
return err
}
Expand Down