Skip to content

Commit

Permalink
feat: add backup button to source control view
Browse files Browse the repository at this point in the history
close #374
  • Loading branch information
Vinzent03 committed Nov 26, 2022
1 parent 8ba0e75 commit 477b166
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
45 changes: 33 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,36 @@ export default class ObsidianGit extends Plugin {
this.addCommand({
id: "commit",
name: "Commit all changes",
callback: () => this.promiseQueue.addTask(() => this.commit(false))
callback: () => this.promiseQueue.addTask(() => this.commit({ fromAutoBackup: false }))
});

this.addCommand({
id: "commit-specified-message",
name: "Commit all changes with specific message",
callback: () => this.promiseQueue.addTask(() => this.commit(false, true))
callback: () => this.promiseQueue.addTask(() => this.commit({
fromAutoBackup: false,
requestCustomMessage: true
}))
});

this.addCommand({
id: "commit-staged",
name: "Commit staged",
callback: () => this.promiseQueue.addTask(() => this.commit(false, false, true))
callback: () => this.promiseQueue.addTask(() => this.commit({
fromAutoBackup: false,
requestCustomMessage: false,
onlyStaged: true
}))
});

this.addCommand({
id: "commit-staged-specified-message",
name: "Commit staged with specific message",
callback: () => this.promiseQueue.addTask(() => this.commit(false, true, true))
callback: () => this.promiseQueue.addTask(() => this.commit({
fromAutoBackup: false,
requestCustomMessage: true,
onlyStaged: true
}))
});

this.addCommand({
Expand Down Expand Up @@ -704,14 +715,14 @@ export default class ObsidianGit extends Plugin {
this.setState(PluginState.idle);
}

async createBackup(fromAutoBackup: boolean, requestCustomMessage = false): Promise<void> {
async createBackup(fromAutoBackup: boolean, requestCustomMessage = false, commitMessage?: string): Promise<void> {
if (!await this.isAllInitialized()) return;

if (this.settings.syncMethod == "reset" && this.settings.pullBeforePush) {
await this.pull();
}

if (!(await this.commit(fromAutoBackup, requestCustomMessage))) return;
if (!(await this.commit({ fromAutoBackup, requestCustomMessage, commitMessage }))) return;

if (!this.settings.disablePush) {
// Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present
Expand All @@ -729,7 +740,17 @@ export default class ObsidianGit extends Plugin {
}

// Returns true if commit was successfully
async commit(fromAutoBackup: boolean, requestCustomMessage = false, onlyStaged = false): Promise<boolean> {
async commit({
fromAutoBackup,
requestCustomMessage = false,
onlyStaged = false,
commitMessage
}: {
fromAutoBackup: boolean,
requestCustomMessage?: boolean,
onlyStaged?: boolean;
commitMessage?: string;
}): Promise<boolean> {
if (!await this.isAllInitialized()) return false;

const hadConflict = this.localStorage.getConflict() === "true";
Expand Down Expand Up @@ -778,25 +799,25 @@ export default class ObsidianGit extends Plugin {


if (changedFiles.length !== 0 || hadConflict) {
let commitMessage = fromAutoBackup ? this.settings.autoCommitMessage : this.settings.commitMessage;
let cmtMessage = commitMessage ??= fromAutoBackup ? this.settings.autoCommitMessage : this.settings.commitMessage;
if ((fromAutoBackup && this.settings.customMessageOnAutoBackup) || requestCustomMessage) {
if (!this.settings.disablePopups && fromAutoBackup) {
new Notice("Auto backup: Please enter a custom commit message. Leave empty to abort",);
}
const tempMessage = await new CustomMessageModal(this, true).open();

if (tempMessage != undefined && tempMessage != "" && tempMessage != "...") {
commitMessage = tempMessage;
cmtMessage = tempMessage;
} else {
this.setState(PluginState.idle);
return false;
}
}
let committedFiles: number | undefined;
if (onlyStaged) {
committedFiles = await this.gitManager.commit(commitMessage);
committedFiles = await this.gitManager.commit(cmtMessage);
} else {
committedFiles = await this.gitManager.commitAll({ message: commitMessage, status, unstagedFiles });
committedFiles = await this.gitManager.commitAll({ message: cmtMessage, status, unstagedFiles });
}
let roughly = false;
if (committedFiles === undefined) {
Expand Down Expand Up @@ -1001,7 +1022,7 @@ export default class ObsidianGit extends Plugin {
doAutoBackup(): void {
this.promiseQueue.addTask(() => {
if (this.settings.differentIntervalCommitAndPush) {
return this.commit(true);
return this.commit({ fromAutoBackup: true });
} else {
return this.createBackup(true);
}
Expand Down
23 changes: 22 additions & 1 deletion src/ui/sidebar/gitView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
FileType,
PluginState,
RootTreeItem,
Status,
Status
} from "src/types";
import { onDestroy } from "svelte";
import { slide } from "svelte/transition";
Expand Down Expand Up @@ -72,6 +72,19 @@
}
}
async function backup() {
loading = true;
if (status) {
plugin.createBackup(false,false,commitMessage)
.then(() => {
if (commitMessage !== plugin.settings.commitMessage) {
commitMessage = "";
}
})
.finally(triggerRefresh);
}
}
async function refresh() {
if (!plugin.gitReady) {
status = undefined;
Expand Down Expand Up @@ -175,6 +188,14 @@
<main>
<div class="nav-header">
<div class="nav-buttons-container">
<div
id="backup-btn"
data-icon="arrow-up-circle"
class="clickable-icon nav-action-button"
aria-label="Backup"
bind:this={buttons[5]}
on:click={backup}
/>
<div
id="commit-btn"
data-icon="check"
Expand Down

0 comments on commit 477b166

Please sign in to comment.