From 619c5d182e95c5f1ca946c56d8c002e6b3f09daf Mon Sep 17 00:00:00 2001 From: Vinzent Date: Sun, 14 Mar 2021 14:27:11 +0100 Subject: [PATCH] fix: needed tracking branch to commit --- main.ts | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/main.ts b/main.ts index 100bf42c..a28ee1f7 100644 --- a/main.ts +++ b/main.ts @@ -65,13 +65,6 @@ export default class ObsidianGit extends Plugin { return; } - const remote = await this.git.remote([]); - - if (!remote) { - this.displayMessage("Failed to detect remote.", 0); - return; - } - if (this.settings.autoPullOnBoot) { const filesUpdated = await this.pull(); @@ -131,37 +124,42 @@ export default class ObsidianGit extends Plugin { const status = await this.git.status(); const currentBranch = status.current; - const trackingBranch = status.tracking; - - if (!trackingBranch) { - this.displayError("Did not push. No tracking branch is set!", 10000); - this.setState(PluginState.idle); - return; - } const changedFiles = status.files; - if (changedFiles.length !== 0) { await this.add(); await this.commit(); this.displayMessage(`Committed ${changedFiles.length} files`); + } else { + this.displayMessage("No changes to commit"); } - const allChangedFiles = (await this.git.diffSummary([currentBranch, trackingBranch])).files; + if (!this.settings.disablePush) { + const trackingBranch = status.tracking; - if (allChangedFiles.length === 0) { - this.displayMessage("No changes detected"); - this.setState(PluginState.idle); - return; - } + if (!trackingBranch) { + this.displayError("Did not push. No upstream branch is set! See README for instructions", 10000); + this.setState(PluginState.idle); + return; + } - if (!this.settings.disablePush) { - if (this.settings.pullBeforePush) { - await this.pull(); + const allChangedFiles = (await this.git.diffSummary([currentBranch, trackingBranch])).files; + + // Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present + if (allChangedFiles.length > 0) { + if (this.settings.pullBeforePush) { + const pulledFilesLength = await this.pull(); + if (pulledFilesLength > 0) { + this.displayMessage(`Pulled ${pulledFilesLength} files from remote`); + } + } + await this.push(); + this.displayMessage(`Pushed ${allChangedFiles.length} files to remote`); + } else { + this.displayMessage("No changes to push"); + this.setState(PluginState.idle); } - await this.push(); - this.displayMessage(`Pushed ${allChangedFiles.length} files to remote`); } this.lastUpdate = Date.now();