Fix self-updater to use the current branch

This commit is contained in:
Logan Cusano
2024-09-14 23:12:35 -04:00
parent 821e4f6a64
commit bc0fc23fb0

View File

@@ -1,8 +1,8 @@
import { DebugBuilder } from "./debugger.mjs"; import { DebugBuilder } from "./debugger.mjs";
const log = new DebugBuilder("client", "selfUpdater"); const log = new DebugBuilder("client", "selfUpdater");
import simpleGit from 'simple-git'; import simpleGit from 'simple-git';
import { restartService } from './serviceHandler.mjs' import { restartService } from './serviceHandler.mjs';
import { launchProcess } from './subprocessHandler.mjs' import { launchProcess } from './subprocessHandler.mjs';
const git = simpleGit(); const git = simpleGit();
@@ -12,27 +12,31 @@ export const checkForUpdates = async () => {
// Fetch remote changes // Fetch remote changes
await git.fetch(); await git.fetch();
// Get the latest commit hash // Get the current branch
const latestCommitHash = await git.revparse(['@{u}']); 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 // Compare with the local commit hash
const localCommitHash = await git.revparse(['HEAD']); const localCommitHash = await git.revparse(['HEAD']);
if (latestCommitHash !== localCommitHash) { 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 // Check if there have been any changes to the code
const gitStatus = await git.status() const gitStatus = await git.status();
log.INFO(gitStatus); log.INFO(gitStatus);
if (gitStatus.modified.length > 0){ if (gitStatus.modified.length > 0){
// There is locally modified code // 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.stash();
await git.reset('hard', ['origin/master']);
} }
// Pull the latest changes from the remote repository // Ensure we are on the correct branch and pull the latest changes
await git.pull(); await git.checkout(currentBranch);
await git.pull('origin', currentBranch);
// Run the post-update script // Run the post-update script
log.INFO('Running 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...'); log.INFO('Update completed successfully. Restarting the application...');
restartApplication(); restartApplication();
return true return true;
} else { } else {
log.INFO('The application is up to date.'); log.INFO('The application is up to date.');
return false return false;
} }
} catch (error) { } catch (error) {
log.ERROR('Error checking for updates:', error); log.ERROR('Error checking for updates:', error);
@@ -53,7 +57,7 @@ export const checkForUpdates = async () => {
} }
// Function to restart the application // Function to restart the application
export const restartApplication = () => { export const restartApplication = () => {
log.INFO('Restarting the application...'); log.INFO('Restarting the application...');
restartService('discord-radio-bot'); restartService('discord-radio-bot');
} };