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