Refactor git operations to use baseDir

2c0dcdc4494db39c98bb049c345442facb72f3ff

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

1 files changed, +34 -30Ignore whitespace
src/endpoints/extensions.js+34 -30
@@ -30,17 +30,23 @@ async function getManifest(extensionPath) {
3030 * @returns {Promise<Object>} - Returns the extension information as an object
3131 */
3232async function checkIfRepoIsUpToDate(extensionPath) {
3333 const git = simpleGit({ baseDir: extensionPath });
3434 await git.cwd(extensionPath).fetch('origin');
3535 const currentBranch = await git.cwd(extensionPath).branch();
3636 const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']);
3737 const log = await git.cwd(extensionPath).log({
3838 from: currentCommitHash,
3939 to: `origin/${currentBranch.current}`,
4040 });
4141
4242 // Fetch remote repository information
4343 const remotes = await git.cwd(extensionPath).getRemotes(true);
44+ if (remotes.length === 0) {
45+ return {
46+ isUpToDate: true,
47+ remoteUrl: '',
48+ };
49+ }
4450
4551 return {
4652 isUpToDate: log.total === 0,
@@ -118,7 +124,6 @@ router.post('/install', async (request, response) => {
118124 * @returns {void}
119125 */
120126router.post('/update', async (request, response) => {
121- const git = simpleGit();
122127 if (!request.body.extensionName) {
123128 return response.status(400).send('Bad Request: extensionName is required in the request body.');
124129 }
@@ -139,15 +144,16 @@ router.post('/update', async (request, response) => {
139144 }
140145
141146 const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath);
142147 const currentBranchgit = await git.cwdsimpleGit({ baseDir: extensionPath).branch( });
148+ const currentBranch = await git.branch();
143149 if (!isUpToDate) {
144150 await git.cwd(extensionPath).pull('origin', currentBranch.current);
145151 console.info(`Extension has been updated at ${extensionPath}`);
146152 } else {
147153 console.info(`Extension is up to date at ${extensionPath}`);
148154 }
149155 await git.cwd(extensionPath).fetch('origin');
150156 const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']);
151157 const shortCommitHash = fullCommitHash.slice(0, 7);
152158
153159 return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl });
@@ -160,8 +166,6 @@ router.post('/update', async (request, response) => {
160166
161167router.post('/branches', async (request, response) => {
162168 try {
163- const git = simpleGit();
164-
165169 const { extensionName, global } = request.body;
166170
167171 if (!extensionName) {
@@ -180,18 +184,19 @@ router.post('/branches', async (request, response) => {
180184 return response.status(404).send(`Directory does not exist at ${extensionPath}`);
181185 }
182186
187+ const git = simpleGit({ baseDir: extensionPath });
183188 // Unshallow the repository if it is shallow
184189 const isShallow = await git.cwd(extensionPath).revparse(['--is-shallow-repository']) === 'true';
185190 if (isShallow) {
186191 console.info(`Unshallowing the repository at ${extensionPath}`);
187192 await git.cwd(extensionPath).fetch('origin', ['--unshallow']);
188193 }
189194
190195 // Fetch all branches
191196 await git.cwd(extensionPath).remote(['set-branches', 'origin', '*']);
192197 await git.cwd(extensionPath).fetch('origin');
193198 const localBranches = await git.cwd(extensionPath).branchLocal();
194199 const remoteBranches = await git.cwd(extensionPath).branch(['-r', '--list', 'origin/*']);
195200 const result = [
196201 ...Object.values(localBranches.branches),
197202 ...Object.values(remoteBranches.branches),
@@ -206,8 +211,6 @@ router.post('/branches', async (request, response) => {
206211
207212router.post('/switch', async (request, response) => {
208213 try {
209- const git = simpleGit();
210-
211214 const { extensionName, branch, global } = request.body;
212215
213216 if (!extensionName || !branch) {
@@ -226,18 +229,19 @@ router.post('/switch', async (request, response) => {
226229 return response.status(404).send(`Directory does not exist at ${extensionPath}`);
227230 }
228231
229232 const branchesgit = await git.cwdsimpleGit({ baseDir: extensionPath).branchLocal( });
233+ const branches = await git.branchLocal();
230234
231235 if (String(branch).startsWith('origin/')) {
232236 const localBranch = branch.replace('origin/', '');
233237 if (branches.all.includes(localBranch)) {
234238 console.info(`Branch ${localBranch} already exists locally, checking it out`);
235239 await git.cwd(extensionPath).checkout(localBranch);
236240 return response.sendStatus(204);
237241 }
238242
239243 console.info(`Branch ${localBranch} does not exist locally, creating it from ${branch}`);
240244 await git.cwd(extensionPath).checkoutBranch(localBranch, branch);
241245 return response.sendStatus(204);
242246 }
243247
@@ -247,14 +251,14 @@ router.post('/switch', async (request, response) => {
247251 }
248252
249253 // Check if the branch is already checked out
250254 const currentBranch = await git.cwd(extensionPath).branch();
251255 if (currentBranch.current === branch) {
252256 console.info(`Branch ${branch} is already checked out`);
253257 return response.sendStatus(204);
254258 }
255259
256260 // Checkout the branch
257261 await git.cwd(extensionPath).checkout(branch);
258262 console.info(`Checked out branch ${branch} at ${extensionPath}`);
259263
260264 return response.sendStatus(204);
@@ -319,7 +323,6 @@ router.post('/move', async (request, response) => {
319323 * @returns {void}
320324 */
321325router.post('/version', async (request, response) => {
322- const git = simpleGit();
323326 if (!request.body.extensionName) {
324327 return response.status(400).send('Bad Request: extensionName is required in the request body.');
325328 }
@@ -333,19 +336,20 @@ router.post('/version', async (request, response) => {
333336 return response.status(404).send(`Directory does not exist at ${extensionPath}`);
334337 }
335338
339+ const git = simpleGit({ baseDir: extensionPath });
336340 let currentCommitHash;
337341 try {
338342 currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']);
339343 } catch (error) {
340344 // it is not a git repo, or has no commits yet, or is a bare repo
341345 // not possible to update it, most likely can't get the branch name either
342346 return response.send({ currentBranchName: '', currentCommitHash: '', isUpToDate: true, remoteUrl: '' });
343347 }
344348
345349 const currentBranch = await git.cwd(extensionPath).branch();
346350 // get only the working branch
347351 const currentBranchName = currentBranch.current;
348352 await git.cwd(extensionPath).fetch('origin');
349353 console.debug(extensionName, currentBranchName, currentCommitHash);
350354 const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath);
351355