Skip to content

Commit 12387b5

Browse files
committed
Git Clone: Validate url and more error handling
Naive Xcode-like check for the url implemented. For now if the url does not start with certain string, the clone button is disabled, just like in Xcode. Also added check for when user decides to press cancel on folder selection #232 (comment)
1 parent 015eafe commit 12387b5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

CodeEditModules/Modules/GitClone/src/GitCloneView.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public struct GitCloneView: View {
5959
if repoName.contains(".git") {
6060
repoName.removeLast(4)
6161
}
62-
getPath(modifiable: &repoPath, saveName: repoName)
62+
let didChoose = getPath(modifiable: &repoPath, saveName: repoName)
63+
if didChoose == nil {
64+
return
65+
}
6366
let dirUrl = URL(string: repoPath)
6467
var isDir: ObjCBool = true
6568
if FileManager.default.fileExists(atPath: repoPath, isDirectory: &isDir) {
@@ -86,6 +89,7 @@ public struct GitCloneView: View {
8689
}
8790
}
8891
.keyboardShortcut(.defaultAction)
92+
.disabled(!isValid(url: repoUrlStr))
8993
}
9094
.offset(x: 185)
9195
.alignmentGuide(.leading) { context in
@@ -101,7 +105,7 @@ public struct GitCloneView: View {
101105
}
102106

103107
extension GitCloneView {
104-
func getPath(modifiable: inout String, saveName: String) {
108+
func getPath(modifiable: inout String, saveName: String) -> String? {
105109
let dialog = NSSavePanel()
106110
dialog.showsResizeIndicator = true
107111
dialog.showsHiddenFiles = false
@@ -119,12 +123,13 @@ extension GitCloneView {
119123
// path contains the directory path e.g
120124
// /Users/ourcodeworld/Desktop/folder
121125
modifiable = path
126+
return path
122127
}
123128
} else {
124129
// User clicked on "Cancel"
125-
return
130+
return nil
126131
}
127-
132+
return nil
128133
}
129134
func showAlert(alertMsg: String, infoText: String) {
130135
let alert = NSAlert()
@@ -134,4 +139,16 @@ extension GitCloneView {
134139
alert.alertStyle = .warning
135140
alert.runModal()
136141
}
142+
func isValid(url: String) -> Bool {
143+
// Doing the same kind of check that Xcode does when cloning
144+
let url = url.lowercased()
145+
if url.starts(with: "http://") && url.count > 7 {
146+
return true
147+
} else if url.starts(with: "https://") && url.count > 8 {
148+
return true
149+
} else if url.starts(with: "git@") && url.count > 4 {
150+
return true
151+
}
152+
return false
153+
}
137154
}

0 commit comments

Comments
 (0)