Skip to content

Commit

Permalink
feat: Add an option to automatically fetch updates from remote reposi…
Browse files Browse the repository at this point in the history
…tory when Obsidian starts
  • Loading branch information
denolehov committed Oct 28, 2020
1 parent 0795c1a commit aa59d29
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default class ObsidianGit extends Plugin {
this.git = git;
this.settings = (await this.loadData()) || new ObsidianGitSettings();

if (this.settings.autoPullOnBoot) {
setTimeout(async () => await this.pullWithNotice(), 700);
}

if (this.settings.autoSaveInterval > 0) {
this.enableAutosave();
}
Expand All @@ -28,17 +32,7 @@ export default class ObsidianGit extends Plugin {
this.addCommand({
id: "pull",
name: "Pull from remote repository",
callback: async () => {
new Notice("Pulling changes from remote repository..");
let filesAffected = await this.pullForUpdates();
if (filesAffected > 0) {
new Notice(
`Pulled new changes. ${filesAffected} files affected.`
);
} else {
new Notice("Everything is up-to-date.");
}
},
callback: async () => this.pullWithNotice(),
});

this.addCommand({
Expand All @@ -60,6 +54,16 @@ export default class ObsidianGit extends Plugin {
return status.isClean();
}

async pullWithNotice(): Promise<void> {
new Notice("Pulling changes from remote repository..");
let filesAffected = await this.pullForUpdates();
if (filesAffected > 0) {
new Notice(`Pulled new changes. ${filesAffected} files affected.`);
} else {
new Notice("Everything is up-to-date.");
}
}

async pullForUpdates(): Promise<number> {
const pullResult = await this.git.pull("origin");
return pullResult.files.length;
Expand Down Expand Up @@ -96,6 +100,7 @@ export default class ObsidianGit extends Plugin {
class ObsidianGitSettings {
commitMessage: string = "vault backup";
autoSaveInterval: number = 0;
autoPullOnBoot: boolean = false;
}

class ObsidianGitSettingsTab extends PluginSettingTab {
Expand Down Expand Up @@ -154,5 +159,17 @@ class ObsidianGitSettingsTab extends PluginSettingTab {
}
})
);

new Setting(containerEl)
.setName("Pull updates on startup")
.setDesc("Automatically pull updates when Obsidian starts")
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.autoPullOnBoot)
.onChange((value) => {
plugin.settings.autoPullOnBoot = value;
plugin.saveData(plugin.settings);
})
);
}
}

0 comments on commit aa59d29

Please sign in to comment.