Refactor git operations to use baseDir
| @@ -30,17 +30,23 @@ async function getManifest(extensionPath) { | |||
| 30 | * @returns {Promise<Object>} - Returns the extension information as an object | 30 | * @returns {Promise<Object>} - Returns the extension information as an object |
| 31 | */ | 31 | */ |
| 32 | async function checkIfRepoIsUpToDate(extensionPath) { | 32 | async function checkIfRepoIsUpToDate(extensionPath) { |
| 33 | const git = simpleGit(); | 33 | const git = simpleGit({ baseDir: extensionPath }); |
| 34 | await git.cwd(extensionPath).fetch('origin'); | 34 | await git.fetch('origin'); |
| 35 | const currentBranch = await git.cwd(extensionPath).branch(); | 35 | const currentBranch = await git.branch(); |
| 36 | const currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); | 36 | const currentCommitHash = await git.revparse(['HEAD']); |
| 37 | const log = await git.cwd(extensionPath).log({ | 37 | const log = await git.log({ |
| 38 | from: currentCommitHash, | 38 | from: currentCommitHash, |
| 39 | to: `origin/${currentBranch.current}`, | 39 | to: `origin/${currentBranch.current}`, |
| 40 | }); | 40 | }); |
| 41 | 41 | ||
| 42 | // Fetch remote repository information | 42 | // Fetch remote repository information |
| 43 | const remotes = await git.cwd(extensionPath).getRemotes(true); | 43 | const remotes = await git.getRemotes(true); |
| 44 | if (remotes.length === 0) { | ||
| 45 | return { | ||
| 46 | isUpToDate: true, | ||
| 47 | remoteUrl: '', | ||
| 48 | }; | ||
| 49 | } | ||
| 44 | 50 | ||
| 45 | return { | 51 | return { |
| 46 | isUpToDate: log.total === 0, | 52 | isUpToDate: log.total === 0, |
| @@ -118,7 +124,6 @@ router.post('/install', async (request, response) => { | |||
| 118 | * @returns {void} | 124 | * @returns {void} |
| 119 | */ | 125 | */ |
| 120 | router.post('/update', async (request, response) => { | 126 | router.post('/update', async (request, response) => { |
| 121 | const git = simpleGit(); | ||
| 122 | if (!request.body.extensionName) { | 127 | if (!request.body.extensionName) { |
| 123 | return response.status(400).send('Bad Request: extensionName is required in the request body.'); | 128 | return response.status(400).send('Bad Request: extensionName is required in the request body.'); |
| 124 | } | 129 | } |
| @@ -139,15 +144,16 @@ router.post('/update', async (request, response) => { | |||
| 139 | } | 144 | } |
| 140 | 145 | ||
| 141 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); | 146 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |
| 142 | const currentBranch = await git.cwd(extensionPath).branch(); | 147 | const git = simpleGit({ baseDir: extensionPath }); |
| 148 | const currentBranch = await git.branch(); | ||
| 143 | if (!isUpToDate) { | 149 | if (!isUpToDate) { |
| 144 | await git.cwd(extensionPath).pull('origin', currentBranch.current); | 150 | await git.pull('origin', currentBranch.current); |
| 145 | console.info(`Extension has been updated at ${extensionPath}`); | 151 | console.info(`Extension has been updated at ${extensionPath}`); |
| 146 | } else { | 152 | } else { |
| 147 | console.info(`Extension is up to date at ${extensionPath}`); | 153 | console.info(`Extension is up to date at ${extensionPath}`); |
| 148 | } | 154 | } |
| 149 | await git.cwd(extensionPath).fetch('origin'); | 155 | await git.fetch('origin'); |
| 150 | const fullCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); | 156 | const fullCommitHash = await git.revparse(['HEAD']); |
| 151 | const shortCommitHash = fullCommitHash.slice(0, 7); | 157 | const shortCommitHash = fullCommitHash.slice(0, 7); |
| 152 | 158 | ||
| 153 | return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); | 159 | return response.send({ shortCommitHash, extensionPath, isUpToDate, remoteUrl }); |
| @@ -160,8 +166,6 @@ router.post('/update', async (request, response) => { | |||
| 160 | 166 | ||
| 161 | router.post('/branches', async (request, response) => { | 167 | router.post('/branches', async (request, response) => { |
| 162 | try { | 168 | try { |
| 163 | const git = simpleGit(); | ||
| 164 | |||
| 165 | const { extensionName, global } = request.body; | 169 | const { extensionName, global } = request.body; |
| 166 | 170 | ||
| 167 | if (!extensionName) { | 171 | if (!extensionName) { |
| @@ -180,18 +184,19 @@ router.post('/branches', async (request, response) => { | |||
| 180 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); | 184 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); |
| 181 | } | 185 | } |
| 182 | 186 | ||
| 187 | const git = simpleGit({ baseDir: extensionPath }); | ||
| 183 | // Unshallow the repository if it is shallow | 188 | // Unshallow the repository if it is shallow |
| 184 | const isShallow = await git.cwd(extensionPath).revparse(['--is-shallow-repository']) === 'true'; | 189 | const isShallow = await git.revparse(['--is-shallow-repository']) === 'true'; |
| 185 | if (isShallow) { | 190 | if (isShallow) { |
| 186 | console.info(`Unshallowing the repository at ${extensionPath}`); | 191 | console.info(`Unshallowing the repository at ${extensionPath}`); |
| 187 | await git.cwd(extensionPath).fetch('origin', ['--unshallow']); | 192 | await git.fetch('origin', ['--unshallow']); |
| 188 | } | 193 | } |
| 189 | 194 | ||
| 190 | // Fetch all branches | 195 | // Fetch all branches |
| 191 | await git.cwd(extensionPath).remote(['set-branches', 'origin', '*']); | 196 | await git.remote(['set-branches', 'origin', '*']); |
| 192 | await git.cwd(extensionPath).fetch('origin'); | 197 | await git.fetch('origin'); |
| 193 | const localBranches = await git.cwd(extensionPath).branchLocal(); | 198 | const localBranches = await git.branchLocal(); |
| 194 | const remoteBranches = await git.cwd(extensionPath).branch(['-r', '--list', 'origin/*']); | 199 | const remoteBranches = await git.branch(['-r', '--list', 'origin/*']); |
| 195 | const result = [ | 200 | const result = [ |
| 196 | ...Object.values(localBranches.branches), | 201 | ...Object.values(localBranches.branches), |
| 197 | ...Object.values(remoteBranches.branches), | 202 | ...Object.values(remoteBranches.branches), |
| @@ -206,8 +211,6 @@ router.post('/branches', async (request, response) => { | |||
| 206 | 211 | ||
| 207 | router.post('/switch', async (request, response) => { | 212 | router.post('/switch', async (request, response) => { |
| 208 | try { | 213 | try { |
| 209 | const git = simpleGit(); | ||
| 210 | |||
| 211 | const { extensionName, branch, global } = request.body; | 214 | const { extensionName, branch, global } = request.body; |
| 212 | 215 | ||
| 213 | if (!extensionName || !branch) { | 216 | if (!extensionName || !branch) { |
| @@ -226,18 +229,19 @@ router.post('/switch', async (request, response) => { | |||
| 226 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); | 229 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); |
| 227 | } | 230 | } |
| 228 | 231 | ||
| 229 | const branches = await git.cwd(extensionPath).branchLocal(); | 232 | const git = simpleGit({ baseDir: extensionPath }); |
| 233 | const branches = await git.branchLocal(); | ||
| 230 | 234 | ||
| 231 | if (String(branch).startsWith('origin/')) { | 235 | if (String(branch).startsWith('origin/')) { |
| 232 | const localBranch = branch.replace('origin/', ''); | 236 | const localBranch = branch.replace('origin/', ''); |
| 233 | if (branches.all.includes(localBranch)) { | 237 | if (branches.all.includes(localBranch)) { |
| 234 | console.info(`Branch ${localBranch} already exists locally, checking it out`); | 238 | console.info(`Branch ${localBranch} already exists locally, checking it out`); |
| 235 | await git.cwd(extensionPath).checkout(localBranch); | 239 | await git.checkout(localBranch); |
| 236 | return response.sendStatus(204); | 240 | return response.sendStatus(204); |
| 237 | } | 241 | } |
| 238 | 242 | ||
| 239 | console.info(`Branch ${localBranch} does not exist locally, creating it from ${branch}`); | 243 | console.info(`Branch ${localBranch} does not exist locally, creating it from ${branch}`); |
| 240 | await git.cwd(extensionPath).checkoutBranch(localBranch, branch); | 244 | await git.checkoutBranch(localBranch, branch); |
| 241 | return response.sendStatus(204); | 245 | return response.sendStatus(204); |
| 242 | } | 246 | } |
| 243 | 247 | ||
| @@ -247,14 +251,14 @@ router.post('/switch', async (request, response) => { | |||
| 247 | } | 251 | } |
| 248 | 252 | ||
| 249 | // Check if the branch is already checked out | 253 | // Check if the branch is already checked out |
| 250 | const currentBranch = await git.cwd(extensionPath).branch(); | 254 | const currentBranch = await git.branch(); |
| 251 | if (currentBranch.current === branch) { | 255 | if (currentBranch.current === branch) { |
| 252 | console.info(`Branch ${branch} is already checked out`); | 256 | console.info(`Branch ${branch} is already checked out`); |
| 253 | return response.sendStatus(204); | 257 | return response.sendStatus(204); |
| 254 | } | 258 | } |
| 255 | 259 | ||
| 256 | // Checkout the branch | 260 | // Checkout the branch |
| 257 | await git.cwd(extensionPath).checkout(branch); | 261 | await git.checkout(branch); |
| 258 | console.info(`Checked out branch ${branch} at ${extensionPath}`); | 262 | console.info(`Checked out branch ${branch} at ${extensionPath}`); |
| 259 | 263 | ||
| 260 | return response.sendStatus(204); | 264 | return response.sendStatus(204); |
| @@ -319,7 +323,6 @@ router.post('/move', async (request, response) => { | |||
| 319 | * @returns {void} | 323 | * @returns {void} |
| 320 | */ | 324 | */ |
| 321 | router.post('/version', async (request, response) => { | 325 | router.post('/version', async (request, response) => { |
| 322 | const git = simpleGit(); | ||
| 323 | if (!request.body.extensionName) { | 326 | if (!request.body.extensionName) { |
| 324 | return response.status(400).send('Bad Request: extensionName is required in the request body.'); | 327 | return response.status(400).send('Bad Request: extensionName is required in the request body.'); |
| 325 | } | 328 | } |
| @@ -333,19 +336,20 @@ router.post('/version', async (request, response) => { | |||
| 333 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); | 336 | return response.status(404).send(`Directory does not exist at ${extensionPath}`); |
| 334 | } | 337 | } |
| 335 | 338 | ||
| 339 | const git = simpleGit({ baseDir: extensionPath }); | ||
| 336 | let currentCommitHash; | 340 | let currentCommitHash; |
| 337 | try { | 341 | try { |
| 338 | currentCommitHash = await git.cwd(extensionPath).revparse(['HEAD']); | 342 | currentCommitHash = await git.revparse(['HEAD']); |
| 339 | } catch (error) { | 343 | } catch (error) { |
| 340 | // it is not a git repo, or has no commits yet, or is a bare repo | 344 | // it is not a git repo, or has no commits yet, or is a bare repo |
| 341 | // not possible to update it, most likely can't get the branch name either | 345 | // not possible to update it, most likely can't get the branch name either |
| 342 | return response.send({ currentBranchName: '', currentCommitHash: '', isUpToDate: true, remoteUrl: '' }); | 346 | return response.send({ currentBranchName: '', currentCommitHash: '', isUpToDate: true, remoteUrl: '' }); |
| 343 | } | 347 | } |
| 344 | 348 | ||
| 345 | const currentBranch = await git.cwd(extensionPath).branch(); | 349 | const currentBranch = await git.branch(); |
| 346 | // get only the working branch | 350 | // get only the working branch |
| 347 | const currentBranchName = currentBranch.current; | 351 | const currentBranchName = currentBranch.current; |
| 348 | await git.cwd(extensionPath).fetch('origin'); | 352 | await git.fetch('origin'); |
| 349 | console.debug(extensionName, currentBranchName, currentCommitHash); | 353 | console.debug(extensionName, currentBranchName, currentCommitHash); |
| 350 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); | 354 | const { isUpToDate, remoteUrl } = await checkIfRepoIsUpToDate(extensionPath); |
| 351 | 355 | ||