Skip to content

Commit

Permalink
Improve handling of paths with spaces
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
samuelmeuli committed Oct 17, 2019
1 parent 5b83bce commit 8c642dd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
7 changes: 3 additions & 4 deletions Sources/tmignore/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)")
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/tmignore/TimeMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions Sources/tmignore/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 8c642dd

Please sign in to comment.