Refactor Git URL related code, fix #336

This commit is contained in:
Mingshen Sun 2019-11-30 15:11:28 -08:00
parent e83a2f941e
commit 258906fdbb
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
8 changed files with 119 additions and 105 deletions

View file

@ -319,15 +319,15 @@
<sections>
<tableViewSection headerTitle="Git Repository URL" id="pbe-W6-w4V">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="gitRepositoryURLTabelViewCell" id="FRr-pf-aPO">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" accessoryType="detailButton" indentationWidth="10" reuseIdentifier="gitRepositoryURLTabelViewCell" id="FRr-pf-aPO">
<rect key="frame" x="0.0" y="55.333332061767578" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" tableViewCell="FRr-pf-aPO" id="60A-PS-qGe">
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
<rect key="frame" x="0.0" y="0.0" width="370" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Git Repository URL" textAlignment="natural" adjustsFontForContentSizeCategory="YES" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="EVT-VU-sCi">
<rect key="frame" x="20" y="11" width="374" height="22"/>
<rect key="frame" x="20" y="11" width="342" height="22"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="URL" returnKeyType="next"/>
<connections>
@ -488,6 +488,8 @@
<outlet property="authPasswordCell" destination="KrP-nb-haa" id="zjH-sg-dfJ"/>
<outlet property="authSSHKeyCell" destination="Qmt-bo-CuJ" id="11L-Tt-MgM"/>
<outlet property="branchNameTextField" destination="VVI-gJ-e37" id="XLA-8I-yPm"/>
<outlet property="gitRepositoryURLTabelViewCell" destination="FRr-pf-aPO" id="aSz-4u-NZl"/>
<outlet property="gitURLCell" destination="FRr-pf-aPO" id="O1e-kc-5tT"/>
<outlet property="gitURLTextField" destination="EVT-VU-sCi" id="XdU-3l-Nsv"/>
<outlet property="usernameTextField" destination="TMg-Gk-7nG" id="htL-4C-WJF"/>
<segue destination="7K9-cE-9qq" kind="unwind" identifier="saveGitServerSettingSegue" unwindAction="saveGitServerSettingWithSegue:" id="5UN-sC-xCA"/>

View file

@ -51,7 +51,7 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
DispatchQueue.global(qos: .userInitiated).async {
let passwords = self.numberOfPasswordsString()
let size = self.sizeOfRepositoryString()
let localCommits = self.numberOfLocalCommitsString()
let localCommits = self.passwordStore.numberOfLocalCommits
let lastSynced = self.lastSyncedTimeString()
let commits = self.numberOfCommitsString()
@ -86,13 +86,6 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController {
return ByteCountFormatter.string(fromByteCount: Int64(self.passwordStore.sizeOfRepositoryByteCount), countStyle: ByteCountFormatter.CountStyle.file)
}
private func numberOfLocalCommitsString() -> String {
if let numberOfLocalCommits = passwordStore.numberOfLocalCommits {
return String(numberOfLocalCommits)
}
return AboutRepositoryTableViewController.VALUE_NOT_AVAILABLE
}
private func lastSyncedTimeString() -> String {
guard let date = self.passwordStore.lastSyncedTime else {
return "SyncAgain?".localize()

View file

@ -10,6 +10,7 @@ import UIKit
import SVProgressHUD
import passKit
class GitServerSettingTableViewController: UITableViewController {
@IBOutlet weak var gitURLTextField: UITextField!
@ -17,25 +18,50 @@ class GitServerSettingTableViewController: UITableViewController {
@IBOutlet weak var branchNameTextField: UITextField!
@IBOutlet weak var authSSHKeyCell: UITableViewCell!
@IBOutlet weak var authPasswordCell: UITableViewCell!
let passwordStore = PasswordStore.shared
var sshLabel: UILabel? = nil
@IBOutlet weak var gitURLCell: UITableViewCell!
@IBOutlet weak var gitRepositoryURLTabelViewCell: UITableViewCell!
private let passwordStore = PasswordStore.shared
private var sshLabel: UILabel? = nil
var authenticationMethod = SharedDefaults[.gitAuthenticationMethod] ?? "Password"
private var gitAuthenticationMethod: GitAuthenticationMethod {
get { SharedDefaults[.gitAuthenticationMethod] }
set { SharedDefaults[.gitAuthenticationMethod] = newValue }
}
private var gitUrl: URL {
get { SharedDefaults[.gitURL] }
set { SharedDefaults[.gitURL] = newValue }
}
private var gitBranchName: String {
get { SharedDefaults[.gitBranchName] }
set { SharedDefaults[.gitBranchName] = newValue }
}
private var gitUsername: String {
get { SharedDefaults[.gitUsername] }
set { SharedDefaults[.gitUsername] = newValue }
}
private var gitCredential: GitCredential {
get {
switch SharedDefaults[.gitAuthenticationMethod] {
case .password:
return GitCredential(credential: .http(userName: SharedDefaults[.gitUsername]))
case .key:
let privateKey: String = AppKeychain.shared.get(for: SshKey.PRIVATE.getKeychainKey()) ?? ""
return GitCredential(credential: .ssh(userName: SharedDefaults[.gitUsername], privateKey: privateKey))
}
}
}
private func checkAuthenticationMethod(method: String) {
private func checkAuthenticationMethod() {
let passwordCheckView = authPasswordCell.viewWithTag(1001)!
let sshKeyCheckView = authSSHKeyCell.viewWithTag(1001)!
switch method {
case "Password":
switch self.gitAuthenticationMethod {
case .password:
passwordCheckView.isHidden = false
sshKeyCheckView.isHidden = true
case "SSH Key":
case .key:
passwordCheckView.isHidden = true
sshKeyCheckView.isHidden = false
default:
passwordCheckView.isHidden = false
sshKeyCheckView.isHidden = true
}
}
@ -48,13 +74,11 @@ class GitServerSettingTableViewController: UITableViewController {
}
override func viewDidLoad() {
super.viewDidLoad()
if let url = SharedDefaults[.gitURL] {
gitURLTextField.text = url.absoluteString
}
usernameTextField.text = SharedDefaults[.gitUsername]
branchNameTextField.text = SharedDefaults[.gitBranchName]
gitURLTextField.text = self.gitUrl.absoluteString
usernameTextField.text = self.gitUsername
branchNameTextField.text = self.gitBranchName
sshLabel = authSSHKeyCell.subviews[0].subviews[0] as? UILabel
checkAuthenticationMethod(method: authenticationMethod)
checkAuthenticationMethod()
authSSHKeyCell.accessoryType = .detailButton
}
@ -62,9 +86,17 @@ class GitServerSettingTableViewController: UITableViewController {
let cell = tableView.cellForRow(at: indexPath)
if cell == authSSHKeyCell {
showSSHKeyActionSheet()
} else if cell == gitURLCell {
showGitURLFormatHelp()
}
}
private func showGitURLFormatHelp() {
let alert = UIAlertController(title: "Git URL Format", message: "https://example.com[:port]/project.git\nssh://[user@]server[:port]/project.git\n[user@]server:project.git (no scheme)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in }))
self.present(alert, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
@ -76,36 +108,15 @@ class GitServerSettingTableViewController: UITableViewController {
}
private func cloneAndSegueIfSuccess() {
// try to clone
let gitRepostiroyURL = gitURLTextField.text!.trimmed
let username = usernameTextField.text!.trimmed
let branchName = branchNameTextField.text!.trimmed
let auth = authenticationMethod
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "PrepareRepository".localize())
var gitCredential: GitCredential
let privateKey: String? = AppKeychain.shared.get(for: SshKey.PRIVATE.getKeychainKey())
if auth == "Password" || privateKey == nil {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username))
} else {
gitCredential = GitCredential(
credential: GitCredential.Credential.ssh(
userName: username,
privateKey: privateKey!
)
)
}
// Remember git credential password/passphrase temporarily, ask whether users want this after a successful clone.
SharedDefaults[.isRememberGitCredentialPassphraseOn] = true
let group = DispatchGroup()
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async(group: group) {
do {
try self.passwordStore.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
credential: gitCredential,
branchName: branchName,
try self.passwordStore.cloneRepository(remoteRepoURL: self.gitUrl,
credential: self.gitCredential,
branchName: self.gitBranchName,
requestGitPassword: self.requestGitPassword,
transferProgressBlock: { (git_transfer_progress, stop) in
DispatchQueue.main.async {
@ -113,7 +124,7 @@ class GitServerSettingTableViewController: UITableViewController {
}
},
checkoutProgressBlock: { (path, completedSteps, totalSteps) in
DispatchQueue.main.async { SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "CheckingOutBranch".localize(branchName))
DispatchQueue.main.async { SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "CheckingOutBranch".localize(self.gitBranchName))
}
})
} catch {
@ -130,10 +141,6 @@ class GitServerSettingTableViewController: UITableViewController {
group.notify(queue: dispatchQueue) {
NSLog("clone done")
DispatchQueue.main.async {
SharedDefaults[.gitURL] = URL(string: gitRepostiroyURL)
SharedDefaults[.gitUsername] = username
SharedDefaults[.gitBranchName] = branchName
SharedDefaults[.gitAuthenticationMethod] = auth
SVProgressHUD.dismiss {
let savePassphraseAlert = UIAlertController(title: "Done".localize(), message: "WantToSaveGitCredential?".localize(), preferredStyle: UIAlertController.Style.alert)
// no
@ -157,24 +164,23 @@ class GitServerSettingTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if cell == authPasswordCell {
authenticationMethod = "Password"
self.gitAuthenticationMethod = .password
} else if cell == authSSHKeyCell {
if !AppKeychain.shared.contains(key: SshKey.PRIVATE.getKeychainKey()) {
Utils.alert(title: "CannotSelectSshKey".localize(), message: "PleaseSetupSshKeyFirst.".localize(), controller: self, completion: nil)
authenticationMethod = "Password"
gitAuthenticationMethod = .password
} else {
authenticationMethod = "SSH Key"
gitAuthenticationMethod = .key
}
}
checkAuthenticationMethod(method: authenticationMethod)
checkAuthenticationMethod()
tableView.deselectRow(at: indexPath, animated: true)
}
@IBAction func save(_ sender: Any) {
// some sanity checks
guard let gitURL = URL(string: gitURLTextField.text!) else {
guard let gitURLTextFieldText = gitURLTextField.text, let gitURL = URL(string: gitURLTextFieldText) else {
Utils.alert(title: "CannotSave".localize(), message: "SetGitRepositoryUrl".localize(), controller: self, completion: nil)
return
}
@ -184,26 +190,30 @@ class GitServerSettingTableViewController: UITableViewController {
return
}
switch gitURL.scheme {
case let val where val == "https":
break
case let val where val == "ssh":
guard let sshUsername = gitURL.user, sshUsername.isEmpty == false else {
func checkUsername() {
if gitURL.user == nil && usernameTextField.text == nil {
Utils.alert(title: "CannotSave".localize(), message: "CannotFindUsername.".localize(), controller: self, completion: nil)
return
}
guard let username = usernameTextField.text, username == sshUsername else {
if let urlUsername = gitURL.user, let textFieldUsername = usernameTextField.text, urlUsername != textFieldUsername.trimmed {
Utils.alert(title: "CannotSave".localize(), message: "CheckEnteredUsername.".localize(), controller: self, completion: nil)
return
}
case let val where val == "http":
Utils.alert(title: "CannotSave".localize(), message: "UseHttps.".localize(), controller: self, completion: nil)
return
default:
Utils.alert(title: "CannotSave".localize(), message: "SpecifySchema.".localize(), controller: self, completion: nil)
return
}
if let scheme = gitURL.scheme {
switch scheme {
case "ssh", "http", "https", "file": checkUsername()
default:
Utils.alert(title: "CannotSave".localize(), message: "Protocol is not supported", controller: self, completion: nil)
return
}
}
self.gitUrl = gitURL
self.gitBranchName = branchName
self.gitUsername = (gitURL.user ?? usernameTextField.text ?? "git").trimmed
if passwordStore.repositoryExisted() {
let alert = UIAlertController(title: "Overwrite?".localize(), message: "OperationWillOverwriteData.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Overwrite".localize(), style: UIAlertAction.Style.destructive, handler: { _ in
@ -272,7 +282,7 @@ class GitServerSettingTableViewController: UITableViewController {
SharedDefaults[.gitSSHKeySource] = nil
if let sshLabel = self.sshLabel {
sshLabel.isEnabled = false
self.checkAuthenticationMethod(method: "Password".localize())
self.checkAuthenticationMethod()
}
}
optionMenu.addAction(deleteAction)

View file

@ -42,6 +42,18 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
case unsynced
}
private var gitCredential: GitCredential {
get {
switch SharedDefaults[.gitAuthenticationMethod] {
case .password:
return GitCredential(credential: .http(userName: SharedDefaults[.gitUsername]))
case .key:
let privateKey: String = AppKeychain.shared.get(for: SshKey.PRIVATE.getKeychainKey()) ?? ""
return GitCredential(credential: .ssh(userName: SharedDefaults[.gitUsername], privateKey: privateKey))
}
}
}
private lazy var searchController: UISearchController = {
let uiSearchController = UISearchController(searchResultsController: nil)
uiSearchController.searchResultsUpdater = self
@ -173,27 +185,17 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "SyncingPasswordStore".localize())
var gitCredential: GitCredential
let privateKey: String? = self.keychain.get(for: SshKey.PRIVATE.getKeychainKey())
if SharedDefaults[.gitAuthenticationMethod] == "Password" || privateKey == nil {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: SharedDefaults[.gitUsername]!))
} else {
gitCredential = GitCredential(
credential: GitCredential.Credential.ssh(
userName: SharedDefaults[.gitUsername]!,
privateKey: privateKey!
)
)
}
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try self.passwordStore.pullRepository(credential: gitCredential, requestGitPassword: self.requestGitPassword(credential:lastPassword:), transferProgressBlock: {(git_transfer_progress, stop) in
try self.passwordStore.pullRepository(credential: self.gitCredential, requestGitPassword: self.requestGitPassword(credential:lastPassword:), transferProgressBlock: {(git_transfer_progress, stop) in
DispatchQueue.main.async {
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "PullingFromRemoteRepository".localize())
}
})
if self.passwordStore.numberOfLocalCommits ?? 0 > 0 {
try self.passwordStore.pushRepository(credential: gitCredential, requestGitPassword: self.requestGitPassword(credential:lastPassword:), transferProgressBlock: {(current, total, bytes, stop) in
if self.passwordStore.numberOfLocalCommits > 0 {
try self.passwordStore.pushRepository(credential: self.gitCredential, requestGitPassword: self.requestGitPassword(credential:lastPassword:), transferProgressBlock: {(current, total, bytes, stop) in
DispatchQueue.main.async {
SVProgressHUD.showProgress(Float(current)/Float(total), status: "PushingToRemoteRepository".localize())
}
@ -215,7 +217,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
message = message | "UnderlyingError".localize(underlyingError.localizedDescription)
if underlyingError.localizedDescription.contains("WrongPassphrase".localize()) {
message = message | "RecoverySuggestion.".localize()
gitCredential.delete()
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(800)) {
@ -593,8 +594,8 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
private func reloadTableView(data: [PasswordsTableEntry], label: PasswordLabel = .all, anim: CAAnimation? = nil) {
// set navigation item
if let numberOfLocalCommits = passwordStore.numberOfLocalCommits, numberOfLocalCommits != 0 {
navigationController?.tabBarItem.badgeValue = "\(numberOfLocalCommits)"
if passwordStore.numberOfLocalCommits != 0 {
navigationController?.tabBarItem.badgeValue = "\(passwordStore.numberOfLocalCommits)"
} else {
navigationController?.tabBarItem.badgeValue = nil
}

View file

@ -105,7 +105,7 @@ class SettingsTableViewController: UITableViewController, UITabBarControllerDele
}
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL].host
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@ -115,7 +115,7 @@ class SettingsTableViewController: UITableViewController, UITabBarControllerDele
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: .passwordStoreErased, object: nil)
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL].host
setPGPKeyTableViewCellDetailText()
setPasswordRepositoryTableViewCellDetailText()
setPasscodeLockCell()
@ -139,11 +139,15 @@ class SettingsTableViewController: UITableViewController, UITabBarControllerDele
}
private func setPasswordRepositoryTableViewCellDetailText() {
if SharedDefaults[.gitURL] == nil {
passwordRepositoryTableViewCell.detailTextLabel?.text = "NotSet".localize()
} else {
passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]!.host
}
let host: String? = {
let gitURL = SharedDefaults[.gitURL]
if gitURL.scheme == nil {
return URL(string: "scheme://" + gitURL.absoluteString)?.host
} else {
return gitURL.host
}
}()
passwordRepositoryTableViewCell.detailTextLabel?.text = host
}
@objc func actOnPasswordStoreErasedNotification() {