Skip to content

Commit

Permalink
fix: storing of last auto backup/pull caused merge conflicts
Browse files Browse the repository at this point in the history
close #74
  • Loading branch information
Vinzent03 committed May 12, 2021
1 parent 57cfd73 commit cf6f279
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ https://github.com/flathub/md.obsidian.Obsidian/issues/5#issuecomment-736974662
To exclude cache files from the repository, create `.gitignore` file in the root of your vault and add the following lines:
```
# to exclude Obsidian workspace settings (including plugin and hotkey configurations)
.obsidian/
.obsidian/
# OR only to exclude workspace cache
.obsidian/workspace
.obsidian/workspace
# This file is used to keep track of last auto backup/pull
.obsidian-git-data
# Add below lines to exclude OS settings and caches
.trash/
Expand Down
46 changes: 35 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ interface ObsidianGitSettings {
disablePopups: boolean;
listChangedFilesInMessageBody: boolean;
showStatusBar: boolean;
lastAutoBackUp: string;
lastAutoPull: string;
}
const DEFAULT_SETTINGS: ObsidianGitSettings = {
commitMessage: "vault backup: {{date}}",
Expand All @@ -36,8 +34,6 @@ const DEFAULT_SETTINGS: ObsidianGitSettings = {
disablePopups: false,
listChangedFilesInMessageBody: false,
showStatusBar: true,
lastAutoBackUp: "",
lastAutoPull: ""
};

export default class ObsidianGit extends Plugin {
Expand Down Expand Up @@ -112,6 +108,35 @@ export default class ObsidianGit extends Plugin {
await this.saveData(this.settings);
}

async saveLastAuto(date: Date, mode: "backup" | "pull") {
const fileName = ".obsidian-git-data";
let data = "\n";
if (await this.app.vault.adapter.exists(fileName)) {
data = await this.app.vault.adapter.read(fileName);
}
const lines = data.split("\n");
if (mode === "backup") {
lines[0] = date.toString();
} else if (mode === "pull") {
lines[1] = date.toString();
}

await this.app.vault.adapter.write(fileName, lines.join("\n"));
}

async loadLastAuto(): Promise<{ "backup": Date, "pull": Date; }> {
const fileName = ".obsidian-git-data";
let data = "\n";
if (await this.app.vault.adapter.exists(fileName)) {
data = await this.app.vault.adapter.read(fileName);
}
const lines = data.split("\n");
return {
"backup": new Date(lines[0]),
"pull": new Date(lines[1])
};
}

async init(): Promise<void> {
if (!this.isGitInstalled()) {
this.displayError("Cannot run git command");
Expand All @@ -134,19 +159,18 @@ export default class ObsidianGit extends Plugin {
if (this.settings.autoPullOnBoot) {
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
}
const lastAutos = await this.loadLastAuto();

if (this.settings.autoSaveInterval > 0) {
const now = new Date();
const last = new Date(this.settings.lastAutoBackUp);

const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - last.getTime()) / 1000) / 60));
const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - lastAutos.backup.getTime()) / 1000) / 60));
this.startAutoBackup(diff <= 0 ? 0 : diff);
}
if (this.settings.autoPullInterval > 0) {
const now = new Date();
const last = new Date(this.settings.lastAutoPull);

const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - last.getTime()) / 1000) / 60));
const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - lastAutos.pull.getTime()) / 1000) / 60));
this.startAutoPull(diff <= 0 ? 0 : diff);
}
}
Expand Down Expand Up @@ -326,7 +350,7 @@ export default class ObsidianGit extends Plugin {
this.timeoutIDBackup = window.setTimeout(
() => {
this.promiseQueue.addTask(() => this.createBackup(true));
this.settings.lastAutoBackUp = new Date().toString();
this.saveLastAuto(new Date(), "backup");
this.saveSettings();
this.startAutoBackup();
},
Expand All @@ -338,7 +362,7 @@ export default class ObsidianGit extends Plugin {
this.timeoutIDPull = window.setTimeout(
() => {
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
this.settings.lastAutoPull = new Date().toString();
this.saveLastAuto(new Date(), "pull");
this.saveSettings();
this.startAutoPull();
},
Expand Down Expand Up @@ -504,7 +528,7 @@ class ObsidianGitSettingsTab extends PluginSettingTab {

if (plugin.settings.autoPullInterval > 0) {
plugin.clearAutoPull();
plugin.startAutoPull(plugin.settings.autoSaveInterval);
plugin.startAutoPull(plugin.settings.autoPullInterval);
new Notice(
`Automatic pull enabled! Every ${plugin.settings.autoPullInterval} minutes.`
);
Expand Down

0 comments on commit cf6f279

Please sign in to comment.