From 926f5b36da635895bb76ef44a6711fec9c68afeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hellmann?= Date: Tue, 21 May 2024 15:07:52 +0200 Subject: [PATCH] fix: git ls-files command output quoting on 'unusual' characters --- .../handler_findFilesByRules_test.go | 2 +- src/archiveClient/handler_findGitFiles.go | 28 +++++++++++++++---- src/archiveClient/handler_utils.go | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/archiveClient/handler_findFilesByRules_test.go b/src/archiveClient/handler_findFilesByRules_test.go index 6de1eae7..10f63d7e 100644 --- a/src/archiveClient/handler_findFilesByRules_test.go +++ b/src/archiveClient/handler_findFilesByRules_test.go @@ -227,7 +227,7 @@ func TestValidation(t *testing.T) { for _, test := range testErrorResponseDataProvider { test := test // scope lint - t.Run(test.name+" in "+test.workingDir, func(t *testing.T) { + t.Run(test.name+"-in-"+test.workingDir, func(t *testing.T) { archiver := New(Config{}) files, err := archiver.FindFilesByRules(uxBlocks, test.workingDir, test.input) diff --git a/src/archiveClient/handler_findGitFiles.go b/src/archiveClient/handler_findGitFiles.go index f43cdb71..11fcbf3f 100644 --- a/src/archiveClient/handler_findGitFiles.go +++ b/src/archiveClient/handler_findGitFiles.go @@ -37,7 +37,8 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi // find excluded files excludedFiles := make(map[string]struct{}) if err := h.listFiles( - createCmd("git", "ls-files", "--deleted", "--exclude-standard"), + createCmd("git", "ls-files", "--deleted", "--exclude-standard", "-z"), + replaceNullBytesWithNewLine, func(path string) error { excludedFiles[path] = struct{}{} @@ -48,7 +49,8 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi } if err := h.listFiles( - createCmd("git", "ls-files", "--others", "--ignored", "--exclude-standard"), + createCmd("git", "ls-files", "--others", "--ignored", "--exclude-standard", "-z"), + replaceNullBytesWithNewLine, func(path string) error { excludedFiles[path] = struct{}{} @@ -60,7 +62,8 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi // add all non deleted if err := h.listFiles( - createCmd("git", "ls-files", "--exclude-standard", "--recurse-submodules"), + createCmd("git", "ls-files", "--exclude-standard", "--recurse-submodules", "-z"), + replaceNullBytesWithNewLine, func(path string) error { if _, exists := excludedFiles[path]; !exists { res = append(res, createFile(filepath.Join(workingDir, path))) @@ -73,7 +76,8 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi // add untracked files if err := h.listFiles( - createCmd("git", "ls-files", "--others", "--exclude-standard"), + createCmd("git", "ls-files", "--others", "--exclude-standard", "-z"), + replaceNullBytesWithNewLine, func(path string) error { if _, exists := excludedFiles[path]; !exists { res = append(res, createFile(filepath.Join(workingDir, path))) @@ -90,6 +94,7 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi if h.config.DeployGitFolder { if err := h.listFiles( createCmd("find", ".git/"), + bytesReader, func(path string) error { res = append(res, createFile(filepath.Join(workingDir, path))) return nil @@ -102,13 +107,24 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi return res, nil } -func (h *Handler) listFiles(cmd *cmdRunner.ExecCmd, fn func(path string) error) error { +// replaceNullBytesWithNewLine used in combination of '-z' flag with git command +// '-z' flag returns null terminated unquoted strings +func replaceNullBytesWithNewLine(output []byte) io.Reader { + output = bytes.ReplaceAll(output, []byte{0}, []byte{'\n'}) + return bytes.NewReader(output) +} + +func bytesReader(out []byte) io.Reader { + return bytes.NewReader(out) +} + +func (h *Handler) listFiles(cmd *cmdRunner.ExecCmd, reader func(out []byte) io.Reader, fn func(path string) error) error { output, err := cmdRunner.Run(cmd) if err != nil { return err } - rd := bufio.NewReader(bytes.NewReader(output)) + rd := bufio.NewReader(reader(output)) for { lineB, _, err := rd.ReadLine() line := string(lineB) diff --git a/src/archiveClient/handler_utils.go b/src/archiveClient/handler_utils.go index 0c19bcda..5b9241eb 100644 --- a/src/archiveClient/handler_utils.go +++ b/src/archiveClient/handler_utils.go @@ -6,7 +6,7 @@ import ( "strings" ) -// fixes paths/dirs that may be missing between source root and deployed files (all dirs are needed for valid TAR file) +// fixMissingDirPath fixes paths/dirs that may be missing between source root and deployed files (all dirs are needed for valid TAR file) // provided createFile func will receive a valid File.SourcePath and MUST strip workDir to form a valid File.ArchivePath // createdPath is used as a cache to not create paths multiple times for multiple calls of the function func (h *Handler) fixMissingDirPath(files []File, createFile func(filePath string) File, createdPaths map[string]struct{}) []File {