diff --git a/FSNotes/Business/Project+Git.swift b/FSNotes/Business/Project+Git.swift index 47c6afb67..71ded2ba3 100644 --- a/FSNotes/Business/Project+Git.swift +++ b/FSNotes/Business/Project+Git.swift @@ -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: "support@fsnot.es") + let repository = try repositoryManager.initRepository(at: cloneURL, signature: signature) repository.setWorkTree(path: url.path) @@ -87,6 +112,8 @@ extension Project { return try repositoryManager.openRepository(at: repoURL) } + } catch { + print("Repo init error: \(error)") } return nil diff --git a/FSNotes/Preferences/PreferencesGitViewController.swift b/FSNotes/Preferences/PreferencesGitViewController.swift index c4ec668ce..a8f6045b4 100644 --- a/FSNotes/Preferences/PreferencesGitViewController.swift +++ b/FSNotes/Preferences/PreferencesGitViewController.swift @@ -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 } diff --git a/FSNotes/ProjectSettingsViewController.swift b/FSNotes/ProjectSettingsViewController.swift index 9e99deeb1..a6d94609e 100644 --- a/FSNotes/ProjectSettingsViewController.swift +++ b/FSNotes/ProjectSettingsViewController.swift @@ -90,7 +90,7 @@ class ProjectSettingsViewController: NSViewController { project.gitOrigin = sender.stringValue project.saveSettings() - if project.isCloudProject() { + if project.isDefault { UserDefaultsManagement.gitOrigin = sender.stringValue } } @@ -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 ?? ""