From bc0fc23fb013be5f4dd6d58d32fb150fb1392a07 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 14 Sep 2024 23:12:35 -0400 Subject: [PATCH] Fix self-updater to use the current branch --- src/modules/selfUpdater.mjs | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/modules/selfUpdater.mjs b/src/modules/selfUpdater.mjs index e40da1f..67af5ca 100644 --- a/src/modules/selfUpdater.mjs +++ b/src/modules/selfUpdater.mjs @@ -1,8 +1,8 @@ import { DebugBuilder } from "./debugger.mjs"; const log = new DebugBuilder("client", "selfUpdater"); import simpleGit from 'simple-git'; -import { restartService } from './serviceHandler.mjs' -import { launchProcess } from './subprocessHandler.mjs' +import { restartService } from './serviceHandler.mjs'; +import { launchProcess } from './subprocessHandler.mjs'; const git = simpleGit(); @@ -12,27 +12,31 @@ export const checkForUpdates = async () => { // Fetch remote changes await git.fetch(); - // Get the latest commit hash - const latestCommitHash = await git.revparse(['@{u}']); + // Get the current branch + const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']); + log.INFO(`Current branch is ${currentBranch}`); + + // Get the latest commit hash for the current branch + const latestCommitHash = await git.revparse([`${currentBranch}@{u}`]); // Compare with the local commit hash const localCommitHash = await git.revparse(['HEAD']); if (latestCommitHash !== localCommitHash) { - log.INFO('An update is available. Updating...'); + log.INFO(`An update is available on branch ${currentBranch}. Updating...`); // Check if there have been any changes to the code - const gitStatus = await git.status() + const gitStatus = await git.status(); log.INFO(gitStatus); if (gitStatus.modified.length > 0){ // There is locally modified code - log.INFO("There is locally modified code, resetting..."); + log.INFO("There is locally modified code, stashing changes..."); await git.stash(); - await git.reset('hard', ['origin/master']); } - // Pull the latest changes from the remote repository - await git.pull(); + // Ensure we are on the correct branch and pull the latest changes + await git.checkout(currentBranch); + await git.pull('origin', currentBranch); // Run the post-update script log.INFO('Running post-update script...'); @@ -42,10 +46,10 @@ export const checkForUpdates = async () => { log.INFO('Update completed successfully. Restarting the application...'); restartApplication(); - return true + return true; } else { log.INFO('The application is up to date.'); - return false + return false; } } catch (error) { log.ERROR('Error checking for updates:', error); @@ -53,7 +57,7 @@ export const checkForUpdates = async () => { } // Function to restart the application -export const restartApplication = () => { +export const restartApplication = () => { log.INFO('Restarting the application...'); restartService('discord-radio-bot'); -} \ No newline at end of file +};