@@ -18,10 +18,19 @@ public struct Branch { // swiftlint:disable:this type_body_length
1818 /// - Parameter directoryURL: The URL of the directory where the Git repository is located.
1919 /// - Returns: A string representing the name of the current branch.
2020 /// - 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 ( )
2534 }
2635
2736 /// Fetches all branches in the given directory, optionally filtering by prefixes.
@@ -31,7 +40,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
3140 /// - prefixes: An array of strings representing branch name prefixes to filter by. Defaults to an empty array.
3241 /// - Returns: An array of `GitBranch` instances representing the fetched branches.
3342 /// - 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 ] {
3544 let fields = [ " fullName " : " %(refname) " ,
3645 " shortName " : " %(refname:short) " ,
3746 " upstreamShortName " : " %(upstream:short) " ,
@@ -51,7 +60,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
5160 let gitCommand = [ " for-each-ref " ] + args + prefixArgs
5261
5362 // Execute the git command using the GitShell utility
54- let result = try GitShell ( ) . git (
63+ let result = try await GitShell ( ) . git (
5564 args: gitCommand,
5665 path: directoryURL,
5766 name: #function,
@@ -177,77 +186,6 @@ public struct Branch { // swiftlint:disable:this type_body_length
177186 return eligibleBranches
178187 }
179188
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-
251189 func getCommitsOnBranch( ) {
252190 guard let noCommitsOnBranchRe = try ? NSRegularExpression (
253191 pattern: " fatal: your current branch '.*' does not have any commits yet "
@@ -257,59 +195,6 @@ public struct Branch { // swiftlint:disable:this type_body_length
257195 }
258196 }
259197
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-
313198 /// Creates a new branch in the specified directory.
314199 ///
315200 /// 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
446331 remoteName: String ,
447332 remoteBranchName: String ) throws -> Bool {
448333 let args = [
449- gitNetworkArguments. joined ( ) ,
450334 " push " ,
451335 remoteName,
452336 " : \( remoteBranchName) "
0 commit comments