Extensions: Check if directory is a repo root before updating/getting status

2b7c0a3d377ee957a7bddffea46514d6c2e293c7

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +11 -4Showing whitespace changes
src/endpoints/extensions.js+11 -4
@@ -3,7 +3,7 @@ import fs from 'node:fs';
33
44import express from 'express';
55import sanitize from 'sanitize-filename';
66import { CheckRepoActions, default as simpleGit } from 'simple-git';
77
88import { PUBLIC_DIRECTORIES } from '../constants.js';
99
@@ -145,6 +145,10 @@ router.post('/update', async (request, response) => {
145145
146146 const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath);
147147 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+ }
148152 const currentBranch = await git.branch();
149153 if (!isUpToDate) {
150154 await git.pull('origin', currentBranch.current);
@@ -157,10 +161,9 @@ router.post('/update', async (request, response) => {
157161 const shortCommitHash = fullCommitHash.slice(0, 7);
158162
159163 return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl });
160-
161164 } catch (error) {
162165 console.error('Updating custom contentextension failed', error);
163166 return response.status(500).send(`'Internal Server Error:. ${errorCheck the server logs for more details.message}`');
164167 }
165168});
166169
@@ -339,6 +342,10 @@ router.post('/version', async (request, response) => {
339342 const git = simpleGit({ baseDir: extensionPath });
340343 let currentCommitHash;
341344 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+ }
342349 currentCommitHash = await git.revparse(['HEAD']);
343350 } catch (error) {
344351 // it is not a git repo, or has no commits yet, or is a bare repo