@@ -312,12 +312,35 @@ public struct Branch { // swiftlint:disable:this type_body_length
312
312
313
313
/// Creates a new branch in the specified directory.
314
314
///
315
+ /// This function creates a new branch in the specified Git repository directory. It allows
316
+ /// for an optional starting point for the new branch and an option to prevent tracking the
317
+ /// new branch.
318
+ ///
315
319
/// - Parameters:
316
320
/// - directoryURL: The URL of the directory where the Git repository is located.
317
321
/// - name: A string representing the name of the new branch.
318
322
/// - startPoint: An optional string representing the starting point for the new branch.
319
323
/// - noTrack: A boolean indicating whether to track the branch. Defaults to false.
320
324
/// - Throws: An error if the shell command fails.
325
+ ///
326
+ /// - Example:
327
+ /// ```swift
328
+ /// do {
329
+ /// let directoryURL = URL(fileURLWithPath: "/path/to/repository")
330
+ /// let branchName = "new-feature-branch"
331
+ /// let startPoint = "main"
332
+ /// let noTrack = true
333
+ /// try createBranch(directoryURL: directoryURL, name: branchName, startPoint: startPoint, noTrack: noTrack)
334
+ /// print("Branch created successfully.")
335
+ /// } catch {
336
+ /// print("Failed to create branch: \(error)")
337
+ /// }
338
+ /// ```
339
+ ///
340
+ /// - Note:
341
+ /// If `noTrack` is set to `true`, the new branch will not track the remote branch from
342
+ /// which it was created. This can be useful when branching directly from a remote branch
343
+ /// to avoid automatically pushing to the remote branch's upstream.
321
344
public func createBranch( directoryURL: URL ,
322
345
name: String ,
323
346
startPoint: String ? ,
@@ -382,23 +405,48 @@ public struct Branch { // swiftlint:disable:this type_body_length
382
405
/// Prepare and execute the Git command to delete the local branch using a ShellClient.
383
406
try GitShell ( ) . git ( args: args,
384
407
path: directoryURL,
385
- name: " deleteLocalBranch " )
408
+ name: #function )
386
409
387
410
// Return true to indicate that the branch deletion was attempted.
388
411
return true
389
412
}
390
413
391
414
/// Deletes a remote branch in the specified directory.
392
415
///
416
+ /// This function deletes a remote branch in the specified Git repository directory. It uses the `git push`
417
+ /// command with a colon (`:`) in front of the branch name to delete the branch on the remote repository.
418
+ ///
419
+ /// If the deletion fails due to an authentication error or if the branch has already been deleted on the
420
+ /// remote, the function attempts to delete the local reference to the remote branch.
421
+ ///
393
422
/// - Parameters:
394
423
/// - directoryURL: The URL of the directory where the Git repository is located.
395
424
/// - remoteName: A string representing the name of the remote repository.
396
425
/// - remoteBranchName: A string representing the name of the branch to delete.
397
426
/// - Throws: An error if the shell command fails.
427
+ ///
428
+ /// - Example:
429
+ /// ```swift
430
+ /// do {
431
+ /// let directoryURL = URL(fileURLWithPath: "/path/to/repository")
432
+ /// let remoteName = "origin"
433
+ /// let remoteBranchName = "feature-branch"
434
+ /// try deleteRemoteBranch(directoryURL: directoryURL, remoteName: remoteName, remoteBranchName: remoteBranchName)
435
+ /// print("Remote branch deleted successfully.")
436
+ /// } catch {
437
+ /// print("Failed to delete remote branch: \(error)")
438
+ /// }
439
+ /// ```
440
+ ///
441
+ /// - Note:
442
+ /// Ensure that you have the necessary permissions to delete branches on the remote repository. If the
443
+ /// user is not authenticated or lacks the required permissions, the push operation will fail, and the
444
+ /// caller must handle this error appropriately.
398
445
public func deleteRemoteBranch( directoryURL: URL ,
399
446
remoteName: String ,
400
- remoteBranchName: String ) throws {
447
+ remoteBranchName: String ) throws -> Bool {
401
448
let args = [
449
+ gitNetworkArguments. joined ( ) ,
402
450
" push " ,
403
451
remoteName,
404
452
" : \( remoteBranchName) "
@@ -408,7 +456,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
408
456
// Let this propagate and leave it to the caller to handle
409
457
let result = try GitShell ( ) . git ( args: args,
410
458
path: directoryURL,
411
- name: " deleteRemoteBranch " ,
459
+ name: #function ,
412
460
options: IGitExecutionOptions (
413
461
expectedErrors: Set ( [ GitError . BranchDeletionFailed] )
414
462
) )
@@ -421,6 +469,8 @@ public struct Branch { // swiftlint:disable:this type_body_length
421
469
let ref = " refs/remotes/ \( remoteName) / \( remoteBranchName) "
422
470
try UpdateRef ( ) . deleteRef ( directoryURL: directoryURL, ref: ref, reason: nil )
423
471
}
472
+
473
+ return true
424
474
}
425
475
426
476
/// Finds all branches that point at a specific commitish in the given directory.
0 commit comments