Fix some "linking against dylib" warning

- Move requestGitPassword to view controllers
- Move Utils.alert out of passKit, because SVProgressHUD is there
This commit is contained in:
Yishi Lin 2017-06-13 13:19:18 +08:00
parent 86a6fb6395
commit 7f6900b7cd
7 changed files with 112 additions and 65 deletions

View file

@ -10,31 +10,30 @@ import Foundation
import UIKit
import SwiftyUserDefaults
import ObjectiveGit
import SVProgressHUD
public struct GitCredential {
public var credential: Credential
private var credential: Credential
public enum Credential {
case http(userName: String, controller: UIViewController)
case ssh(userName: String, privateKeyFile: URL, controller: UIViewController)
case http(userName: String)
case ssh(userName: String, privateKeyFile: URL)
}
public init(credential: Credential) {
self.credential = credential
}
public func credentialProvider() throws -> GTCredentialProvider {
public func credentialProvider(requestGitPassword: @escaping (Credential, String?) -> String?) throws -> GTCredentialProvider {
var attempts = 0
var lastPassword: String? = nil
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
var credential: GTCredential? = nil
switch self.credential {
case let .http(userName, controller):
case let .http(userName):
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
if newPassword == nil || attempts != 0 {
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
newPassword = requestedPassword
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
} else {
@ -44,10 +43,10 @@ public struct GitCredential {
attempts += 1
lastPassword = newPassword
credential = try? GTCredential(userName: userName, password: newPassword!)
case let .ssh(userName, privateKeyFile, controller):
case let .ssh(userName, privateKeyFile):
var newPassword = Utils.getPasswordFromKeychain(name: "gitSSHKeyPassphrase")
if newPassword == nil || attempts != 0 {
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
newPassword = requestedPassword
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
} else {
@ -71,38 +70,5 @@ public struct GitCredential {
Utils.removeKeychain(name: "gitSSHKeyPassphrase")
}
}
private func requestGitPassword(_ controller: UIViewController, _ lastPassword: String?) -> String? {
let sem = DispatchSemaphore(value: 0)
var password: String?
var message = ""
switch credential {
case .http:
message = "Please fill in the password of your Git account."
case .ssh:
message = "Please fill in the password of your SSH key."
}
DispatchQueue.main.async {
SVProgressHUD.dismiss()
let alert = UIAlertController(title: "Password", message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = lastPassword ?? ""
textField.isSecureTextEntry = true
})
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
password = alert.textFields!.first!.text
sem.signal()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
password = nil
sem.signal()
})
controller.present(alert, animated: true, completion: nil)
}
let _ = sem.wait(timeout: .distantFuture)
return password
}
}

View file

@ -11,7 +11,6 @@ import CoreData
import UIKit
import SwiftyUserDefaults
import ObjectiveGit
import SVProgressHUD
import ObjectivePGP
public class PasswordStore {
@ -262,12 +261,13 @@ public class PasswordStore {
public func cloneRepository(remoteRepoURL: URL,
credential: GitCredential,
requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
Utils.removeFileIfExists(at: storeURL)
Utils.removeFileIfExists(at: tempStoreURL)
do {
let credentialProvider = try credential.credentialProvider()
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
if fm.fileExists(atPath: storeURL.path) {
@ -287,12 +287,12 @@ public class PasswordStore {
}
}
public func pullRepository(credential: GitCredential, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
public func pullRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
}
do {
let credentialProvider = try credential.credentialProvider()
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
let remote = try GTRemote(name: "origin", in: storeRepository)
try storeRepository.pull(storeRepository.currentBranch(), from: remote, withOptions: options, progress: transferProgressBlock)
@ -538,12 +538,12 @@ public class PasswordStore {
return branches.first
}
public func pushRepository(credential: GitCredential, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
public func pushRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
}
do {
let credentialProvider = try credential.credentialProvider()
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
if let masterBranch = try getLocalBranch(withName: "master") {
let remote = try GTRemote(name: "origin", in: storeRepository)