make cloneRepository robust

This commit is contained in:
Bob Sun 2017-02-04 14:59:55 +08:00
parent 529e170704
commit 577d21bf13
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4

View file

@ -42,6 +42,7 @@ class PasswordStore {
static let shared = PasswordStore() static let shared = PasswordStore()
let storeURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store") let storeURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store")
let tempStoreURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/password-store-temp")
var storeRepository: GTRepository? var storeRepository: GTRepository?
var gitCredential: GitCredential? var gitCredential: GitCredential?
@ -91,23 +92,24 @@ class PasswordStore {
credential: GitCredential, credential: GitCredential,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws { checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
print("start cloning remote repo: \(remoteRepoURL)")
let fm = FileManager.default
if (storeRepository != nil) {
print("remove item")
do {
try fm.removeItem(at: storeURL)
} catch let error as NSError {
print(error.debugDescription)
}
}
print("start cloning...") print("start cloning...")
let credentialProvider = try credential.credentialProvider() let credentialProvider = try credential.credentialProvider()
let options: [String: Any] = [ let options: [String: Any] = [
GTRepositoryCloneOptionsCredentialProvider: credentialProvider, GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
] ]
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock) storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock)
print("clone finish") print("clone finish")
let fm = FileManager.default
do {
if fm.fileExists(atPath: storeURL.path) {
try fm.removeItem(at: storeURL)
}
try fm.copyItem(at: tempStoreURL, to: storeURL)
try fm.removeItem(at: tempStoreURL)
} catch {
print(error)
}
storeRepository = try GTRepository(url: storeURL)
updatePasswordEntityCoreData() updatePasswordEntityCoreData()
gitCredential = credential gitCredential = credential
} }