Skip to content

Commit

Permalink
feat: different intervals for commit and push
Browse files Browse the repository at this point in the history
close #106
  • Loading branch information
Vinzent03 committed Jun 9, 2022
1 parent c797128 commit 59367aa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
autoCommitMessage: undefined, // default undefined for settings migration
commitDateFormat: "YYYY-MM-DD HH:mm:ss",
autoSaveInterval: 0,
autoPushInterval: 0,
autoPullInterval: 0,
autoPullOnBoot: false,
disablePush: false,
Expand All @@ -20,6 +21,7 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
treeStructure: false,
refreshSourceControl: true,
basePath: "",
differentIntervalCommitAndPush: false,
};

export const GIT_VIEW_CONFIG = {
Expand Down
54 changes: 48 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class ObsidianGit extends Plugin {
statusBar: StatusBar;
state: PluginState;
timeoutIDBackup: number;
timeoutIDPush: number;
timeoutIDPull: number;
lastUpdate: number;
gitReady = false;
Expand Down Expand Up @@ -204,18 +205,21 @@ export default class ObsidianGit extends Plugin {
await this.saveData(this.settings);
}

async saveLastAuto(date: Date, mode: "backup" | "pull") {
async saveLastAuto(date: Date, mode: "backup" | "pull" | "push") {
if (mode === "backup") {
window.localStorage.setItem(this.manifest.id + ":lastAutoBackup", date.toString());
} else if (mode === "pull") {
window.localStorage.setItem(this.manifest.id + ":lastAutoPull", date.toString());
} else if (mode === "push") {
window.localStorage.setItem(this.manifest.id + ":lastAutoPush", date.toString());
}
}

async loadLastAuto(): Promise<{ "backup": Date, "pull": Date; }> {
async loadLastAuto(): Promise<{ "backup": Date, "pull": Date; "push": Date; }> {
return {
"backup": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoBackup") ?? ""),
"pull": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoPull") ?? "")
"pull": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoPull") ?? ""),
"push": new Date(window.localStorage.getItem(this.manifest.id + ":lastAutoPush") ?? ""),
};
}

Expand Down Expand Up @@ -250,6 +254,12 @@ export default class ObsidianGit extends Plugin {
const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - lastAutos.backup.getTime()) / 1000) / 60));
this.startAutoBackup(diff <= 0 ? 0 : diff);
}
if (this.settings.differentIntervalCommitAndPush && this.settings.autoPushInterval > 0) {
const now = new Date();

const diff = this.settings.autoPushInterval - (Math.round(((now.getTime() - lastAutos.push.getTime()) / 1000) / 60));
this.startAutoPush(diff <= 0 ? 0 : diff);
}
if (this.settings.autoPullInterval > 0) {
const now = new Date();

Expand Down Expand Up @@ -406,7 +416,11 @@ export default class ObsidianGit extends Plugin {
} else {
const pushedFiles = await this.gitManager.push();
this.lastUpdate = Date.now();
this.displayMessage(`Pushed ${pushedFiles} ${pushedFiles > 1 ? 'files' : 'file'} to remote`);
if (pushedFiles > 0) {
this.displayMessage(`Pushed ${pushedFiles} ${pushedFiles > 1 ? 'files' : 'file'} to remote`);
} else {
this.displayMessage(`No changes to push`);
}
this.offlineMode = false;
this.setState(PluginState.idle);

Expand Down Expand Up @@ -457,8 +471,15 @@ export default class ObsidianGit extends Plugin {
}
}

doAutoBackup() {
this.promiseQueue.addTask(() => this.createBackup(true));
// This is used for both auto backup and commit
doAutoBackup(): void {
this.promiseQueue.addTask(() => {
if (this.settings.differentIntervalCommitAndPush) {
return this.commit(true);
} else {
return this.createBackup(true);
}
});
this.saveLastAuto(new Date(), "backup");
this.saveSettings();
this.startAutoBackup();
Expand All @@ -476,6 +497,18 @@ export default class ObsidianGit extends Plugin {
);
}

startAutoPush(minutes?: number) {
this.timeoutIDPush = window.setTimeout(
() => {
this.promiseQueue.addTask(() => this.push());
this.saveLastAuto(new Date(), "push");
this.saveSettings();
this.startAutoPush();
},
(minutes ?? this.settings.autoPushInterval) * 60000
);
}

clearAutoBackup(): boolean {
let wasActive = false;
if (this.timeoutIDBackup) {
Expand All @@ -501,6 +534,15 @@ export default class ObsidianGit extends Plugin {
return false;
}

clearAutoPush(): boolean {
if (this.timeoutIDPush) {
window.clearTimeout(this.timeoutIDPush);
this.timeoutIDPush = undefined;
return true;
}
return false;
}


async handleConflict(conflicted: string[]): Promise<void> {
this.setState(PluginState.conflicted);
Expand Down
65 changes: 59 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,32 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {

containerEl.createEl('br');
containerEl.createEl("h3", { text: "Automatic" });
const commitOrBackup = plugin.settings.differentIntervalCommitAndPush ? "commit" : "backup";

new Setting(containerEl)
.setName("Vault backup interval (minutes)")
.setDesc("Commit and push changes every X minutes. Set to 0 (default) to disable. (See below setting for further configuration!)")
.setName("Split automatic commit and push")
.setDesc("Enable to use separate timer for commit and push")
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.differentIntervalCommitAndPush)
.onChange((value) => {
plugin.settings.differentIntervalCommitAndPush = value;
plugin.saveSettings();
plugin.clearAutoBackup();
plugin.clearAutoPush();
if (plugin.settings.autoSaveInterval > 0) {
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
}
if (value && plugin.settings.autoPushInterval > 0) {
plugin.startAutoPush(plugin.settings.autoPushInterval);
}
this.display();
})
);

new Setting(containerEl)
.setName(`Vault ${commitOrBackup} interval (minutes)`)
.setDesc(`${plugin.settings.differentIntervalCommitAndPush ? "Commit" : "Commit and push"} changes every X minutes. Set to 0 (default) to disable. (See below setting for further configuration!)`)
.addText((text) =>
text
.setValue(String(plugin.settings.autoSaveInterval))
Expand All @@ -28,19 +50,20 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
plugin.clearAutoBackup();
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
new Notice(
`Automatic backup enabled! Every ${plugin.settings.autoSaveInterval} minutes.`
`Automatic ${commitOrBackup} enabled! Every ${plugin.settings.autoSaveInterval} minutes.`
);
} else if (plugin.settings.autoSaveInterval <= 0) {
plugin.clearAutoBackup() &&
new Notice("Automatic backup disabled!");
new Notice(`Automatic ${commitOrBackup} disabled!`);
}
} else {
new Notice("Please specify a valid number.");
}
})
);

new Setting(containerEl)
.setName("If turned on, do auto backup every X minutes after last change. Prevents auto backup while editing a file. If turned off, do auto backup every X minutes. It's independent from last change.")
.setName(`If turned on, do auto ${commitOrBackup} every X minutes after last change. Prevents auto ${commitOrBackup} while editing a file. If turned off, do auto ${commitOrBackup} every X minutes. It's independent from last change.`)
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.autoBackupAfterFileChange)
Expand All @@ -53,6 +76,36 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
}
})
);

if (plugin.settings.differentIntervalCommitAndPush) {
new Setting(containerEl)
.setName(`Vault push interval (minutes)`)
.setDesc("Push changes every X minutes. Set to 0 (default) to disable.")
.addText((text) =>
text
.setValue(String(plugin.settings.autoPushInterval))
.onChange((value) => {
if (!isNaN(Number(value))) {
plugin.settings.autoPushInterval = Number(value);
plugin.saveSettings();

if (plugin.settings.autoPushInterval > 0) {
plugin.clearAutoPush();
plugin.startAutoPush(plugin.settings.autoPushInterval);
new Notice(
`Automatic push enabled! Every ${plugin.settings.autoPushInterval} minutes.`
);
} else if (plugin.settings.autoPushInterval <= 0) {
plugin.clearAutoPush() &&
new Notice("Automatic push disabled!");
}
} else {
new Notice("Please specify a valid number.");
}
})
);
}

new Setting(containerEl)
.setName("Auto pull interval (minutes)")
.setDesc("Pull changes every X minutes. Set to 0 (default) to disable.")
Expand Down Expand Up @@ -115,7 +168,7 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
);

new Setting(containerEl)
.setName("Commit message on auto backup")
.setName("Commit message on auto backup/commit")
.setDesc(
"Available placeholders: {{date}}" +
" (see below), {{hostname}} (see below) and {{numFiles}} (number of changed files in the commit)"
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ObsidianGitSettings {
autoCommitMessage: string;
commitDateFormat: string;
autoSaveInterval: number;
autoPushInterval: number;
autoPullInterval: number;
autoPullOnBoot: boolean;
syncMethod: SyncMethod;
Expand All @@ -16,6 +17,7 @@ export interface ObsidianGitSettings {
customMessageOnAutoBackup: boolean;
autoBackupAfterFileChange: boolean;
treeStructure: boolean;
differentIntervalCommitAndPush: boolean;

/**
* @deprecated Migrated to `syncMethod = 'merge'`
Expand Down

0 comments on commit 59367aa

Please sign in to comment.