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