Skip to content

Commit 0c48681

Browse files
committed
Fix getCommitHash() for file paths with spaces
1 parent b2272b2 commit 0c48681

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

Sources/GitFileInfo.swift

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,16 @@ private let getGitRoot: (URL) -> URL? = memoize({ $0.relativePath }) { url in
111111
private let getDefaultGitInfo: (URL) -> GitFileInfo = memoize({ $0.relativePath }) { url in
112112
let name = "git config user.name".shellOutput(cwd: url)
113113
let email = "git config user.email".shellOutput(cwd: url)
114-
115114
return GitFileInfo(authorName: name, authorEmail: email)
116115
}
117116

118117
private let getMovedFiles: (URL) -> [(from: URL, to: URL)] = memoize({ $0.relativePath }) { root in
119118
let command = "git diff --diff-filter=R --staged --name-status"
120-
let output = command.shellOutput(cwd: root)
121-
122-
guard let safeValue = output, !safeValue.isEmpty else { return [] }
123-
124-
return safeValue.split(separator: "\n").compactMap { input -> (URL, URL)? in
125-
var parts = input.split(separator: "\t").dropFirst()
126-
119+
guard let output = command.shellOutput(cwd: root) else { return [] }
120+
return output.components(separatedBy: "\n").compactMap { input -> (URL, URL)? in
121+
var parts = input.components(separatedBy: "\t").dropFirst()
127122
guard let from = parts.popFirst(), let to = parts.popFirst(), from != to else { return nil }
128-
129-
let fromURL = URL(fileURLWithPath: String(from), relativeTo: root)
130-
let toURL = URL(fileURLWithPath: String(to), relativeTo: root)
131-
132-
return (fromURL, toURL)
123+
return (URL(fileURLWithPath: from, relativeTo: root), URL(fileURLWithPath: to, relativeTo: root))
133124
}
134125
}
135126

@@ -144,24 +135,12 @@ private func getCommitHash(_ url: URL, root: URL) -> String? {
144135
"--author-date-order",
145136
"--pretty=%H",
146137
"--",
147-
trackedFile.relativePath,
138+
"\"\(trackedFile.relativePath)\"",
148139
]
149-
.filter { ($0?.count ?? 0) > 0 }
150140
.joined(separator: " ")
151141

152-
let output = command.shellOutput(cwd: root)
153-
154-
guard let safeValue = output, !safeValue.isEmpty else { return nil }
155-
156-
if safeValue.contains("\n") {
157-
let parts = safeValue.split(separator: "\n")
158-
159-
if parts.count > 1, let first = parts.first {
160-
return String(first)
161-
}
162-
}
163-
164-
return safeValue
142+
guard let output = command.shellOutput(cwd: root) else { return nil }
143+
return output.components(separatedBy: "\n").first.flatMap { $0.isEmpty ? nil : $0 }
165144
}
166145

167146
private let getCommitInfo: ((String?, URL)) -> GitFileInfo? = memoize(

0 commit comments

Comments
 (0)