-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemote.swift
438 lines (413 loc) · 18.8 KB
/
Remote.swift
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//
// Remote.swift
// AuroraEditor
//
// Created by Nanashi Li on 2022/08/12.
// Copyright © 2022 Aurora Company. All rights reserved.
// This source code is restricted for Aurora Editor usage only.
//
import Foundation
public struct Remote {
public init() {}
/// Retrieve a list of Git remotes associated with a local repository.
///
/// This function lists the Git remotes configured for a given local Git repository. \
/// It returns an array of `GitRemote` objects, each representing a remote repository with a name and URL.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository for which to retrieve the remotes.
///
/// - Throws: An error if there is a problem accessing the Git repository or \
/// executing the Git command to retrieve remotes.
///
/// - Returns: An array of `GitRemote` objects representing the Git remotes configured for the local repository.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
///
/// do {
/// let remotes = try getRemotes(directoryURL: localRepositoryURL)
/// print("Git Remotes:")
/// for remote in remotes {
/// print("Name: \(remote.name), URL: \(remote.url)")
/// }
/// } catch {
/// print("Error retrieving remotes: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the Git command `git remote -v`\
/// to retrieve the list of Git remotes configured for the local repository.
///
/// - Warning: Ensure that the provided `directoryURL` points to a valid local Git repository directory.\
/// Failure to do so may result in errors or incorrect results.
///
/// - Returns: An array of `GitRemote` objects representing the configured Git remotes for the local repository.
public func getRemotes(directoryURL: URL) throws -> [GitRemote] {
let result = try GitShell().git(args: ["remote", "-v"],
path: directoryURL,
name: #function)
// Check for Git errors
if let gitError = result.gitError, gitError == .NotAGitRepository {
return []
}
// Process the output into an array of IRemote
let output = result.stdout
let lines = output.components(separatedBy: "\n")
let remotes = lines
.filter { $0.contains("(fetch)") }
.map { $0.split(whereSeparator: { $0.isWhitespace }).map(String.init) }
.compactMap { parts in
// Ensure we have at least two parts: remote name and URL
if parts.count >= 2 {
return GitRemote(name: parts[0], url: parts[1])
}
return nil
}
return remotes
}
/// Add a new Git remote repository with the specified name and URL.
///
/// Use this function to add a new Git remote repository to the local repository,
/// providing a name for the remote and its URL. \
/// If the remote repository with the given name already exists,
/// the function updates the URL of the existing remote to the new URL.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository where the remote will be added.
/// - name: The name for the new remote repository.
/// - url: The URL of the remote Git repository.
///
/// - Throws: An error if there is a problem accessing the Git repository or \
/// executing the Git command to add the remote repository.
///
/// - Returns: A `GitRemote` object representing the added remote repository with its name and URL.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remoteName = "my-remote"
/// let remoteURL = "https://github.com/myusername/myrepo.git"
///
/// do {
/// let addedRemote = try addRemote(directoryURL: localRepositoryURL, name: remoteName, url: remoteURL)
/// print("Remote '\(addedRemote.name)' added with URL: \(addedRemote.url)")
/// } catch {
/// print("Error adding remote: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the Git command `git remote add <remote-name> <remote-url>` \
/// to add or update a remote repository. \
/// If a remote with the specified name already exists, it updates the URL to the new URL.
///
/// - Warning: Be cautious when passing user-provided remote names and URLs to this function, \
/// as it may execute arbitrary Git commands. \
/// Ensure that input is properly validated and sanitized to prevent command injection vulnerabilities.
///
/// - Returns: A `GitRemote` object representing the added or updated remote repository with its name and URL.
public func addRemote(directoryURL: URL, name: String, url: String) throws -> GitRemote? {
try GitShell().git(args: ["remote", "add", name, url],
path: directoryURL,
name: #function)
return GitRemote(name: name, url: url)
}
/// Remove an existing Git remote repository by its name or silently ignore if it doesn't exist.
///
/// Use this function to remove an existing Git remote repository specified by its remote name.\
/// If the remote with the given name does not exist, the function silently ignores the operation.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository.
/// - name: The name of the remote repository to remove.
///
/// - Throws: An error if there is a problem accessing the Git repository or \
/// executing the Git command to remove the remote repository.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remoteName = "my-remote"
///
/// do {
/// try removeRemote(directoryURL: localRepositoryURL, name: remoteName)
/// print("Remote '\(remoteName)' successfully removed or does not exist.")
/// } catch {
/// print("Error removing remote: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the Git command `git remote remove <remote-name>` to \
/// remove the remote repository. \
/// If the remote with the specified name does not exist, it does not raise an error.
///
/// - Warning: Be cautious when passing user-provided remote names to this function, \
/// as it may execute arbitrary Git commands. \
/// Ensure that input is properly validated and sanitized to prevent command injection vulnerabilities.
public func removeRemote(directoryURL: URL, name: String) throws {
try GitShell().git(args: ["remote", "remove", name],
path: directoryURL,
name: #function,
options: IGitExecutionOptions(successExitCodes: Set([0, 2, 128])))
}
/// Change the URL of a Git remote repository by its name.
///
/// Use this function to update the URL of a Git remote repository specified by its remote name. \
/// The function executes a Git command to change the URL of the specified remote repository.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository.
/// - name: The name of the remote repository for which to change the URL.
/// - url: The new URL to set for the remote Git repository.
///
/// - Throws: An error if there is a problem accessing the Git repository or \
/// executing the Git command to change the remote's URL.
///
/// - Returns: `true` if the URL was successfully updated; otherwise, `false`.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remoteName = "origin"
/// let newRemoteURL = "https://new-remote-url.com/repository.git"
///
/// do {
/// if try setRemoteURL(directoryURL: localRepositoryURL, name: remoteName, url: newRemoteURL) {
/// print("URL for remote '\(remoteName)' successfully updated.")
/// } else {
/// print("Failed to update URL for remote '\(remoteName)'.")
/// }
/// } catch {
/// print("Error changing remote URL: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the Git command `git remote set-url <remote-name> <new-url>` to\
/// change the URL of the specified remote repository.
/// - It returns `true` if the URL was successfully updated, and `false` otherwise.
///
/// - Warning: Be cautious when passing user-provided remote names and URLs to this function, \
/// as it may execute arbitrary Git commands. \
/// Ensure that input is properly validated and sanitized to prevent command injection vulnerabilities.
public func setRemoteURL(directoryURL: URL, name: String, url: String) throws -> Bool {
try GitShell().git(args: ["remote", "set-url", name, url],
path: directoryURL,
name: #function)
return true
}
/// Get the URL associated with a Git remote repository by its name.
///
/// Use this function to retrieve the URL of a Git remote repository specified by its remote name. \
/// The function executes a Git command to obtain the URL of the specified remote repository.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository.
/// - name: The name of the remote repository for which to fetch the URL.
///
/// - Throws: An error if there is a problem accessing the Git repository or \
/// executing the Git command to retrieve the remote's URL.
///
/// - Returns: A string representing the URL of the remote Git repository, \
/// or `nil` if the specified remote repository could not be found.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remoteName = "origin"
///
/// do {
/// if let remoteURL = try getRemoteURL(directoryURL: localRepositoryURL, name: remoteName) {
/// print("URL for remote '\(remoteName)': \(remoteURL)")
/// } else {
/// print("Remote '\(remoteName)' not found.")
/// }
/// } catch {
/// print("Error retrieving remote URL: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the Git command `git remote get-url <remote-name>` to \
/// retrieve the URL of the specified remote repository.
/// - If the specified remote repository does not exist, the function returns `nil`.
///
/// - Warning: Be cautious when passing user-provided remote names to this function, \
/// as it may execute arbitrary Git commands. \
/// Ensure that input is properly validated and sanitized to prevent command injection vulnerabilities.
public func getRemoteURL(directoryURL: URL, name: String) async throws -> String? {
let result = try await GitShell().git(args: ["remote",
"get-url",
name],
path: directoryURL,
name: #function,
options: IGitExecutionOptions(successExitCodes: Set([0, 2, 128])))
if result.exitCode != 0 {
return nil
}
return result.stdout
}
/// Update the HEAD reference for a remote Git repository.
///
/// Use this function to update the HEAD reference of a remote Git repository associated
/// with a specific remote name. \
/// The function executes a Git command to set the HEAD reference of the remote repository
/// to its default branch (usually the main branch).
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository.
/// - remote: An instance of `IRemote` representing the remote repository to update.
///
/// - Throws: An error if there is a problem accessing the Git repository or executing \
/// the Git command to update the remote's HEAD reference.
///
/// - Note:
/// - The function executes the Git command `git remote set-head -a <remote-name>` \
/// to update the remote's HEAD reference to its default branch. \
/// This operation is typically used to synchronize the remote's HEAD reference with its default branch.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remote = GitRemote(name: "origin", url: "https://github.com/your/repo.git")
///
/// do {
/// try updateRemoteHEAD(directoryURL: localRepositoryURL, remote: remote)
/// print("Remote HEAD reference updated for remote '\(remote.name)'.")
/// } catch {
/// print("Error updating remote HEAD reference: \(error)")
/// }
/// ```
public func updateRemoteHEAD(directoryURL: URL, remote: IRemote) throws {
try GitShell().git(args: [gitNetworkArguments.joined(),
"remote",
"set-head",
"-a",
remote.name],
path: directoryURL,
name: #function,
options: IGitExecutionOptions(successExitCodes: Set([0, 1, 128])))
}
/// Get the name of the HEAD branch in a remote Git repository.
///
/// Use this function to retrieve the name of the HEAD branch in a remote Git repository
/// associated with a specific remote name. \
/// The function constructs the reference path for the remote's HEAD and retrieves it using the `Refs` utility.
///
/// - Parameters:
/// - directoryURL: The URL of the local Git repository.
/// - remote: The name of the remote repository for which to fetch the HEAD branch.
///
/// - Throws: An error if there is a problem accessing the Git repository or retrieving the remote's HEAD reference.
///
/// - Returns: A string representing the name of the HEAD branch in the remote repository, or `nil` if not found.
///
/// - Example:
/// ```swift
/// let localRepositoryURL = URL(fileURLWithPath: "/path/to/local/repository")
/// let remoteName = "origin"
///
/// do {
/// if let remoteHEAD = try getRemoteHEAD(directoryURL: localRepositoryURL, remote: remoteName) {
/// print("Remote HEAD: \(remoteHEAD)")
/// } else {
/// print("Remote HEAD not found for remote '\(remoteName)'.")
/// }
/// } catch {
/// print("Error fetching remote HEAD reference: \(error)")
/// }
/// ```
///
/// - Note:
/// - This function constructs the reference path for the remote's HEAD using the provided \
/// `remote` name and then fetches the reference using the `Refs` utility.
/// - The function returns `nil` if the remote's HEAD reference is not found or \
/// if there is an error in the process.
public func getRemoteHEAD(directoryURL: URL, remote: String) throws -> String? {
let remoteNamespace = "refs/remotes/\(remote)/"
if let match = try Refs().getSymbolicRef(directoryURL: directoryURL, ref: "\(remoteNamespace)HEAD"),
match.count > remoteNamespace.count,
match.starts(with: remoteNamespace) {
return match.substring(remoteNamespace.count)
}
return nil
}
/// Retrieve the latest commit hash and reference for a given remote Git repository and branch.
///
/// This function executes the `git ls-remote` command to fetch the latest commit hash and reference
/// for a specified remote Git repository and branch.
///
/// - Parameters:
/// - repoURL: The URL of the remote Git repository.
/// - branch: The branch for which to fetch the latest commit hash and reference.
///
/// - Throws: An error if there is a problem accessing the Git repository or executing the Git command.
///
/// - Returns: A tuple containing the latest commit hash and reference for the specified branch.
///
/// - Example:
/// ```swift
/// let repoURL = "https://github.com/AuroraEditor/AuroraEditor.git"
/// let branch = "development"
///
/// do {
/// let (commitHash, ref) = try getLatestCommitHashAndRef(repoURL: repoURL, branch: branch)
/// print("Latest Commit Hash: \(commitHash), Reference: \(ref)")
/// } catch {
/// print("Error retrieving latest commit hash and reference: \(error)")
/// }
/// ```
///
/// - Note:
/// - The function executes the `git ls-remote` command with the specified repository URL and branch
/// to fetch the latest commit hash and reference.
/// - Ensure that the provided `repoURL` and `branch` are valid and accessible.
///
/// - Returns: A tuple containing the latest commit hash and reference for the specified branch.
public func getLatestCommitHashAndRef(
directoryURL: URL,
repoURL: String,
branch: String
) async throws -> (commitHash: String, ref: String) {
let result = try await GitShell().git(
args: [
"ls-remote",
repoURL,
branch
],
path: directoryURL,
name: #function
)
guard result.exitCode == 0 else {
throw NSError(
domain: "Remote",
code: Int(
result.exitCode
),
userInfo: [NSLocalizedDescriptionKey: "Failed to execute git command"]
)
}
guard let line = result.stdout.split(separator: "\n").first else {
throw NSError(
domain: "Remote",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "No output from git command"]
)
}
let parts = line.split(separator: "\t")
guard parts.count == 2 else {
throw NSError(
domain: "Remote",
code: 2,
userInfo: [NSLocalizedDescriptionKey: "Unexpected output format"]
)
}
let commitHash = String(parts[0])
let ref = String(parts[1])
return (commitHash, ref)
}
}