Skip to content

Commit

Permalink
fix: does not push when no changes detected (fix #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Mar 13, 2021
1 parent 30d8798 commit d016dee
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ export default class ObsidianGit extends Plugin {
this.addCommand({
id: "push",
name: "Commit *all* changes and push to remote repository",
callback: async () => {
const changedFiles = await this.getFilesChanged();

if (!changedFiles.length) {
this.displayMessage("No changes detected");
this.setState(PluginState.idle);
return;
}
await this.createBackup();
}
callback: () => this.createBackup()
});
}
async onunload() {
Expand Down Expand Up @@ -136,38 +127,49 @@ export default class ObsidianGit extends Plugin {
}

async createBackup() {
const changedFiles = await this.getFilesChanged();
this.setState(PluginState.status);
const status = await this.git.status();

if (changedFiles.length === 0) {
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;
}

await this.add();
await this.commit();
this.displayMessage(`Committed ${changedFiles.length} files`);
const changedFiles = status.files;


if (changedFiles.length !== 0) {
await this.add();
await this.commit();
this.displayMessage(`Committed ${changedFiles.length} files`);
}

const allChangedFiles = (await this.git.diffSummary([currentBranch, trackingBranch])).files;

if (allChangedFiles.length === 0) {
this.displayMessage("No changes detected");
this.setState(PluginState.idle);
return;
}

if (!this.settings.disablePush) {
if (this.settings.pullBeforePush) {
await this.pull();
}
await this.push();
this.displayMessage(`Pushed ${changedFiles.length} files to remote`);
this.displayMessage(`Pushed ${allChangedFiles.length} files to remote`);
}

this.lastUpdate = Date.now();
this.setState(PluginState.idle);
}



// region: main methods
async getFilesChanged(): Promise<FileStatusResult[]> {
this.setState(PluginState.status);
const status = await this.git.status();
return status.files;
}

async add(): Promise<void> {
this.setState(PluginState.add);
await this.git.add(
Expand Down

0 comments on commit d016dee

Please sign in to comment.