Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Dec 23, 2022
1 parent defa284 commit 6cd0cd7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
35 changes: 31 additions & 4 deletions FSNotes/Business/Project+Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,41 @@ extension Project {
let repositoryManager = RepositoryManager()
let repoURL = UserDefaultsManagement.gitStorage.appendingPathComponent(getShortSign() + " - " + label + ".git")

// Open
do {
let repository = try repositoryManager.openRepository(at: repoURL)
return repository
} catch {
guard let originString = getGitOrigin(), let origin = URL(string: originString) else { return nil }

} catch {/*_*/}

// Clone
if let originString = getGitOrigin(), let origin = URL(string: originString) {
let cloneURL = UserDefaultsManagement.gitStorage.appendingPathComponent("tmp")
try? FileManager.default.removeItem(at: cloneURL)

let repository = try repositoryManager.cloneRepository(from: origin, at: cloneURL, authentication: getHandler())
do {
let repository = try repositoryManager.cloneRepository(from: origin, at: cloneURL, authentication: getHandler())

repository.setWorkTree(path: url.path)

let dotGit = cloneURL.appendingPathComponent(".git")

if FileManager.default.directoryExists(atUrl: dotGit) {
try? FileManager.default.moveItem(at: dotGit, to: repoURL)

return try repositoryManager.openRepository(at: repoURL)
}
} catch {/*_*/}

return nil
}

// Init
do {
let cloneURL = UserDefaultsManagement.gitStorage.appendingPathComponent("tmp")
try? FileManager.default.createDirectory(at: cloneURL, withIntermediateDirectories: true)

let signature = Signature(name: "FSNotes App", email: "[email protected]")
let repository = try repositoryManager.initRepository(at: cloneURL, signature: signature)

repository.setWorkTree(path: url.path)

Expand All @@ -87,6 +112,8 @@ extension Project {

return try repositoryManager.openRepository(at: repoURL)
}
} catch {
print("Repo init error: \(error)")
}

return nil
Expand Down
3 changes: 3 additions & 0 deletions FSNotes/Preferences/PreferencesGitViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class PreferencesGitViewController: NSViewController {
}

@IBAction func origin(_ sender: NSTextField) {
let project = Storage.sharedInstance().getDefault()
project?.gitOrigin = sender.stringValue

UserDefaultsManagement.gitOrigin = sender.stringValue
}

Expand Down
4 changes: 2 additions & 2 deletions FSNotes/ProjectSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ProjectSettingsViewController: NSViewController {
project.gitOrigin = sender.stringValue
project.saveSettings()

if project.isCloudProject() {
if project.isDefault {
UserDefaultsManagement.gitOrigin = sender.stringValue
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ class ProjectSettingsViewController: NSViewController {
directionASC.state = project.sortDirection == .asc ? .on : .off
directionDESC.state = project.sortDirection == .desc ? .on : .off

if project.isCloudProject(), let masterOrigin = UserDefaultsManagement.gitOrigin {
if project.isDefault, let masterOrigin = UserDefaultsManagement.gitOrigin {
origin.stringValue = masterOrigin
} else {
origin.stringValue = project.gitOrigin ?? ""
Expand Down

0 comments on commit 6cd0cd7

Please sign in to comment.