Skip to content

Commit

Permalink
feat: set last auto backup to last commit
Browse files Browse the repository at this point in the history
close #73
  • Loading branch information
Vinzent03 committed Nov 27, 2022
1 parent 9895b9e commit d8cfbf2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
changedFilesInStatusBar: false,
showedMobileNotice: false,
refreshSourceControlTimer: 7000,
showBranchStatusBar: true
showBranchStatusBar: true,
setLastSaveToLastCommit: false,
};

export const GIT_VIEW_CONFIG = {
Expand Down
2 changes: 2 additions & 0 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export abstract class GitManager {

abstract getDiffString(filePath: string, stagedChanges: boolean): Promise<string>;

abstract getLastCommitTime(): Promise<Date | undefined>;


getVaultPath(path: string): string {
if (this.plugin.settings.basePath) {
Expand Down
10 changes: 9 additions & 1 deletion src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,15 @@ export class IsomorphicGit extends GitManager {
const diff = createPatch(filePath, stagedContent, workdirContent);
return diff;
}
};
}

async getLastCommitTime(): Promise<Date | undefined> {
const repo = this.getRepo();
const oid = await this.resolveRef("HEAD");
const commit = await git.readCommit({ ...repo, oid: oid });
const date = commit.commit.committer.timestamp;
return new Date(date * 1000);
}

private getFileStatusResult(row: [string, 0 | 1, 0 | 1 | 2, 0 | 1 | 2 | 3]): FileStatusResult {

Expand Down
70 changes: 49 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ export default class ObsidianGit extends Plugin {
this.showNotices();

try {
if (Platform.isDesktopApp) {
if (!Platform.isDesktopApp) {
this.gitManager = new SimpleGit(this);
await (this.gitManager as SimpleGit).setGitInstance();

Expand Down Expand Up @@ -593,26 +593,7 @@ 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 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();

const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - lastAutos.pull.getTime()) / 1000) / 60));
this.startAutoPull(diff <= 0 ? 0 : diff);
}
this.setUpAutos();
break;
default:
console.log("Something weird happened. The 'checkRequirements' result is " + result);
Expand Down Expand Up @@ -698,6 +679,7 @@ export default class ObsidianGit extends Plugin {
if (!await this.isAllInitialized()) return;

const filesUpdated = await this.pull();
this.setUpAutoBackup();
if (!filesUpdated) {
this.displayMessage("Everything is up-to-date");
}
Expand Down Expand Up @@ -824,6 +806,7 @@ export default class ObsidianGit extends Plugin {
roughly = true;
committedFiles = changedFiles.length;
}
this.setUpAutoBackup();
this.displayMessage(`Committed${roughly ? " approx." : ""} ${committedFiles} ${committedFiles > 1 ? 'files' : 'file'}`);
} else {
this.displayMessage("No changes to commit");
Expand Down Expand Up @@ -1004,6 +987,51 @@ export default class ObsidianGit extends Plugin {
return true;
}

async setUpAutoBackup() {
if (this.settings.setLastSaveToLastCommit) {
this.clearAutoBackup();
const lastCommitDate = await this.gitManager.getLastCommitTime();
if (lastCommitDate) {
this.localStorage.setLastAutoBackup(lastCommitDate.toString());
}
}

if (!this.timeoutIDBackup && !this.onFileModifyEventRef) {
const lastAutos = await this.loadLastAuto();

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

const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - lastAutos.backup.getTime()) / 1000) / 60));
this.startAutoBackup(diff <= 0 ? 0 : diff);
}
}
}

async setUpAutos() {
this.setUpAutoBackup();
const lastAutos = await this.loadLastAuto();

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();

const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - lastAutos.pull.getTime()) / 1000) / 60));
this.startAutoPull(diff <= 0 ? 0 : diff);
}
}

clearAutos(): void {
this.clearAutoBackup();
this.clearAutoPush();
this.clearAutoPull();
}

startAutoBackup(minutes?: number) {
const time = (minutes ?? this.settings.autoSaveInterval) * 60000;
if (this.settings.autoBackupAfterFileChange) {
Expand Down
49 changes: 34 additions & 15 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,40 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName(`Auto Backup after Filechange`)
.setDesc(`If turned on, do auto ${commitOrBackup} every ${plugin.settings.autoSaveInterval} minutes after last change. This also prevents auto ${commitOrBackup} while editing a file. If turned off, it's independent from last the change.`)
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.autoBackupAfterFileChange)
.onChange((value) => {
plugin.settings.autoBackupAfterFileChange = value;
plugin.saveSettings();
plugin.clearAutoBackup();
if (plugin.settings.autoSaveInterval > 0) {
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
}
})
);
if (!plugin.settings.setLastSaveToLastCommit)
new Setting(containerEl)
.setName(`Auto Backup after file change`)
.setDesc(`If turned on, do auto ${commitOrBackup} every ${plugin.settings.autoSaveInterval} minutes after last change. This also prevents auto ${commitOrBackup} while editing a file. If turned off, it's independent from last the change.`)
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.autoBackupAfterFileChange)
.onChange((value) => {
plugin.settings.autoBackupAfterFileChange = value;
this.display();
plugin.saveSettings();
plugin.clearAutoBackup();
if (plugin.settings.autoSaveInterval > 0) {
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
}
})
);

if (!plugin.settings.autoBackupAfterFileChange)
new Setting(containerEl)
.setName(`Auto ${commitOrBackup} after lastest commit`)
.setDesc(`If turned on, set last auto ${commitOrBackup} time to lastest commit`)
.addToggle((toggle) =>
toggle
.setValue(plugin.settings.setLastSaveToLastCommit)
.onChange(async (value) => {
plugin.settings.setLastSaveToLastCommit = value;
plugin.saveSettings();
this.display();
plugin.clearAutoBackup();
await plugin.setUpAutoBackup();
})
);


if (plugin.settings.differentIntervalCommitAndPush) {
new Setting(containerEl)
Expand Down
7 changes: 7 additions & 0 deletions src/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ export class SimpleGit extends GitManager {
return (await this.git.diff([`${commit1}..${commit2}`, "--", file]));
}

async getLastCommitTime(): Promise<Date | undefined> {
const res = await this.git.log({ n: 1 }, (err) => this.onError(err));
if (res != null && res.latest != null) {
return new Date(res.latest.date);
}
}

private isGitInstalled(): boolean {
// https://github.com/steveukx/git-js/issues/402
const command = spawnSync(this.plugin.localStorage.getGitPath() || 'git', ['--version'], {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ObsidianGitSettings {
showedMobileNotice: boolean;
refreshSourceControlTimer: number;
showBranchStatusBar: boolean;
setLastSaveToLastCommit: boolean;
}

export type SyncMethod = 'rebase' | 'merge' | 'reset';
Expand Down
1 change: 1 addition & 0 deletions src/ui/sidebar/gitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
if (commitMessage !== plugin.settings.commitMessage) {
commitMessage = "";
}
plugin.setUpAutoBackup();
})
.finally(triggerRefresh);
}
Expand Down

0 comments on commit d8cfbf2

Please sign in to comment.