-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSourceControlGitView.swift
More file actions
232 lines (212 loc) Β· 8.08 KB
/
SourceControlGitView.swift
File metadata and controls
232 lines (212 loc) Β· 8.08 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// SourceControlGitView.swift
// CodeEdit
//
// Created by Raymond Vleeshouwer on 02/04/23.
//
import SwiftUI
struct SourceControlGitView: View {
@AppSettings(\.sourceControl.git)
var git
let gitConfig = GitConfigClient(shellClient: currentWorld.shellClient)
@State private var authorName: String = ""
@State private var authorEmail: String = ""
@State private var defaultBranch: String = ""
@State private var preferRebaseWhenPulling: Bool = false
@State private var hasAppeared: Bool = false
@State private var resolvedGitIgnorePath: String = "~/.gitignore_global"
var body: some View {
Group {
Section {
gitAuthorName
gitEmail
} header: {
Text("Git Configuration")
Text("""
Applied globally to all repositories on your Mac. \
[Learn more...](https://git-scm.com/docs/git-config)
""")
}
Section {
defaultBranchName
preferToRebaseWhenPulling
showMergeCommitsInPerFileLog
}
Section {
gitConfigEditor
}
Section {
IgnoredFilesListView()
} header: {
Text("Ignored Files")
Text("""
Patterns for files and folders that Git should ignore and not track. \
Applied globally to all repositories on your Mac. \
[Learn more...](https://git-scm.com/docs/gitignore)
""")
}
Section {
gitIgnoreEditor
}
}
.onAppear {
// Intentionally using an onAppear with a Task instead of just a .task modifier.
// When we did this it was executing too often.
Task {
authorName = try await gitConfig.get(key: "user.name", global: true) ?? ""
authorEmail = try await gitConfig.get(key: "user.email", global: true) ?? ""
defaultBranch = try await gitConfig.get(key: "init.defaultBranch", global: true) ?? ""
preferRebaseWhenPulling = try await gitConfig.get(key: "pull.rebase", global: true) ?? false
try? await Task.sleep(for: .milliseconds(0))
hasAppeared = true
}
}
}
}
private extension SourceControlGitView {
private var gitAuthorName: some View {
TextField("Author Name", text: $authorName)
.onChange(of: authorName) { newValue in
if hasAppeared {
Limiter.debounce(id: "authorNameDebouncer", duration: 0.5) {
Task {
await gitConfig.set(key: "user.name", value: newValue, global: true)
}
}
}
}
}
private var gitEmail: some View {
TextField("Author Email", text: $authorEmail)
.onChange(of: authorEmail) { newValue in
if hasAppeared {
Limiter.debounce(id: "authorEmailDebouncer", duration: 0.5) {
Task {
await gitConfig.set(key: "user.email", value: newValue, global: true)
}
}
}
}
}
private var defaultBranchName: some View {
TextField(text: $defaultBranch) {
Text("Default branch name")
Text("Cannot contain spaces, backslashes, or other symbols")
}
.onChange(of: defaultBranch) { newValue in
if hasAppeared {
Limiter.debounce(id: "defaultBranchDebouncer", duration: 0.5) {
Task {
await gitConfig.set(key: "init.defaultBranch", value: newValue, global: true)
}
}
}
}
}
private var preferToRebaseWhenPulling: some View {
Toggle(
"Prefer to rebase when pulling",
isOn: $preferRebaseWhenPulling
)
.onChange(of: preferRebaseWhenPulling) { newValue in
if hasAppeared {
Limiter.debounce(id: "pullRebaseDebouncer", duration: 0.5) {
Task {
await gitConfig.set(key: "pull.rebase", value: newValue, global: true)
}
}
}
}
}
private var showMergeCommitsInPerFileLog: some View {
Toggle(
"Show merge commits in per-file log",
isOn: $git.showMergeCommitsPerFileLog
)
}
private var gitConfigEditor: some View {
HStack {
Text("Git configuration is stored in \"~/.gitconfig\".")
.font(.subheadline)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
Button("Open in Editor...", action: openGitConfigFile)
}
.frame(maxWidth: .infinity)
}
private var gitIgnoreEditor: some View {
HStack {
Text("Ignored file patterns are stored in \"\(resolvedGitIgnorePath)\".")
.font(.subheadline)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
Button("Open in Editor...", action: openGitIgnoreFile)
}
.frame(maxWidth: .infinity)
.onAppear {
Task {
resolvedGitIgnorePath = await gitIgnorePath()
}
}
}
private var gitIgnoreURL: URL {
get async throws {
if let excludesfile: String = try await gitConfig.get(
key: "core.excludesfile",
global: true
), !excludesfile.isEmpty {
if excludesfile.starts(with: "~/") {
let relativePath = String(excludesfile.dropFirst(2))
return FileManager.default.homeDirectoryForCurrentUser.appending(path: relativePath)
} else if excludesfile.starts(with: "/") {
return URL(fileURLWithPath: excludesfile)
} else {
return FileManager.default.homeDirectoryForCurrentUser.appending(path: excludesfile)
}
} else {
let defaultURL = FileManager.default.homeDirectoryForCurrentUser.appending(
path: ".gitignore_global"
)
await gitConfig.set(key: "core.excludesfile", value: "~/\(defaultURL.lastPathComponent)", global: true)
return defaultURL
}
}
}
private func gitIgnorePath() async -> String {
do {
let url = try await gitIgnoreURL
return url.path.replacingOccurrences(of: FileManager.default.homeDirectoryForCurrentUser.path, with: "~")
} catch {
return "~/.gitignore_global"
}
}
private func openGitConfigFile() {
let fileURL = FileManager.default.homeDirectoryForCurrentUser.appending(path: ".gitconfig")
if !FileManager.default.fileExists(atPath: fileURL.path) {
FileManager.default.createFile(atPath: fileURL.path, contents: nil)
}
NSDocumentController.shared.openDocument(
withContentsOf: fileURL,
display: true
) { _, _, error in
if let error = error {
print("Failed to open document: \(error.localizedDescription)")
}
}
}
private func openGitIgnoreFile() {
Task {
do {
let fileURL = try await gitIgnoreURL
// Ensure the file exists
if !FileManager.default.fileExists(atPath: fileURL.path) {
FileManager.default.createFile(atPath: fileURL.path, contents: nil)
}
// Open the file in the editor
try await NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true)
} catch {
print("Failed to open document: \(error.localizedDescription)")
}
}
}
}