Skip to content

Commit d8cfbf2

Browse files
committed
feat: set last auto backup to last commit
close #73
1 parent 9895b9e commit d8cfbf2

File tree

8 files changed

+105
-38
lines changed

8 files changed

+105
-38
lines changed

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
2525
changedFilesInStatusBar: false,
2626
showedMobileNotice: false,
2727
refreshSourceControlTimer: 7000,
28-
showBranchStatusBar: true
28+
showBranchStatusBar: true,
29+
setLastSaveToLastCommit: false,
2930
};
3031

3132
export const GIT_VIEW_CONFIG = {

src/gitManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export abstract class GitManager {
7575

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

78+
abstract getLastCommitTime(): Promise<Date | undefined>;
79+
7880

7981
getVaultPath(path: string): string {
8082
if (this.plugin.settings.basePath) {

src/isomorphicGit.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,15 @@ export class IsomorphicGit extends GitManager {
770770
const diff = createPatch(filePath, stagedContent, workdirContent);
771771
return diff;
772772
}
773-
};
773+
}
774+
775+
async getLastCommitTime(): Promise<Date | undefined> {
776+
const repo = this.getRepo();
777+
const oid = await this.resolveRef("HEAD");
778+
const commit = await git.readCommit({ ...repo, oid: oid });
779+
const date = commit.commit.committer.timestamp;
780+
return new Date(date * 1000);
781+
}
774782

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

src/main.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ export default class ObsidianGit extends Plugin {
548548
this.showNotices();
549549

550550
try {
551-
if (Platform.isDesktopApp) {
551+
if (!Platform.isDesktopApp) {
552552
this.gitManager = new SimpleGit(this);
553553
await (this.gitManager as SimpleGit).setGitInstance();
554554

@@ -593,26 +593,7 @@ export default class ObsidianGit extends Plugin {
593593
if (this.settings.autoPullOnBoot) {
594594
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
595595
}
596-
const lastAutos = await this.loadLastAuto();
597-
598-
if (this.settings.autoSaveInterval > 0) {
599-
const now = new Date();
600-
601-
const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - lastAutos.backup.getTime()) / 1000) / 60));
602-
this.startAutoBackup(diff <= 0 ? 0 : diff);
603-
}
604-
if (this.settings.differentIntervalCommitAndPush && this.settings.autoPushInterval > 0) {
605-
const now = new Date();
606-
607-
const diff = this.settings.autoPushInterval - (Math.round(((now.getTime() - lastAutos.push.getTime()) / 1000) / 60));
608-
this.startAutoPush(diff <= 0 ? 0 : diff);
609-
}
610-
if (this.settings.autoPullInterval > 0) {
611-
const now = new Date();
612-
613-
const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - lastAutos.pull.getTime()) / 1000) / 60));
614-
this.startAutoPull(diff <= 0 ? 0 : diff);
615-
}
596+
this.setUpAutos();
616597
break;
617598
default:
618599
console.log("Something weird happened. The 'checkRequirements' result is " + result);
@@ -698,6 +679,7 @@ export default class ObsidianGit extends Plugin {
698679
if (!await this.isAllInitialized()) return;
699680

700681
const filesUpdated = await this.pull();
682+
this.setUpAutoBackup();
701683
if (!filesUpdated) {
702684
this.displayMessage("Everything is up-to-date");
703685
}
@@ -824,6 +806,7 @@ export default class ObsidianGit extends Plugin {
824806
roughly = true;
825807
committedFiles = changedFiles.length;
826808
}
809+
this.setUpAutoBackup();
827810
this.displayMessage(`Committed${roughly ? " approx." : ""} ${committedFiles} ${committedFiles > 1 ? 'files' : 'file'}`);
828811
} else {
829812
this.displayMessage("No changes to commit");
@@ -1004,6 +987,51 @@ export default class ObsidianGit extends Plugin {
1004987
return true;
1005988
}
1006989

990+
async setUpAutoBackup() {
991+
if (this.settings.setLastSaveToLastCommit) {
992+
this.clearAutoBackup();
993+
const lastCommitDate = await this.gitManager.getLastCommitTime();
994+
if (lastCommitDate) {
995+
this.localStorage.setLastAutoBackup(lastCommitDate.toString());
996+
}
997+
}
998+
999+
if (!this.timeoutIDBackup && !this.onFileModifyEventRef) {
1000+
const lastAutos = await this.loadLastAuto();
1001+
1002+
if (this.settings.autoSaveInterval > 0) {
1003+
const now = new Date();
1004+
1005+
const diff = this.settings.autoSaveInterval - (Math.round(((now.getTime() - lastAutos.backup.getTime()) / 1000) / 60));
1006+
this.startAutoBackup(diff <= 0 ? 0 : diff);
1007+
}
1008+
}
1009+
}
1010+
1011+
async setUpAutos() {
1012+
this.setUpAutoBackup();
1013+
const lastAutos = await this.loadLastAuto();
1014+
1015+
if (this.settings.differentIntervalCommitAndPush && this.settings.autoPushInterval > 0) {
1016+
const now = new Date();
1017+
1018+
const diff = this.settings.autoPushInterval - (Math.round(((now.getTime() - lastAutos.push.getTime()) / 1000) / 60));
1019+
this.startAutoPush(diff <= 0 ? 0 : diff);
1020+
}
1021+
if (this.settings.autoPullInterval > 0) {
1022+
const now = new Date();
1023+
1024+
const diff = this.settings.autoPullInterval - (Math.round(((now.getTime() - lastAutos.pull.getTime()) / 1000) / 60));
1025+
this.startAutoPull(diff <= 0 ? 0 : diff);
1026+
}
1027+
}
1028+
1029+
clearAutos(): void {
1030+
this.clearAutoBackup();
1031+
this.clearAutoPush();
1032+
this.clearAutoPull();
1033+
}
1034+
10071035
startAutoBackup(minutes?: number) {
10081036
const time = (minutes ?? this.settings.autoSaveInterval) * 60000;
10091037
if (this.settings.autoBackupAfterFileChange) {

src/settings.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,40 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
6969
})
7070
);
7171

72-
new Setting(containerEl)
73-
.setName(`Auto Backup after Filechange`)
74-
.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.`)
75-
.addToggle((toggle) =>
76-
toggle
77-
.setValue(plugin.settings.autoBackupAfterFileChange)
78-
.onChange((value) => {
79-
plugin.settings.autoBackupAfterFileChange = value;
80-
plugin.saveSettings();
81-
plugin.clearAutoBackup();
82-
if (plugin.settings.autoSaveInterval > 0) {
83-
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
84-
}
85-
})
86-
);
72+
if (!plugin.settings.setLastSaveToLastCommit)
73+
new Setting(containerEl)
74+
.setName(`Auto Backup after file change`)
75+
.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.`)
76+
.addToggle((toggle) =>
77+
toggle
78+
.setValue(plugin.settings.autoBackupAfterFileChange)
79+
.onChange((value) => {
80+
plugin.settings.autoBackupAfterFileChange = value;
81+
this.display();
82+
plugin.saveSettings();
83+
plugin.clearAutoBackup();
84+
if (plugin.settings.autoSaveInterval > 0) {
85+
plugin.startAutoBackup(plugin.settings.autoSaveInterval);
86+
}
87+
})
88+
);
89+
90+
if (!plugin.settings.autoBackupAfterFileChange)
91+
new Setting(containerEl)
92+
.setName(`Auto ${commitOrBackup} after lastest commit`)
93+
.setDesc(`If turned on, set last auto ${commitOrBackup} time to lastest commit`)
94+
.addToggle((toggle) =>
95+
toggle
96+
.setValue(plugin.settings.setLastSaveToLastCommit)
97+
.onChange(async (value) => {
98+
plugin.settings.setLastSaveToLastCommit = value;
99+
plugin.saveSettings();
100+
this.display();
101+
plugin.clearAutoBackup();
102+
await plugin.setUpAutoBackup();
103+
})
104+
);
105+
87106

88107
if (plugin.settings.differentIntervalCommitAndPush) {
89108
new Setting(containerEl)

src/simpleGit.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,13 @@ export class SimpleGit extends GitManager {
440440
return (await this.git.diff([`${commit1}..${commit2}`, "--", file]));
441441
}
442442

443+
async getLastCommitTime(): Promise<Date | undefined> {
444+
const res = await this.git.log({ n: 1 }, (err) => this.onError(err));
445+
if (res != null && res.latest != null) {
446+
return new Date(res.latest.date);
447+
}
448+
}
449+
443450
private isGitInstalled(): boolean {
444451
// https://github.com/steveukx/git-js/issues/402
445452
const command = spawnSync(this.plugin.localStorage.getGitPath() || 'git', ['--version'], {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface ObsidianGitSettings {
3636
showedMobileNotice: boolean;
3737
refreshSourceControlTimer: number;
3838
showBranchStatusBar: boolean;
39+
setLastSaveToLastCommit: boolean;
3940
}
4041

4142
export type SyncMethod = 'rebase' | 'merge' | 'reset';

src/ui/sidebar/gitView.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
if (commitMessage !== plugin.settings.commitMessage) {
6868
commitMessage = "";
6969
}
70+
plugin.setUpAutoBackup();
7071
})
7172
.finally(triggerRefresh);
7273
}

0 commit comments

Comments
 (0)