forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitIgnoreCheck.swift
More file actions
105 lines (92 loc) · 3.24 KB
/
GitIgnoreCheck.swift
File metadata and controls
105 lines (92 loc) · 3.24 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Dependencies
import Foundation
import Terminal
import Preferences
public struct CheckIfGitIgnoredDependencyKey: DependencyKey {
public static var liveValue: GitIgnoredChecker = DefaultGitIgnoredChecker()
public static var testValue: GitIgnoredChecker = DefaultGitIgnoredChecker(isTest: true)
}
public extension DependencyValues {
var gitIgnoredChecker: GitIgnoredChecker {
get { self[CheckIfGitIgnoredDependencyKey.self] }
set { self[CheckIfGitIgnoredDependencyKey.self] = newValue }
}
}
public protocol GitIgnoredChecker {
func checkIfGitIgnored(fileURL: URL) async -> Bool
func checkIfGitIgnored(fileURLs: [URL]) async -> [URL]
}
public extension GitIgnoredChecker {
func checkIfGitIgnored(filePath: String) async -> Bool {
await checkIfGitIgnored(fileURL: URL(fileURLWithPath: filePath))
}
func checkIfGitIgnored(filePaths: [String]) async -> [String] {
await checkIfGitIgnored(fileURLs: filePaths.map { URL(fileURLWithPath: $0) })
.map(\.path)
}
}
public struct DefaultGitIgnoredChecker: GitIgnoredChecker {
var isTest = false
var noCheck: Bool {
if isTest { return true }
return UserDefaults.shared.value(for: \.disableGitIgnoreCheck)
}
public init() {}
init(isTest: Bool) {
self.isTest = isTest
}
public func checkIfGitIgnored(fileURL: URL) async -> Bool {
if noCheck { return false }
let terminal = Terminal()
guard let gitFolderURL = gitFolderURL(forFileURL: fileURL) else {
return false
}
do {
let result = try await terminal.runCommand(
"/bin/bash",
arguments: ["-c", "git check-ignore \"\(fileURL.path)\""],
currentDirectoryURL: gitFolderURL,
environment: [:]
)
if result.isEmpty { return false }
return true
} catch {
return false
}
}
public func checkIfGitIgnored(fileURLs: [URL]) async -> [URL] {
if noCheck { return [] }
let filePaths = fileURLs.map { "\"\($0.path)\"" }.joined(separator: " ")
guard let firstFileURL = fileURLs.first else { return [] }
let terminal = Terminal()
guard let gitFolderURL = gitFolderURL(forFileURL: firstFileURL) else {
return []
}
do {
let result = try await terminal.runCommand(
"/bin/bash",
arguments: ["-c", "git check-ignore \(filePaths)"],
currentDirectoryURL: gitFolderURL,
environment: [:]
)
return result
.split(whereSeparator: \.isNewline)
.map(String.init)
.compactMap(URL.init(fileURLWithPath:))
} catch {
return []
}
}
}
func gitFolderURL(forFileURL fileURL: URL) -> URL? {
var currentURL = fileURL
let fileManager = FileManager.default
while currentURL.path != "/" {
let gitFolderURL = currentURL.appendingPathComponent(".git")
if fileManager.fileExists(atPath: gitFolderURL.path) {
return currentURL
}
currentURL = currentURL.deletingLastPathComponent()
}
return nil
}