Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pass/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit
import CoreData
import SVProgressHUD
import passKit
import SwiftyUserDefaults

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -33,6 +34,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.perform(#selector(postSearchNotification), with: nil, afterDelay: 0.4)
}
}

// Assign default values to global settings.
SharedDefaults.register(defaults: [DefaultsKeys.gitBranchName._key: "master"])

return true
}

Expand Down
36 changes: 33 additions & 3 deletions pass/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,39 @@
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Branch Name" id="Uoy-58-5ug" userLabel="Branch Name">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" id="5XL-Uj-JWL" userLabel="branchNameTableViewCell">
<rect key="frame" x="0.0" y="163.33333333333334" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="5XL-Uj-JWL" id="BfY-mD-gfv">
<rect key="frame" x="0.0" y="0.0" width="414" height="43.666666666666664"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Branch Name" textAlignment="natural" adjustsFontForContentSizeCategory="YES" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="VVI-gJ-e37" userLabel="Text">
<rect key="frame" x="20" y="6.6666666666666661" width="374" height="30.333333333333336"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="alphabet" returnKeyType="done"/>
<connections>
<outlet property="delegate" destination="ynQ-64-MfA" id="y89-Ub-ecB"/>
</connections>
</textField>
</subviews>
<constraints>
<constraint firstItem="VVI-gJ-e37" firstAttribute="trailing" secondItem="BfY-mD-gfv" secondAttribute="trailingMargin" id="IGP-5Y-nMm"/>
<constraint firstItem="VVI-gJ-e37" firstAttribute="centerY" secondItem="BfY-mD-gfv" secondAttribute="centerY" id="IOt-p9-Bwe"/>
<constraint firstItem="VVI-gJ-e37" firstAttribute="leading" secondItem="BfY-mD-gfv" secondAttribute="leadingMargin" id="alH-v2-xcf"/>
<constraint firstAttribute="bottomMargin" secondItem="VVI-gJ-e37" secondAttribute="bottom" constant="-4" id="pAT-la-hAA"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="Username" id="fRu-A2-SCk">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="usernameTableVIewCell" rowHeight="52" id="tnj-5U-kMB">
<rect key="frame" x="0.0" y="163.33333333333334" width="414" height="52"/>
<rect key="frame" x="0.0" y="263.33333333333337" width="414" height="52"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" tableViewCell="tnj-5U-kMB" id="f0c-pI-MSJ">
<rect key="frame" x="0.0" y="0.0" width="414" height="51.666666666666664"/>
Expand Down Expand Up @@ -384,7 +413,7 @@
<tableViewSection headerTitle="Authentication Method" id="h0N-tI-shZ">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="2" indentationWidth="0.0" shouldIndentWhileEditing="NO" id="KrP-nb-haa">
<rect key="frame" x="0.0" y="271.33333333333337" width="414" height="44"/>
<rect key="frame" x="0.0" y="371.33333333333337" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KrP-nb-haa" id="1uB-oE-kfI">
<rect key="frame" x="0.0" y="0.0" width="414" height="43.666666666666664"/>
Expand Down Expand Up @@ -416,7 +445,7 @@
<inset key="separatorInset" minX="62" minY="0.0" maxX="0.0" maxY="0.0"/>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" accessoryType="detailButton" hidesAccessoryWhenEditing="NO" indentationLevel="2" indentationWidth="0.0" shouldIndentWhileEditing="NO" id="Qmt-bo-CuJ">
<rect key="frame" x="0.0" y="315.33333333333337" width="414" height="44"/>
<rect key="frame" x="0.0" y="415.33333333333337" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Qmt-bo-CuJ" id="p3u-8b-h3U">
<rect key="frame" x="0.0" y="0.0" width="362" height="43.666666666666664"/>
Expand Down Expand Up @@ -466,6 +495,7 @@
<connections>
<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="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"/>
Expand Down
5 changes: 5 additions & 0 deletions pass/Controllers/GitServerSettingTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GitServerSettingTableViewController: UITableViewController {

@IBOutlet weak var gitURLTextField: UITextField!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var branchNameTextField: UITextField!
@IBOutlet weak var authSSHKeyCell: UITableViewCell!
@IBOutlet weak var authPasswordCell: UITableViewCell!
let passwordStore = PasswordStore.shared
Expand Down Expand Up @@ -51,6 +52,7 @@ class GitServerSettingTableViewController: UITableViewController {
gitURLTextField.text = url.absoluteString
}
usernameTextField.text = SharedDefaults[.gitUsername]
branchNameTextField.text = SharedDefaults[.gitBranchName]
sshLabel = authSSHKeyCell.subviews[0].subviews[0] as? UILabel
checkAuthenticationMethod(method: authenticationMethod)
authSSHKeyCell.accessoryType = .detailButton
Expand All @@ -77,6 +79,7 @@ class GitServerSettingTableViewController: UITableViewController {
// try to clone
let gitRepostiroyURL = gitURLTextField.text!.trimmed
let username = usernameTextField.text!
let branchName = branchNameTextField.text!
let auth = authenticationMethod

SVProgressHUD.setDefaultMaskType(.black)
Expand All @@ -100,6 +103,7 @@ class GitServerSettingTableViewController: UITableViewController {
do {
try self.passwordStore.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
credential: gitCredential,
branchName: branchName,
requestGitPassword: self.requestGitPassword,
transferProgressBlock: { (git_transfer_progress, stop) in
DispatchQueue.main.async {
Expand All @@ -114,6 +118,7 @@ class GitServerSettingTableViewController: UITableViewController {
DispatchQueue.main.async {
SharedDefaults[.gitURL] = URL(string: gitRepostiroyURL)
SharedDefaults[.gitUsername] = username
SharedDefaults[.gitBranchName] = branchName
SharedDefaults[.gitAuthenticationMethod] = auth
SVProgressHUD.dismiss()
let savePassphraseAlert = UIAlertController(title: "Done", message: "Do you want to save the Git credential password/passphrase?", preferredStyle: UIAlertControllerStyle.alert)
Expand Down
9 changes: 6 additions & 3 deletions passKit/Helpers/AppError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Foundation

public enum AppError: Error {
case RepositoryNotSetError
case RepositoryRemoteMasterNotFoundError
case RepositoryRemoteBranchNotFoundError(_: String)
case RepositoryBranchNotFound(_: String)
case KeyImportError
case PasswordDuplicatedError
case GitResetError
Expand All @@ -25,8 +26,10 @@ extension AppError: LocalizedError {
switch self {
case .RepositoryNotSetError:
return "Git repository is not set."
case .RepositoryRemoteMasterNotFoundError:
return "Cannot find remote branch origin/master."
case let .RepositoryRemoteBranchNotFoundError(remoteBranchName):
return "Cannot find remote branch 'origin/\(remoteBranchName)'."
case let .RepositoryBranchNotFound(branchName):
return "Branch with name '\(branchName)' not found in repository."
case .KeyImportError:
return "Cannot import the key."
case .PasswordDuplicatedError:
Expand Down
1 change: 1 addition & 0 deletions passKit/Helpers/DefaultsKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public extension DefaultsKeys {
static let gitURL = DefaultsKey<URL?>("gitURL")
static let gitAuthenticationMethod = DefaultsKey<String?>("gitAuthenticationMethod")
static let gitUsername = DefaultsKey<String?>("gitUsername")
static let gitBranchName = DefaultsKey<String?>("gitBranchName")
static let gitSSHPrivateKeyURL = DefaultsKey<URL?>("gitSSHPrivateKeyURL")
static let gitSSHKeySource = DefaultsKey<String?>("gitSSHKeySource")
static let gitSSHPrivateKeyArmor = DefaultsKey<String?>("gitSSHPrivateKeyArmor")
Expand Down
42 changes: 32 additions & 10 deletions passKit/Models/PasswordStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public class PasswordStore {

public func cloneRepository(remoteRepoURL: URL,
credential: GitCredential,
branchName: String,
requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?,
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
Expand All @@ -308,6 +309,7 @@ public class PasswordStore {
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
try fm.moveItem(at: tempStoreURL, to: storeURL)
storeRepository = try GTRepository(url: storeURL)
try checkoutAndChangeBranch(withName: branchName)
} catch {
credential.delete()
DispatchQueue.main.async {
Expand All @@ -324,6 +326,27 @@ public class PasswordStore {
}
}

private func checkoutAndChangeBranch(withName localBranchName: String) throws {
if (localBranchName == "master") {
return
}
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
}
let remoteBranchName = "origin/\(localBranchName)"
guard let remoteBranch = try? storeRepository.lookUpBranch(withName: remoteBranchName, type: .remote, success: nil) else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
}
guard let remoteBranchOid = remoteBranch.oid else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
}
let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil)
try localBranch.updateTrackingBranch(remoteBranch)
let checkoutOptions = GTCheckoutOptions.init(strategy: .force)
try storeRepository.checkoutReference(localBranch.reference, options: checkoutOptions)
try storeRepository.moveHEAD(to: localBranch.reference)
}

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
Expand Down Expand Up @@ -563,9 +586,9 @@ public class PasswordStore {
do {
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
if let masterBranch = try getLocalBranch(withName: "master") {
if let branch = try getLocalBranch(withName: SharedDefaults[.gitBranchName]!) {
let remote = try GTRemote(name: "origin", in: storeRepository)
try storeRepository.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock)
try storeRepository.push(branch, to: remote, withOptions: options, progress: transferProgressBlock)
}
} catch {
throw(error)
Expand Down Expand Up @@ -786,19 +809,18 @@ public class PasswordStore {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
}
// get the remote origin/master branch
guard let index = try storeRepository.remoteBranches().index(where: { $0.shortName == "master" }) else {
throw AppError.RepositoryRemoteMasterNotFoundError
// get the remote branch
let remoteBranchName = SharedDefaults[.gitBranchName]!
guard let remoteBranch = try storeRepository.remoteBranches().first(where: { $0.shortName == remoteBranchName }) else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
}
let remoteMasterBranch = try storeRepository.remoteBranches()[index]

// check oid before calling localCommitsRelative
guard remoteMasterBranch.oid != nil else {
throw AppError.RepositoryRemoteMasterNotFoundError
guard remoteBranch.oid != nil else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
}

// get a list of local commits
return try storeRepository.localCommitsRelative(toRemoteBranch: remoteMasterBranch)
return try storeRepository.localCommitsRelative(toRemoteBranch: remoteBranch)
}


Expand Down