-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add functionality for cloning from git in welcome view #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c90eae4
Git Clone: Add basic functionality of cloning repository
Pythonen 97c1fee
Git Clone: Select target folder and clone the repository
Pythonen 713abee
Git Clone: Open cloned folder
Pythonen d8735bc
Git Clone: Add error handling
Pythonen 4a201b7
Git Clone: Implementation as a module
Pythonen 3aed56c
Git Clone: Uncomment dependency from Package.swift
Pythonen 715d465
Merge branch 'main' into git-clone
Pythonen e6a1c17
Git Clone: Switch to use NSSavePanel
Pythonen 5e0b088
Git Clone: Remove .git only if the url has it.
Pythonen 14f4a78
Merge branch 'main' into git-clone
Pythonen fe462ea
Git Clone: Reposition UI
Pythonen e94ceec
Git Clone: Change clone view to be sheet
Pythonen 015eafe
Merge branch 'main' into git-clone
Pythonen 12387b5
Git Clone: Validate url and more error handling
Pythonen 5cfdd7a
Merge branch 'main' into git-clone
Pythonen 8fea833
Git Clone: Add automatic pasting
Pythonen 65eef65
Merge branch 'main' into git-clone
Pythonen 07661da
GitClone: Fix some force unwrappings and make vars private
Pythonen 10653c1
Merge branch 'main' into git-clone
Pythonen 7db99f1
GitClone: Make shellClient constant
Pythonen 5774bbb
GitClone: Fix indentation and make logic private func
Pythonen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
CodeEditModules/Modules/GitClone/src/GitCloneView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // | ||
| // GitCloneView.swift | ||
| // CodeEdit | ||
| // | ||
| // Created by Aleksi Puttonen on 23.3.2022. | ||
| // | ||
|
|
||
| import SwiftUI | ||
| import GitClient | ||
| import Foundation | ||
| import ShellClient | ||
|
|
||
| public struct GitCloneView: View { | ||
| private let shellClient: ShellClient | ||
| @Binding private var isPresented: Bool | ||
| @State private var repoUrlStr = "" | ||
| @State private var repoPath = "~/" | ||
| public init(shellClient: ShellClient, isPresented: Binding<Bool>) { | ||
| self.shellClient = shellClient | ||
| self._isPresented = isPresented | ||
| } | ||
| public var body: some View { | ||
| VStack(spacing: 8) { | ||
| HStack { | ||
| Image(nsImage: NSApp.applicationIconImage) | ||
| .resizable() | ||
| .frame(width: 64, height: 64) | ||
| .padding(.bottom, 50) | ||
| VStack(alignment: .leading) { | ||
| Text("Clone an existing repository") | ||
| .bold() | ||
| .padding(.bottom, 2) | ||
| Text("Enter a git repository URL:") | ||
| .font(.system(size: 11)) | ||
| .foregroundColor(.secondary) | ||
| .alignmentGuide(.trailing) { context in | ||
| context[.trailing] | ||
| } | ||
| TextField("Git Repository URL", text: $repoUrlStr) | ||
| .lineLimit(1) | ||
| .padding(.bottom, 15) | ||
| .frame(width: 300) | ||
| HStack { | ||
| Button("Cancel") { | ||
| isPresented = false | ||
| } | ||
| Button("Clone") { | ||
| cloneRepository() | ||
| } | ||
| .keyboardShortcut(.defaultAction) | ||
| .disabled(!isValid(url: repoUrlStr)) | ||
| } | ||
| .offset(x: 185) | ||
| .alignmentGuide(.leading) { context in | ||
| context[.leading] | ||
| } | ||
| } | ||
| } | ||
| .padding(.top, 20) | ||
| .padding(.horizontal, 20) | ||
| .padding(.bottom, 16) | ||
| .onAppear { | ||
| self.checkClipboard(textFieldText: &repoUrlStr) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension GitCloneView { | ||
| func getPath(modifiable: inout String, saveName: String) -> String? { | ||
| let dialog = NSSavePanel() | ||
| dialog.showsResizeIndicator = true | ||
| dialog.showsHiddenFiles = false | ||
| dialog.showsTagField = false | ||
| dialog.prompt = "Clone" | ||
| dialog.nameFieldStringValue = saveName | ||
| dialog.nameFieldLabel = "Clone as" | ||
| dialog.title = "Clone" | ||
|
|
||
| if dialog.runModal() == NSApplication.ModalResponse.OK { | ||
| let result = dialog.url | ||
|
|
||
| if result != nil { | ||
| let path: String = result!.path | ||
| // path contains the directory path e.g | ||
| // /Users/ourcodeworld/Desktop/folder | ||
| modifiable = path | ||
| return path | ||
| } | ||
| } else { | ||
| // User clicked on "Cancel" | ||
| return nil | ||
| } | ||
| return nil | ||
| } | ||
| func showAlert(alertMsg: String, infoText: String) { | ||
| let alert = NSAlert() | ||
| alert.messageText = alertMsg | ||
| alert.informativeText = infoText | ||
| alert.addButton(withTitle: "OK") | ||
| alert.alertStyle = .warning | ||
| alert.runModal() | ||
| } | ||
| func isValid(url: String) -> Bool { | ||
| // Doing the same kind of check that Xcode does when cloning | ||
| let url = url.lowercased() | ||
| if url.starts(with: "http://") && url.count > 7 { | ||
| return true | ||
| } else if url.starts(with: "https://") && url.count > 8 { | ||
| return true | ||
| } else if url.starts(with: "git@") && url.count > 4 { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| func checkClipboard(textFieldText: inout String) { | ||
| if let url = NSPasteboard.general.pasteboardItems?.first?.string(forType: .string) { | ||
| if isValid(url: url) { | ||
| textFieldText = url | ||
| } | ||
| } | ||
| } | ||
| private func cloneRepository() { | ||
| do { | ||
| if repoUrlStr == "" { | ||
| showAlert(alertMsg: "Url cannot be empty", | ||
| infoText: "You must specify a repository to clone") | ||
| return | ||
| } | ||
| // Parsing repo name | ||
| let repoURL = URL(string: repoUrlStr) | ||
| if var repoName = repoURL?.lastPathComponent { | ||
| // Strip .git from name if it has it. | ||
| // Cloning repository without .git also works | ||
| if repoName.contains(".git") { | ||
| repoName.removeLast(4) | ||
| } | ||
| guard getPath(modifiable: &repoPath, saveName: repoName) != nil else { | ||
| return | ||
| } | ||
| } else { | ||
| return | ||
| } | ||
| guard let dirUrl = URL(string: repoPath) else { | ||
| return | ||
| } | ||
| var isDir: ObjCBool = true | ||
| if FileManager.default.fileExists(atPath: repoPath, isDirectory: &isDir) { | ||
| showAlert(alertMsg: "Error", infoText: "Directory already exists") | ||
| return | ||
| } | ||
| try FileManager.default.createDirectory(atPath: repoPath, | ||
| withIntermediateDirectories: true, | ||
| attributes: nil) | ||
| try GitClient.default(directoryURL: dirUrl, | ||
| shellClient: shellClient).cloneRepository(repoUrlStr) | ||
| // TODO: Maybe add possibility to checkout to certain branch straight after cloning | ||
| isPresented = false | ||
| } catch { | ||
| guard let error = error as? GitClient.GitClientError else { | ||
| return showAlert(alertMsg: "Error", infoText: error.localizedDescription) | ||
| } | ||
| switch error { | ||
| case let .outputError(message): | ||
| showAlert(alertMsg: "Error", infoText: message) | ||
| case .notGitRepository: | ||
| showAlert(alertMsg: "Error", infoText: "Not git repository") | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.