diff --git a/pkg/shp/streamer/tar.go b/pkg/shp/streamer/tar.go index e2255f7c7..4e3a9d3ba 100644 --- a/pkg/shp/streamer/tar.go +++ b/pkg/shp/streamer/tar.go @@ -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. diff --git a/pkg/shp/streamer/tar_test.go b/pkg/shp/streamer/tar_test.go index 810e6e4f1..d0ed23203 100644 --- a/pkg/shp/streamer/tar_test.go +++ b/pkg/shp/streamer/tar_test.go @@ -3,6 +3,7 @@ package streamer import ( "archive/tar" "io" + "path/filepath" "strings" "testing" @@ -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 { @@ -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()) } diff --git a/pkg/shp/streamer/util.go b/pkg/shp/streamer/util.go index 4fa42b724..d499fdd90 100644 --- a/pkg/shp/streamer/util.go +++ b/pkg/shp/streamer/util.go @@ -6,7 +6,6 @@ import ( "io/fs" "os" "path/filepath" - "strings" ) type writeCounter struct{ total int } @@ -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 }