Skip to content

Commit

Permalink
Some bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nanashili committed Jul 7, 2024
1 parent db7f9c4 commit 631dc63
Show file tree
Hide file tree
Showing 17 changed files with 544 additions and 41 deletions.
56 changes: 53 additions & 3 deletions Sources/Version-Control/Base/Commands/Branch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,35 @@ public struct Branch { // swiftlint:disable:this type_body_length

/// Creates a new branch in the specified directory.
///
/// This function creates a new branch in the specified Git repository directory. It allows
/// for an optional starting point for the new branch and an option to prevent tracking the
/// new branch.
///
/// - Parameters:
/// - directoryURL: The URL of the directory where the Git repository is located.
/// - name: A string representing the name of the new branch.
/// - startPoint: An optional string representing the starting point for the new branch.
/// - noTrack: A boolean indicating whether to track the branch. Defaults to false.
/// - Throws: An error if the shell command fails.
///
/// - Example:
/// ```swift
/// do {
/// let directoryURL = URL(fileURLWithPath: "/path/to/repository")
/// let branchName = "new-feature-branch"
/// let startPoint = "main"
/// let noTrack = true
/// try createBranch(directoryURL: directoryURL, name: branchName, startPoint: startPoint, noTrack: noTrack)
/// print("Branch created successfully.")
/// } catch {
/// print("Failed to create branch: \(error)")
/// }
/// ```
///
/// - Note:
/// If `noTrack` is set to `true`, the new branch will not track the remote branch from
/// which it was created. This can be useful when branching directly from a remote branch
/// to avoid automatically pushing to the remote branch's upstream.
public func createBranch(directoryURL: URL,
name: String,
startPoint: String?,
Expand Down Expand Up @@ -382,23 +405,48 @@ public struct Branch { // swiftlint:disable:this type_body_length
/// Prepare and execute the Git command to delete the local branch using a ShellClient.
try GitShell().git(args: args,
path: directoryURL,
name: "deleteLocalBranch")
name: #function)

// Return true to indicate that the branch deletion was attempted.
return true
}

/// Deletes a remote branch in the specified directory.
///
/// This function deletes a remote branch in the specified Git repository directory. It uses the `git push`
/// command with a colon (`:`) in front of the branch name to delete the branch on the remote repository.
///
/// If the deletion fails due to an authentication error or if the branch has already been deleted on the
/// remote, the function attempts to delete the local reference to the remote branch.
///
/// - Parameters:
/// - directoryURL: The URL of the directory where the Git repository is located.
/// - remoteName: A string representing the name of the remote repository.
/// - remoteBranchName: A string representing the name of the branch to delete.
/// - Throws: An error if the shell command fails.
///
/// - Example:
/// ```swift
/// do {
/// let directoryURL = URL(fileURLWithPath: "/path/to/repository")
/// let remoteName = "origin"
/// let remoteBranchName = "feature-branch"
/// try deleteRemoteBranch(directoryURL: directoryURL, remoteName: remoteName, remoteBranchName: remoteBranchName)

Check failure on line 434 in Sources/Version-Control/Base/Commands/Branch.swift

View workflow job for this annotation

GitHub Actions / lint

Line should be 120 characters or less; currently it has 124 characters (line_length)
/// print("Remote branch deleted successfully.")
/// } catch {
/// print("Failed to delete remote branch: \(error)")
/// }
/// ```
///
/// - Note:
/// Ensure that you have the necessary permissions to delete branches on the remote repository. If the
/// user is not authenticated or lacks the required permissions, the push operation will fail, and the
/// caller must handle this error appropriately.
public func deleteRemoteBranch(directoryURL: URL,
remoteName: String,
remoteBranchName: String) throws {
remoteBranchName: String) throws -> Bool {
let args = [
gitNetworkArguments.joined(),
"push",
remoteName,
":\(remoteBranchName)"
Expand All @@ -408,7 +456,7 @@ public struct Branch { // swiftlint:disable:this type_body_length
// Let this propagate and leave it to the caller to handle
let result = try GitShell().git(args: args,
path: directoryURL,
name: "deleteRemoteBranch",
name: #function,
options: IGitExecutionOptions(
expectedErrors: Set([GitError.BranchDeletionFailed])
))
Expand All @@ -421,6 +469,8 @@ public struct Branch { // swiftlint:disable:this type_body_length
let ref = "refs/remotes/\(remoteName)/\(remoteBranchName)"
try UpdateRef().deleteRef(directoryURL: directoryURL, ref: ref, reason: nil)
}

return true
}

/// Finds all branches that point at a specific commitish in the given directory.
Expand Down
6 changes: 2 additions & 4 deletions Sources/Version-Control/Base/Commands/Checkout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public struct GitCheckout {
public typealias ProgressCallback = (CheckoutProgress) -> Void

public func getCheckoutArgs(progressCallback: ProgressCallback?) -> [String] {
var args = gitNetworkArguments
// var args = gitNetworkArguments
var args: [String] = []

if let callback = progressCallback {
args += ["checkout", "--progress"]
Expand Down Expand Up @@ -68,7 +69,6 @@ public struct GitCheckout {

public func getCheckoutOpts( // swiftlint:disable:this function_parameter_count

Check failure on line 70 in Sources/Version-Control/Base/Commands/Checkout.swift

View workflow job for this annotation

GitHub Actions / lint

SwiftLint rule 'function_parameter_count' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
directoryURL: URL,
account: IGitAccount?,
title: String,
target: String,
progressCallback: ProgressCallback?,
Expand Down Expand Up @@ -141,7 +141,6 @@ public struct GitCheckout {
) throws -> Bool {
let opts = try getCheckoutOpts(
directoryURL: directoryURL,
account: account,
title: "Checking out branch \(branch.name)",
target: branch.name,
progressCallback: progressCallback,
Expand All @@ -165,7 +164,6 @@ public struct GitCheckout {
progressCallback: ProgressCallback?) async throws -> Bool {
let opts = try getCheckoutOpts(
directoryURL: directoryURL,
account: account,
title: "Checking out Commit",
target: shortenSHA(commit.sha),
progressCallback: progressCallback,
Expand Down
Loading

0 comments on commit 631dc63

Please sign in to comment.