Skip to content

Commit

Permalink
fix: minor new sync method fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jan 29, 2022
1 parent ac4cd1d commit f1d6b33
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -159,7 +160,6 @@ export default class ObsidianGit extends Plugin {
}
});

this.migrateSettings()

if (this.settings.showStatusBar) {
// init statusBar
Expand All @@ -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();
}
}

Expand Down
21 changes: 9 additions & 12 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SyncMethod, string> = {
'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)
Expand Down
43 changes: 22 additions & 21 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f1d6b33

Please sign in to comment.