From 8c642dd345741e515eecc96363fd1107ba77f617 Mon Sep 17 00:00:00 2001 From: Samuel Meuli Date: Thu, 17 Oct 2019 20:43:21 +0200 Subject: [PATCH] Improve handling of paths with spaces Fixes #2 --- Sources/tmignore/Git.swift | 7 +++---- Sources/tmignore/TimeMachine.swift | 4 ++-- Sources/tmignore/utils.swift | 10 ++-------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Sources/tmignore/Git.swift b/Sources/tmignore/Git.swift index 5e937c9..4cfdddf 100644 --- a/Sources/tmignore/Git.swift +++ b/Sources/tmignore/Git.swift @@ -12,7 +12,7 @@ class Git { logger.debug("Obtaining list of ignored files for repo at \(repoPath)…") var ignoredFiles = [String]() - let command = "git -C \(repoPath) ls-files --directory --exclude-standard --ignored --others" + let command = "git -C '\(repoPath)' ls-files --directory --exclude-standard --ignored --others" let (status, stdout, stderr) = runCommand(command: command) if status != 0 { @@ -54,6 +54,7 @@ class Git { let (status, stdout, stderr) = runCommand(command: command) if status != 0 { for errLine in splitLines(linesStr: stderr) { + // Ignore permission errors if !errLine.hasSuffix("Operation not permitted") { logger.error("Error searching for Git repositories: \(errLine)") } @@ -64,9 +65,7 @@ class Git { let gitDirs = splitLines(linesStr: stdout) // Build list of repositories (e.g. ["/path/to/repo"]) - repoPaths = gitDirs.map { - String($0.dropLast(5)).replacingOccurrences(of: " ", with: "\\ ") - } + repoPaths = gitDirs.map { String($0.dropLast(5)) } logger.info("Found \(repoPaths.count) Git repositories") return repoPaths diff --git a/Sources/tmignore/TimeMachine.swift b/Sources/tmignore/TimeMachine.swift index 9fad491..3971a7b 100644 --- a/Sources/tmignore/TimeMachine.swift +++ b/Sources/tmignore/TimeMachine.swift @@ -9,7 +9,7 @@ class TimeMachine { Adds the provided path to the list of exclusions (it will not be included in future backups) */ static func addExclusion(path: String) { - let (status, _, stdErr) = runCommand(command: "tmutil addexclusion \(path)") + let (status, _, stdErr) = runCommand(command: "tmutil addexclusion '\(path)'") if status == 0 { logger.debug("Added Time Machine exclusion: \(path)") } else { @@ -22,7 +22,7 @@ class TimeMachine { backups) */ static func removeExclusion(path: String) { - let (status, _, stdErr) = runCommand(command: "tmutil removeexclusion \(path)") + let (status, _, stdErr) = runCommand(command: "tmutil removeexclusion '\(path)'") if status == 0 || status == 213 { // 213: File path wasn't found and could therefore not be excluded from a Time Machine // backup. This error occurs for cached exlusions which were deleted, therefore it is diff --git a/Sources/tmignore/utils.swift b/Sources/tmignore/utils.swift index 427039f..f27d8a4 100644 --- a/Sources/tmignore/utils.swift +++ b/Sources/tmignore/utils.swift @@ -40,14 +40,8 @@ func runCommand(command: String) -> (status: Int32, stdout: String?, stderr: Str // Convert stdout and stderr to strings let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile() let stderrData = stderr.fileHandleForReading.readDataToEndOfFile() - let stdoutStr: String = NSString( - data: stdoutData, - encoding: String.Encoding.utf8.rawValue - )! as String - let stderrStr: String = NSString( - data: stderrData, - encoding: String.Encoding.utf8.rawValue - )! as String + let stdoutStr = String(data: stdoutData, encoding: String.Encoding.utf8) + let stderrStr = String(data: stderrData, encoding: String.Encoding.utf8) task.waitUntilExit()