diff --git a/src/main.ts b/src/main.ts index 2bcb6f65..80748c6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -37,6 +37,7 @@ export default class ObsidianGit extends Plugin { async onload() { console.log('loading ' + this.manifest.name + " plugin"); await this.loadSettings(); + this.migrateSettings(); addIcons(); @@ -159,7 +160,6 @@ export default class ObsidianGit extends Plugin { } }); - this.migrateSettings() if (this.settings.showStatusBar) { // init statusBar @@ -174,10 +174,10 @@ export default class ObsidianGit extends Plugin { } migrateSettings() { - if(this.settings.mergeOnPull) { - this.settings.syncMethod = 'merge' - this.settings.mergeOnPull = undefined - this.saveSettings() + if (this.settings.mergeOnPull != undefined) { + this.settings.syncMethod = this.settings.mergeOnPull ? 'merge' : 'rebase'; + this.settings.mergeOnPull = undefined; + this.saveSettings(); } } diff --git a/src/settings.ts b/src/settings.ts index 95ac61bc..aaa75421 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -87,22 +87,19 @@ export class ObsidianGitSettingsTab extends PluginSettingTab { .setDesc( "Selects the method used for handling new changes found in your remote git repository." ) - .addDropdown(async (dropdown) => { + .addDropdown((dropdown) => { const options: Record = { 'merge': 'Merge', 'rebase': 'Rebase', 'reset': 'None (for use with Obsidian Sync)', - } - dropdown.addOptions(options) - if (plugin.settings.syncMethod) { - dropdown.setValue(plugin.settings.syncMethod) - } else { - dropdown.setValue('merge') - } - dropdown.onChange(async (option) => { - plugin.settings.syncMethod = option as SyncMethod - plugin.saveSettings() - }) + }; + dropdown.addOptions(options); + dropdown.setValue(plugin.settings.syncMethod); + + dropdown.onChange(async (option: SyncMethod) => { + plugin.settings.syncMethod = option; + plugin.saveSettings(); + }); }); new Setting(containerEl) diff --git a/src/simpleGit.ts b/src/simpleGit.ts index ece69644..8ba108cd 100644 --- a/src/simpleGit.ts +++ b/src/simpleGit.ts @@ -2,7 +2,6 @@ import { spawnSync } from "child_process"; import { FileSystemAdapter } from "obsidian"; import * as path from "path"; import simpleGit, * as simple from "simple-git"; -import { Response } from "simple-git"; import { GitManager } from "./gitManager"; import ObsidianGit from "./main"; import { BranchInfo, FileStatusResult, PluginState } from "./types"; @@ -148,40 +147,42 @@ export class SimpleGit extends GitManager { if (this.plugin.settings.updateSubmodules) await this.git.subModule(["update", "--remote", "--merge", "--recursive"], (err: any) => this.onError(err)); - const branchInfo = await this.branchInfo() - const localCommit = await this.git.revparse([branchInfo.current]) + const branchInfo = await this.branchInfo(); + const localCommit = await this.git.revparse([branchInfo.current]); - await this.git.fetch() - const upstreamCommit = await this.git.revparse([branchInfo.tracking]) + await this.git.fetch((err: any) => this.onError(err)); + const upstreamCommit = await this.git.revparse([branchInfo.tracking]); if (localCommit !== upstreamCommit) { if (this.plugin.settings.syncMethod === 'merge' || this.plugin.settings.syncMethod === 'rebase') { try { - switch(this.plugin.settings.syncMethod) { + switch (this.plugin.settings.syncMethod) { case 'merge': - this.git.merge([branchInfo.tracking]) - break + await this.git.merge([branchInfo.tracking]); + break; case 'rebase': - this.git.rebase([branchInfo.tracking]) - break + await this.git.rebase([branchInfo.tracking]); + } - } catch(err) { - this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`) - } - const status = await this.git.status(); - if (status.conflicted.length > 0) { - this.plugin.handleConflict(status.conflicted); + } catch (err) { + this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`); + const status = await this.git.status(); + if (status.conflicted.length > 0) { + this.plugin.handleConflict(status.conflicted); + } + return; } - } else if(this.plugin.settings.syncMethod === 'reset') { + + } else if (this.plugin.settings.syncMethod === 'reset') { try { - await this.git.raw(['update-ref', `refs/heads/${branchInfo.current}`, upstreamCommit]) + await this.git.raw(['update-ref', `refs/heads/${branchInfo.current}`, upstreamCommit]); } catch (err) { - this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`) + this.plugin.displayError(`Sync failed (${this.plugin.settings.syncMethod}): ${err.message}`); } } - const filesChanged = await this.git.diff([`${localCommit}..${upstreamCommit}`, '--name-only']) - return filesChanged.split(/\r\n|\r|\n/).filter((value) => value.length > 0).length + const filesChanged = await this.git.diff([`${localCommit}..${upstreamCommit}`, '--name-only']); + return filesChanged.split(/\r\n|\r|\n/).filter((value) => value.length > 0).length; } else { return 0; } diff --git a/src/types.ts b/src/types.ts index 63899e28..6225eecf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,11 +16,13 @@ export interface ObsidianGitSettings { autoBackupAfterFileChange: boolean; treeStructure: boolean; - /* Obsolete settings */ - mergeOnPull?: boolean; // Migrated to `syncMethod = 'merge'` + /** + * @deprecated Migrated to `syncMethod = 'merge'` + */ + mergeOnPull?: boolean; } -export type SyncMethod = 'rebase' | 'merge' | 'reset' +export type SyncMethod = 'rebase' | 'merge' | 'reset'; export interface Author { name: string;