Skip to content

Commit f6ee5b7

Browse files
committed
Update
1 parent 6c4fc82 commit f6ee5b7

1 file changed

Lines changed: 50 additions & 6 deletions

File tree

Tool/Sources/GitIgnoreCheck/GitIgnoreCheck.swift

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Preferences
55

66
public struct CheckIfGitIgnoredDependencyKey: DependencyKey {
77
public static var liveValue: GitIgnoredChecker = DefaultGitIgnoredChecker()
8+
public static var testValue: GitIgnoredChecker = DefaultGitIgnoredChecker(isTest: true)
89
}
910

1011
public extension DependencyValues {
@@ -16,25 +17,44 @@ public extension DependencyValues {
1617

1718
public protocol GitIgnoredChecker {
1819
func checkIfGitIgnored(fileURL: URL) async -> Bool
20+
func checkIfGitIgnored(fileURLs: [URL]) async -> [URL]
1921
}
2022

21-
extension GitIgnoredChecker {
23+
public extension GitIgnoredChecker {
2224
func checkIfGitIgnored(filePath: String) async -> Bool {
2325
await checkIfGitIgnored(fileURL: URL(fileURLWithPath: filePath))
2426
}
27+
28+
func checkIfGitIgnored(filePaths: [String]) async -> [String] {
29+
await checkIfGitIgnored(fileURLs: filePaths.map { URL(fileURLWithPath: $0) })
30+
.map(\.path)
31+
}
2532
}
2633

27-
struct DefaultGitIgnoredChecker: GitIgnoredChecker {
28-
func checkIfGitIgnored(fileURL: URL) async -> Bool {
29-
if UserDefaults.shared.value(for: \.disableGitIgnoreCheck) { return false }
34+
public struct DefaultGitIgnoredChecker: GitIgnoredChecker {
35+
var isTest = false
36+
37+
var noCheck: Bool {
38+
if isTest { return false }
39+
return UserDefaults.shared.value(for: \.disableGitIgnoreCheck)
40+
}
41+
42+
public init() {}
43+
44+
init(isTest: Bool) {
45+
self.isTest = isTest
46+
}
47+
48+
public func checkIfGitIgnored(fileURL: URL) async -> Bool {
49+
if noCheck { return false }
3050
let terminal = Terminal()
3151
guard let gitFolderURL = gitFolderURL(forFileURL: fileURL) else {
3252
return false
3353
}
3454
do {
3555
_ = try await terminal.runCommand(
3656
"/bin/bash",
37-
arguments: ["-c", "check-ignore \"filePath\""],
57+
arguments: ["-c", "check-ignore \"\(fileURL.path)\""],
3858
currentDirectoryPath: gitFolderURL.path,
3959
environment: [:]
4060
)
@@ -43,6 +63,30 @@ struct DefaultGitIgnoredChecker: GitIgnoredChecker {
4363
return false
4464
}
4565
}
66+
67+
public func checkIfGitIgnored(fileURLs: [URL]) async -> [URL] {
68+
if noCheck { return [] }
69+
let filePaths = fileURLs.map { "\"\($0.path)\"" }.joined(separator: " ")
70+
guard let firstFileURL = fileURLs.first else { return [] }
71+
let terminal = Terminal()
72+
guard let gitFolderURL = gitFolderURL(forFileURL: firstFileURL) else {
73+
return []
74+
}
75+
do {
76+
let result = try await terminal.runCommand(
77+
"/bin/bash",
78+
arguments: ["-c", "check-ignore \(filePaths)"],
79+
currentDirectoryPath: gitFolderURL.path,
80+
environment: [:]
81+
)
82+
return result
83+
.split(separator: "\n")
84+
.map(String.init)
85+
.compactMap(URL.init(fileURLWithPath:))
86+
} catch {
87+
return []
88+
}
89+
}
4690
}
4791

4892
func gitFolderURL(forFileURL fileURL: URL) -> URL? {
@@ -51,7 +95,7 @@ func gitFolderURL(forFileURL fileURL: URL) -> URL? {
5195
while currentURL.path != "/" {
5296
let gitFolderURL = currentURL.appendingPathComponent(".git")
5397
if fileManager.fileExists(atPath: gitFolderURL.path) {
54-
return gitFolderURL
98+
return currentURL
5599
}
56100
currentURL = currentURL.deletingLastPathComponent()
57101
}

0 commit comments

Comments
 (0)