diff --git a/pass/Base.lproj/Main.storyboard b/pass/Base.lproj/Main.storyboard index b246c5a..05ccc23 100644 --- a/pass/Base.lproj/Main.storyboard +++ b/pass/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -331,10 +331,10 @@ - + - + @@ -357,10 +357,10 @@ - + - + @@ -383,10 +383,10 @@ - + - + - + - + + + + + + + + + + + + @@ -960,6 +977,7 @@ + @@ -1079,7 +1097,7 @@ - + @@ -1137,7 +1155,7 @@ - + @@ -1193,7 +1211,7 @@ pfZ36xQbOAQYKKf6ZTT5R/Y= - + diff --git a/pass/Controllers/AdvancedSettingsTableViewController.swift b/pass/Controllers/AdvancedSettingsTableViewController.swift index 54e041b..5523d88 100644 --- a/pass/Controllers/AdvancedSettingsTableViewController.swift +++ b/pass/Controllers/AdvancedSettingsTableViewController.swift @@ -12,6 +12,8 @@ import SVProgressHUD class AdvancedSettingsTableViewController: UITableViewController { @IBOutlet weak var eraseDataTableViewCell: UITableViewCell! + @IBOutlet weak var discardChangesTableViewCell: UITableViewCell! + override func viewDidLoad() { super.viewDidLoad() } @@ -31,6 +33,31 @@ class AdvancedSettingsTableViewController: UITableViewController { alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:nil)) self.present(alert, animated: true, completion: nil) tableView.deselectRow(at: indexPath, animated: true) + } else if tableView.cellForRow(at: indexPath) == discardChangesTableViewCell { + let alert = UIAlertController(title: "Discard All Changes?", message: "Do you want to permanently discard all changes to the local copy of your password data? You cannot undo this action.", preferredStyle: UIAlertControllerStyle.alert) + alert.addAction(UIAlertAction(title: "Discard All Changesa", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in + DispatchQueue.global(qos: .userInitiated).async { + SVProgressHUD.show(withStatus: "Resetting ...") + DispatchQueue.main.async { + do { + try PasswordStore.shared.reset() + NotificationCenter.default.post(Notification(name: Notification.Name("passwordStoreChangeDiscarded"))) + self.navigationController!.popViewController(animated: true) + SVProgressHUD.showSuccess(withStatus: "Done") + SVProgressHUD.dismiss(withDelay: 1) + } catch { + DispatchQueue.main.async { + SVProgressHUD.showError(withStatus: error.localizedDescription) + SVProgressHUD.dismiss(withDelay: 1) + } + } + } + } + + })) + alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:nil)) + self.present(alert, animated: true, completion: nil) + tableView.deselectRow(at: indexPath, animated: true) } } diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index 0cd7c55..e0a17b9 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -604,4 +604,34 @@ class PasswordStore { Defaults.removeAll() storeRepository = nil } + + func reset() throws { + // get the remote origin/master branch + guard let remoteBranches = try storeRepository?.remoteBranches(), + let index = remoteBranches.index(where: { $0.shortName == "master" }) + else { + throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Cannot find remote branch origin/master."]) + } + let remoteMasterBranch = remoteBranches[index] + //print("remoteMasterBranch \(remoteMasterBranch)") + + // get a list of local commits + if let localCommits = try storeRepository?.localCommitsRelative(toRemoteBranch: remoteMasterBranch), + localCommits.count > 0 { + //print("PasswordStore.reset: \(localCommits.count)") + // get the first local commit + guard let firstLocalCommit = localCommits.first, + firstLocalCommit.parents.count == 1, + let newHead = firstLocalCommit.parents.first else { + throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Cannot decide how to reset."]) + } + try self.storeRepository?.reset(to: newHead, resetType: GTRepositoryResetType.hard) + self.updatePasswordEntityCoreData() + NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) + self.setAllSynced() + } else { + //print("PasswordStore.reset: no new commit") + return; // no new commit + } + } }