Skip to content

Commit

Permalink
fix: git ls-files command output quoting on 'unusual' characters
Browse files Browse the repository at this point in the history
  • Loading branch information
l-hellmann committed May 21, 2024
1 parent fdf57e2 commit 7fe7ee0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/archiveClient/handler_findFilesByRules.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (h *Handler) FindFilesByRules(uxBlocks uxBlock.UxBlocks, workingDir string,
}

res := make([]File, 0, 200)
createdPaths := make(map[string]struct{})
for _, source := range sources {
parts := strings.Split(source, "~")
if len(parts) > 2 {
Expand Down Expand Up @@ -88,7 +87,7 @@ func (h *Handler) FindFilesByRules(uxBlocks uxBlock.UxBlocks, workingDir string,
return nil, err
}

files = h.fixMissingDirPath(files, createFile, createdPaths)
files = h.fixMissingDirPath(files, createFile)
res = append(res, files...)
}

Expand Down
30 changes: 23 additions & 7 deletions src/archiveClient/handler_findGitFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}

Expand All @@ -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{}{}

Expand All @@ -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)))
Expand All @@ -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)))
Expand All @@ -84,12 +88,13 @@ func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []Fi
return nil, err
}

res = h.fixMissingDirPath(res, createFile, make(map[string]struct{}))
res = h.fixMissingDirPath(res, createFile)

// add .git dir to allow git commands inside build.prepare and build.build commands
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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/archiveClient/handler_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ 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 {
func (h *Handler) fixMissingDirPath(files []File, createFile func(filePath string) File) []File {
fixedFiles := make([]File, 0, len(files)+50)
createdPaths := make(map[string]struct{})

for _, file := range files {
// filepath.Dir calls Clean, which replaces all / with os.PathSeparator (same as calling filepath.FromSlash)
Expand Down

0 comments on commit 7fe7ee0

Please sign in to comment.