-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathGitStatus.swift
More file actions
47 lines (37 loc) · 1.56 KB
/
GitStatus.swift
File metadata and controls
47 lines (37 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Foundation
import SystemUtils
public enum UntrackedFilesOption: String {
case all, no, normal
}
public struct GitStatus {
static let unTrackedFilePrefix = "?? "
public static func getStatus(repositoryURL: URL, untrackedFilesOption: UntrackedFilesOption = .all) -> [GitChange] {
let arguments = ["status", "--porcelain", "--untracked-files=\(untrackedFilesOption.rawValue)"]
let result = try? SystemUtils.executeCommand(
inDirectory: repositoryURL.path,
path: GitPath,
arguments: arguments
)
if let result = result {
return Self.parseStatus(statusOutput: result, repositoryURL: repositoryURL)
} else {
return []
}
}
private static func parseStatus(statusOutput: String, repositoryURL: URL) -> [GitChange] {
var changes: [GitChange] = []
let fileManager = FileManager.default
let lines = statusOutput.components(separatedBy: .newlines)
for line in lines {
if line.hasPrefix(unTrackedFilePrefix) {
let fileRelativePath = String(line.dropFirst(unTrackedFilePrefix.count))
let fileURL = repositoryURL.appendingPathComponent(fileRelativePath)
guard fileManager.fileExists(atPath: fileURL.path) else { continue }
changes.append(
.init(url: fileURL, originalURL: fileURL, status: .untracked)
)
}
}
return changes
}
}