Fix getCommitHash() for file paths with spaces

This commit is contained in:
Nick Lockwood
2025-11-25 17:58:35 +00:00
parent 26ae579519
commit 8afe6babb4
+7 -28
View File
@@ -111,25 +111,16 @@ private let getGitRoot: (URL) -> URL? = memoize({ $0.relativePath }) { url in
private let getDefaultGitInfo: (URL) -> GitFileInfo = memoize({ $0.relativePath }) { url in
let name = "git config user.name".shellOutput(cwd: url)
let email = "git config user.email".shellOutput(cwd: url)
return GitFileInfo(authorName: name, authorEmail: email)
}
private let getMovedFiles: (URL) -> [(from: URL, to: URL)] = memoize({ $0.relativePath }) { root in
let command = "git diff --diff-filter=R --staged --name-status"
let output = command.shellOutput(cwd: root)
guard let safeValue = output, !safeValue.isEmpty else { return [] }
return safeValue.split(separator: "\n").compactMap { input -> (URL, URL)? in
var parts = input.split(separator: "\t").dropFirst()
guard let output = command.shellOutput(cwd: root) else { return [] }
return output.components(separatedBy: "\n").compactMap { input -> (URL, URL)? in
var parts = input.components(separatedBy: "\t").dropFirst()
guard let from = parts.popFirst(), let to = parts.popFirst(), from != to else { return nil }
let fromURL = URL(fileURLWithPath: String(from), relativeTo: root)
let toURL = URL(fileURLWithPath: String(to), relativeTo: root)
return (fromURL, toURL)
return (URL(fileURLWithPath: from, relativeTo: root), URL(fileURLWithPath: to, relativeTo: root))
}
}
@@ -144,24 +135,12 @@ private func getCommitHash(_ url: URL, root: URL) -> String? {
"--author-date-order",
"--pretty=%H",
"--",
trackedFile.relativePath,
"\"\(trackedFile.relativePath)\"",
]
.filter { ($0?.count ?? 0) > 0 }
.joined(separator: " ")
let output = command.shellOutput(cwd: root)
guard let safeValue = output, !safeValue.isEmpty else { return nil }
if safeValue.contains("\n") {
let parts = safeValue.split(separator: "\n")
if parts.count > 1, let first = parts.first {
return String(first)
}
}
return safeValue
guard let output = command.shellOutput(cwd: root) else { return nil }
return output.components(separatedBy: "\n").first.flatMap { $0.isEmpty ? nil : $0 }
}
private let getCommitInfo: ((String?, URL)) -> GitFileInfo? = memoize(