mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Fix getCommitHash() for file paths with spaces
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user