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
4import express from 'express';4import express from 'express';
5import sanitize from 'sanitize-filename';5import sanitize from 'sanitize-filename';
6import { default as simpleGit } from 'simple-git';6import { CheckRepoActions, default as simpleGit } from 'simple-git';
77
8import { PUBLIC_DIRECTORIES } from '../constants.js';8import { PUBLIC_DIRECTORIES } from '../constants.js';
99
@@ -145,6 +145,10 @@ router.post('/update', async (request, response) => {
145145
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);
158162
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});
166169
@@ -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 repo351 // it is not a git repo, or has no commits yet, or is a bare repo