Extensions: Check if directory is a repo root before updating/getting status
| @@ -3,7 +3,7 @@ import fs from 'node:fs'; | |||
| 3 | 3 | ||
| 4 | import express from 'express'; | 4 | import express from 'express'; |
| 5 | import sanitize from 'sanitize-filename'; | 5 | import sanitize from 'sanitize-filename'; |
| 6 | import { default as simpleGit } from 'simple-git'; | 6 | import { CheckRepoActions, default as simpleGit } from 'simple-git'; |
| 7 | 7 | ||
| 8 | import { PUBLIC_DIRECTORIES } from '../constants.js'; | 8 | import { PUBLIC_DIRECTORIES } from '../constants.js'; |
| 9 | 9 | ||
| @@ -145,6 +145,10 @@ router.post('/update', async (request, response) => { | |||
| 145 | 145 | ||
| 146 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); | 146 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |
| 147 | const git = simpleGit({ baseDir: extensionPath }); | 147 | const git = simpleGit({ baseDir: extensionPath }); |
| 148 | const isRepo = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT); | ||
| 149 | if (!isRepo) { | ||
| 150 | throw new Error(`Directory is not a Git repository at ${extensionPath}`); | ||
| 151 | } | ||
| 148 | const currentBranch = await git.branch(); | 152 | const currentBranch = await git.branch(); |
| 149 | if (!isUpToDate) { | 153 | if (!isUpToDate) { |
| 150 | await git.pull('origin', currentBranch.current); | 154 | await git.pull('origin', currentBranch.current); |
| @@ -157,10 +161,9 @@ router.post('/update', async (request, response) => { | |||
| 157 | const shortCommitHash = fullCommitHash.slice(0, 7); | 161 | const shortCommitHash = fullCommitHash.slice(0, 7); |
| 158 | 162 | ||
| 159 | return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); | 163 | return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); |
| 160 | |||
| 161 | } catch (error) { | 164 | } catch (error) { |
| 162 | console.error('Updating custom content failed', error); | 165 | console.error('Updating extension failed', error); |
| 163 | return response.status(500).send(`Server Error: ${error.message}`); | 166 | return response.status(500).send('Internal Server Error. Check the server logs for more details.'); |
| 164 | } | 167 | } |
| 165 | }); | 168 | }); |
| 166 | 169 | ||
| @@ -339,6 +342,10 @@ router.post('/version', async (request, response) => { | |||
| 339 | const git = simpleGit({ baseDir: extensionPath }); | 342 | const git = simpleGit({ baseDir: extensionPath }); |
| 340 | let currentCommitHash; | 343 | let currentCommitHash; |
| 341 | try { | 344 | try { |
| 345 | const isRepo = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT); | ||
| 346 | if (!isRepo) { | ||
| 347 | throw new Error(`Directory is not a Git repository at ${extensionPath}`); | ||
| 348 | } | ||
| 342 | currentCommitHash = await git.revparse(['HEAD']); | 349 | currentCommitHash = await git.revparse(['HEAD']); |
| 343 | } catch (error) { | 350 | } catch (error) { |
| 344 | // it is not a git repo, or has no commits yet, or is a bare repo | 351 | // it is not a git repo, or has no commits yet, or is a bare repo |