From f36899a8ad16128f93077fb7e1fb7d9e78d351e5 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Wed, 22 Mar 2017 19:07:41 -0700 Subject: [PATCH] Change getNumberOfUnsynced to numberOfLocalCommits --- .../AboutRepositoryTableViewController.swift | 2 +- .../Controllers/PasswordsViewController.swift | 10 ++--- pass/Models/PasswordStore.swift | 38 ++++++++++++++----- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/pass/Controllers/AboutRepositoryTableViewController.swift b/pass/Controllers/AboutRepositoryTableViewController.swift index a136494..d410bde 100644 --- a/pass/Controllers/AboutRepositoryTableViewController.swift +++ b/pass/Controllers/AboutRepositoryTableViewController.swift @@ -74,7 +74,7 @@ class AboutRepositoryTableViewController: BasicStaticTableViewController { // section 0 [[.style: CellDataStyle.value1, .accessoryType: type, .title: "Passwords", .detailText: numberOfPasswords], [.style: CellDataStyle.value1, .accessoryType: type, .title: "Size", .detailText: sizeOfRepository], - [.style: CellDataStyle.value1, .accessoryType: type, .title: "Unsynced", .detailText: String(self?.passwordStore.getNumberOfUnsyncedPasswords() ?? 0)], + [.style: CellDataStyle.value1, .accessoryType: type, .title: "Local Commits", .detailText: String(self?.passwordStore.numberOfLocalCommits() ?? 0)], [.style: CellDataStyle.value1, .accessoryType: type, .title: "Last Synced", .detailText: Utils.getLastUpdatedTimeString()], [.style: CellDataStyle.value1, .accessoryType: type, .title: "Commits", .detailText: numberOfCommitsString], [.title: "Commit Logs", .action: "segue", .link: "showCommitLogsSegue"], diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 035f889..87b98bf 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -106,7 +106,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.show(withStatus: "Sync Password Store") - let numberOfUnsyncedPasswords = self.passwordStore.getNumberOfUnsyncedPasswords() + let numberOfLocalCommits = self.passwordStore.numberOfLocalCommits() DispatchQueue.global(qos: .userInitiated).async { [unowned self] in do { try self.passwordStore.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in @@ -114,7 +114,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository") } }) - if numberOfUnsyncedPasswords > 0 { + if numberOfLocalCommits > 0 { try self.passwordStore.pushRepository(transferProgressBlock: {(current, total, bytes, stop) in DispatchQueue.main.async { SVProgressHUD.showProgress(Float(current)/Float(total), status: "Push Remote Repository") @@ -362,11 +362,11 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV } else { title = "Password Store" } - let numberOfUnsynced = self.passwordStore.getNumberOfUnsyncedPasswords() - if numberOfUnsynced == 0 { + let numberOfLocalCommits = self.passwordStore.numberOfLocalCommits() + if numberOfLocalCommits == 0 { navigationItem.title = "\(title)" } else { - navigationItem.title = "\(title) (\(numberOfUnsynced))" + navigationItem.title = "\(title) (\(numberOfLocalCommits))" } } diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index 7163358..13d184a 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -647,17 +647,8 @@ class PasswordStore { // return the number of discarded commits func reset() throws -> Int { - // 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), + if let localCommits = try getLocalCommits(), localCommits.count > 0 { // get the oldest local commit guard let firstLocalCommit = localCommits.last, @@ -675,4 +666,31 @@ class PasswordStore { return 0 // no new commit } } + + func numberOfLocalCommits() -> Int { + do { + if let localCommits = try getLocalCommits() { + return localCommits.count + } else { + return 0 + } + } catch { + print(error) + } + return 0 + } + + private func getLocalCommits() throws -> [GTCommit]? { + // 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 + return try storeRepository?.localCommitsRelative(toRemoteBranch: remoteMasterBranch) + } }