@@ -18,10 +18,19 @@ public struct Branch { // swiftlint:disable:this type_body_length
18
18
/// - Parameter directoryURL: The URL of the directory where the Git repository is located.
19
19
/// - Returns: A string representing the name of the current branch.
20
20
/// - Throws: An error if the shell command fails.
21
- public func getCurrentBranch( directoryURL: URL ) throws -> String {
22
- return try ShellClient . live ( ) . run (
23
- " cd \( directoryURL. relativePath. escapedWhiteSpaces ( ) ) ;git branch --show-current "
24
- ) . removingNewLines ( )
21
+ public func getCurrentBranch( directoryURL: URL ) async throws -> String {
22
+ let args = [
23
+ " branch " ,
24
+ " --show-current "
25
+ ]
26
+
27
+ let result = try await GitShell ( ) . git (
28
+ args: args,
29
+ path: directoryURL,
30
+ name: #function
31
+ )
32
+
33
+ return result. stdout. removingNewLines ( )
25
34
}
26
35
27
36
/// Fetches all branches in the given directory, optionally filtering by prefixes.
@@ -31,7 +40,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
31
40
/// - prefixes: An array of strings representing branch name prefixes to filter by. Defaults to an empty array.
32
41
/// - Returns: An array of `GitBranch` instances representing the fetched branches.
33
42
/// - Throws: An error if the shell command fails.
34
- public func getBranches( directoryURL: URL , prefixes: [ String ] = [ ] ) throws -> [ GitBranch ] {
43
+ public func getBranches( directoryURL: URL , prefixes: [ String ] = [ ] ) async throws -> [ GitBranch ] {
35
44
let fields = [ " fullName " : " %(refname) " ,
36
45
" shortName " : " %(refname:short) " ,
37
46
" upstreamShortName " : " %(upstream:short) " ,
@@ -51,7 +60,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
51
60
let gitCommand = [ " for-each-ref " ] + args + prefixArgs
52
61
53
62
// Execute the git command using the GitShell utility
54
- let result = try GitShell ( ) . git (
63
+ let result = try await GitShell ( ) . git (
55
64
args: gitCommand,
56
65
path: directoryURL,
57
66
name: #function,
@@ -177,77 +186,6 @@ public struct Branch { // swiftlint:disable:this type_body_length
177
186
return eligibleBranches
178
187
}
179
188
180
- /// Retrieves a list of the most recently modified branches, up to a specified limit.
181
- ///
182
- /// - Parameters:
183
- /// - directoryURL: The URL of the directory where the Git repository is located.
184
- /// - limit: An integer specifying the maximum number of branches to retrieve.
185
- /// - Returns: An array of strings representing the names of the recent branches.
186
- /// - Throws: An error if the shell command fails.
187
- public func getRecentBranches( directoryURL: URL , limit: Int ) throws -> [ String ] {
188
- let regex = try NSRegularExpression (
189
- // swiftlint:disable:next line_length
190
- pattern: #"^.*? (renamed|checkout)(?:: moving from|\s*) (?:refs/heads/|\s*)(.*?) to (?:refs/heads/|\s*)(.*?)$"# ,
191
- options: [ ]
192
- )
193
-
194
- let args = [
195
- " log " ,
196
- " -g " ,
197
- " --no-abbrev-commit " ,
198
- " --pretty=oneline " ,
199
- " HEAD " ,
200
- " -n " ,
201
- " 2500 " ,
202
- " -- "
203
- ]
204
-
205
- let result = try GitShell ( ) . git ( args: args,
206
- path: directoryURL,
207
- name: #function)
208
-
209
- if result. exitCode == 128 {
210
- // error code 128 is returned if the branch is unborn
211
- return [ ]
212
- }
213
-
214
- let lines = result. stdout. components ( separatedBy: " \n " )
215
- var names = Set < String > ( )
216
- var excludedNames = Set < String > ( )
217
-
218
- for line in lines {
219
- if let match = regex. firstMatch (
220
- in: line,
221
- options: [ ] ,
222
- range: NSRange ( location: 0 , length: line. utf16. count)
223
- ) ,
224
- match. numberOfRanges == 4 {
225
- let operationTypeRange = Range ( match. range ( at: 1 ) , in: line) !
226
- let excludeBranchNameRange = Range ( match. range ( at: 2 ) , in: line) !
227
- let branchNameRange = Range ( match. range ( at: 3 ) , in: line) !
228
-
229
- let operationType = String ( line [ operationTypeRange] )
230
- let excludeBranchName = String ( line [ excludeBranchNameRange] )
231
- let branchName = String ( line [ branchNameRange] )
232
-
233
- if operationType == " renamed " {
234
- // exclude intermediate-state renaming branch from recent branches
235
- excludedNames. insert ( excludeBranchName)
236
- }
237
-
238
- if !excludedNames. contains ( branchName) {
239
- names. insert ( branchName)
240
- }
241
- }
242
-
243
- if names. count >= limit {
244
- break
245
- }
246
- }
247
-
248
- return Array ( names)
249
- }
250
-
251
189
func getCommitsOnBranch( ) {
252
190
guard let noCommitsOnBranchRe = try ? NSRegularExpression (
253
191
pattern: " fatal: your current branch '.*' does not have any commits yet "
@@ -257,59 +195,6 @@ public struct Branch { // swiftlint:disable:this type_body_length
257
195
}
258
196
}
259
197
260
- /// Asynchronously fetches the names and dates of branches checked out after a specified date.
261
- ///
262
- /// - Parameters:
263
- /// - directoryURL: The URL of the directory where the Git repository is located.
264
- /// - afterDate: A `Date` object representing the starting point for the search.
265
- /// - Returns: A dictionary mapping branch names to the dates they were checked out.
266
- /// - Throws: An error if the shell command fails.
267
- func getBranchCheckouts( directoryURL: URL , afterDate: Date ) async throws -> [ String : Date ] {
268
- let regexPattern = #"^[a-z0-9]{40}\sHEAD@{(.*)}\scheckout: moving from\s.*\sto\s(.*)$"# // regexr.com/46n1v
269
- let regex = try NSRegularExpression ( pattern: regexPattern, options: [ ] )
270
-
271
- let args = [
272
- " reflog " ,
273
- " --date=iso " ,
274
- " --after= \( afterDate. timeIntervalSince1970) " ,
275
- " --pretty=%H %gd %gs " ,
276
- " --grep-reflog=checkout: moving from .* to .*$ " ,
277
- " -- "
278
- ]
279
-
280
- let result = try GitShell ( ) . git ( args: args,
281
- path: directoryURL,
282
- name: #function)
283
-
284
- var checkouts = [ String: Date] ( )
285
-
286
- if result. exitCode == 128 {
287
- return checkouts
288
- }
289
-
290
- let lines = result. stdout. components ( separatedBy: " \n " )
291
- for line in lines {
292
- if let match = regex. firstMatch (
293
- in: line,
294
- options: [ ] ,
295
- range: NSRange ( location: 0 , length: line. utf16. count)
296
- ) ,
297
- match. numberOfRanges == 3 {
298
- let timestampRange = Range ( match. range ( at: 1 ) , in: line) !
299
- let branchNameRange = Range ( match. range ( at: 2 ) , in: line) !
300
-
301
- let timestampString = String ( line [ timestampRange] )
302
- let branchName = String ( line [ branchNameRange] )
303
-
304
- if let timestamp = ISO8601DateFormatter ( ) . date ( from: timestampString) {
305
- checkouts [ branchName] = timestamp
306
- }
307
- }
308
- }
309
-
310
- return checkouts
311
- }
312
-
313
198
/// Creates a new branch in the specified directory.
314
199
///
315
200
/// This function creates a new branch in the specified Git repository directory. It allows
@@ -446,7 +331,6 @@ public struct Branch { // swiftlint:disable:this type_body_length
446
331
remoteName: String ,
447
332
remoteBranchName: String ) throws -> Bool {
448
333
let args = [
449
- gitNetworkArguments. joined ( ) ,
450
334
" push " ,
451
335
remoteName,
452
336
" : \( remoteBranchName) "
0 commit comments