Skip to content

Commit 987c3d4

Browse files
committed
It is now possible to switch to a remote branch and create a new branch from a remote branch. We are now disabling rename branch for remote branches.
1 parent 84b9199 commit 987c3d4

File tree

6 files changed

+24
-27
lines changed

6 files changed

+24
-27
lines changed

CodeEdit/Features/NavigatorArea/SourceControlNavigator/Repository/Views/SourceControlNavigatorRepositoryView+contextMenu.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension SourceControlNavigatorRepositoryView {
4747
showRenameBranch = true
4848
fromBranch = item.branch
4949
}
50-
.disabled(item.branch == nil)
50+
.disabled(item.branch == nil || item.branch?.isRemote == true)
5151
Divider()
5252
Button("Add Existing Remote...") {
5353
sourceControlManager.addExistingRemoteSheetIsPresented = true

CodeEdit/Features/NavigatorArea/SourceControlNavigator/Repository/Views/SourceControlNavigatorRepositoryView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ struct SourceControlNavigatorRepositoryView: View {
7070
)
7171
.sheet(isPresented: $showNewBranch) {
7272
SourceControlNavigatorNewBranchView(
73-
fromBranch: fromBranch
73+
fromBranch: $fromBranch
7474
)
7575
}
7676
.sheet(isPresented: $showRenameBranch) {
7777
SourceControlNavigatorRenameBranchView(
78-
fromBranch: fromBranch
78+
fromBranch: $fromBranch
7979
)
8080
}
8181
.alert(

CodeEdit/Features/SourceControl/Client/GitClient+Branches.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,6 @@ extension GitClient {
7878
_ = try await run("branch -d \(branch.name)")
7979
}
8080

81-
/// Create new branch
82-
func newBranch(name: String, from: GitBranch) async throws {
83-
if !from.isLocal {
84-
return
85-
}
86-
87-
_ = try await run("checkout -b \(name) \(from.name)")
88-
}
89-
9081
/// Rename branch
9182
/// - Parameter from: Name of the branch to rename
9283
/// - Parameter to: New name for branch
@@ -96,15 +87,18 @@ extension GitClient {
9687

9788
/// Checkout branch
9889
/// - Parameter branch: Branch to checkout
99-
func checkoutBranch(_ branch: GitBranch, forceLocal: Bool = false) async throws {
90+
func checkoutBranch(_ branch: GitBranch, forceLocal: Bool = false, newName: String? = nil) async throws {
10091
var command = "checkout "
10192

102-
// If branch is remote, try to create local branch
103-
if branch.isRemote {
104-
let localName = branch.name.replacingOccurrences(of: "origin/", with: "")
105-
command += forceLocal ? localName : "-b " + localName + " " + branch.name
93+
let targetName = newName ?? branch.name
94+
95+
if (branch.isRemote && !forceLocal) || newName != nil {
96+
let sourceBranch = branch.isRemote
97+
? branch.longName.replacingOccurrences(of: "refs/remotes/", with: "")
98+
: branch.name
99+
command += "-b \(targetName) \(sourceBranch)"
106100
} else {
107-
command += branch.name
101+
command += targetName
108102
}
109103

110104
do {
@@ -117,8 +111,10 @@ extension GitClient {
117111
// try to switch to local branch
118112
if let error = error as? GitClientError,
119113
branch.isRemote,
120-
error.localizedDescription.contains("already exists") {
114+
error.description.contains("already exists") {
121115
try await checkoutBranch(branch, forceLocal: true)
116+
} else {
117+
print(error)
122118
}
123119
}
124120
}

CodeEdit/Features/SourceControl/SourceControlManager+GitClient.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ extension SourceControlManager {
4747

4848
/// Create new branch, can be created only from local branch
4949
func newBranch(name: String, from: GitBranch) async throws {
50-
if !from.isLocal {
51-
return
52-
}
53-
54-
try await gitClient.newBranch(name: name, from: from)
50+
try await gitClient.checkoutBranch(from, newName: name)
5551
await refreshBranches()
5652
await refreshCurrentBranch()
5753
}

CodeEdit/Features/SourceControl/Views/SourceControlNavigatorNewBranchView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ struct SourceControlNavigatorNewBranchView: View {
1414
@EnvironmentObject var sourceControlManager: SourceControlManager
1515

1616
@State var name: String = ""
17-
let fromBranch: GitBranch?
17+
@Binding var fromBranch: GitBranch?
1818

1919
var body: some View {
2020
if let branch = fromBranch ?? sourceControlManager.currentBranch {
2121
VStack(spacing: 0) {
2222
Form {
2323
Section {
24-
LabeledContent("From", value: branch.name)
24+
LabeledContent(
25+
"From",
26+
value: branch.isRemote
27+
? branch.longName.replacingOccurrences(of: "refs/remotes/", with: "")
28+
: branch.name
29+
)
2530
TextField("To", value: $name, formatter: RegexFormatter(pattern: "[^a-zA-Z0-9_-]"))
2631
} header: {
2732
Text("Create a new branch")

CodeEdit/Features/SourceControl/Views/SourceControlNavigatorRenameBranchView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct SourceControlNavigatorRenameBranchView: View {
1515

1616
@State var name: String = ""
1717

18-
let fromBranch: GitBranch?
18+
@Binding var fromBranch: GitBranch?
1919

2020
var body: some View {
2121
if let branch = fromBranch ?? sourceControlManager.currentBranch {

0 commit comments

Comments
 (0)