Skip to content

Commit

Permalink
fix: reposervice fix cwd (#1004)
Browse files Browse the repository at this point in the history
* fix: reposervice fix cwd

* fix: cwd on git file sys svc
  • Loading branch information
harishv7 authored Nov 1, 2023
1 parent daa94bf commit 44663eb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
64 changes: 45 additions & 19 deletions src/services/db/GitFileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class GitFileSystemService {
? `${EFS_VOL_PATH_STAGING}/${repoName}`
: `${EFS_VOL_PATH_STAGING_LITE}/${repoName}`
return ResultAsync.fromPromise(
this.git.cwd(`${repoPath}`).checkIsRepo(),
this.git.cwd({ path: `${repoPath}`, root: false }).checkIsRepo(),
(error) => {
logger.error(
`Error when checking if ${repoName} is a Git repo: ${error}`
Expand All @@ -129,7 +129,9 @@ export default class GitFileSystemService {
? `${EFS_VOL_PATH_STAGING}/${repoName}`
: `${EFS_VOL_PATH_STAGING_LITE}/${repoName}`
return ResultAsync.fromPromise(
this.git.cwd(repoPath).remote(["get-url", "origin"]),
this.git
.cwd({ path: repoPath, root: false })
.remote(["get-url", "origin"]),
(error) => {
logger.error(`Error when checking origin remote URL: ${error}`)

Expand Down Expand Up @@ -196,7 +198,7 @@ export default class GitFileSystemService {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.revparse(["--abbrev-ref", "HEAD"]),
(error) => {
logger.error(`Error when getting current branch: ${error}`)
Expand All @@ -210,7 +212,9 @@ export default class GitFileSystemService {
).andThen((currentBranch) => {
if (currentBranch !== branchName) {
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).checkout(branchName),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.checkout(branchName),
(error) => {
logger.error(`Error when checking out ${branchName}: ${error}`)

Expand All @@ -237,7 +241,9 @@ export default class GitFileSystemService {
? EFS_VOL_PATH_STAGING
: EFS_VOL_PATH_STAGING_LITE
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).revparse([`HEAD:${filePath}`]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.revparse([`HEAD:${filePath}`]),
(error) => {
logger.error(
`Error when getting Git blob hash: ${error} when trying to access ${efsVolPath}/${repoName}`
Expand Down Expand Up @@ -294,7 +300,9 @@ export default class GitFileSystemService {
): ResultAsync<LogResult<DefaultLogFields>, GitFileSystemError> {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).log([branchName]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.log([branchName]),
(error) => {
logger.error(
`Error when getting latest commit of "${branchName}" branch: ${error}, when trying to access ${efsVolPath}/${repoName} for ${branchName}`
Expand Down Expand Up @@ -323,7 +331,7 @@ export default class GitFileSystemService {
const efsVolPath = this.getEfsVolPathFromBranch(branchName)
return ResultAsync.fromPromise(
this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.reset(["--hard", commitSha])
.clean(CleanOptions.FORCE + CleanOptions.RECURSIVE),
(error) => {
Expand Down Expand Up @@ -370,15 +378,15 @@ export default class GitFileSystemService {
const clonePromise = isStaging
? this.git
.clone(originUrl, `${efsVolPath}/${repoName}`)
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.checkout(branch)
: this.git
.clone(originUrl, `${efsVolPath}/${repoName}`, [
"--branch",
branch,
"--single-branch",
])
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })

return ResultAsync.fromPromise(clonePromise, (error) => {
logger.error(`Error when cloning ${repoName}: ${error}`)
Expand Down Expand Up @@ -440,7 +448,9 @@ export default class GitFileSystemService {

return this.ensureCorrectBranch(repoName, branchName).andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).pull(),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.pull(),
(error) => {
// Full error message 1: Your configuration specifies to merge
// with the ref 'refs/heads/staging' from the remote, but no
Expand Down Expand Up @@ -506,9 +516,11 @@ export default class GitFileSystemService {
ResultAsync.fromPromise(
isForce
? this.git
.cwd(`${efsVolPath}/${repoName}`)
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push([...gitOptions, "--force"])
: this.git.cwd(`${efsVolPath}/${repoName}`).push(gitOptions),
: this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(gitOptions),
(error) => {
logger.error(`Error when pushing ${repoName}: ${error}`)

Expand All @@ -526,8 +538,12 @@ export default class GitFileSystemService {
// Retry push once
ResultAsync.fromPromise(
isForce
? this.git.cwd(`${efsVolPath}/${repoName}`).push(["--force"])
: this.git.cwd(`${efsVolPath}/${repoName}`).push(),
? this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(["--force"])
: this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.push(),
(error) => {
logger.error(`Error when pushing ${repoName}: ${error}`)

Expand Down Expand Up @@ -592,7 +608,9 @@ export default class GitFileSystemService {
}

return ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).add(pathSpec),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.add(pathSpec),
(error) => {
logger.error(
`Error when Git adding files to ${repoName}: ${error}`
Expand All @@ -612,7 +630,9 @@ export default class GitFileSystemService {
})
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).commit(commitMessage),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.commit(commitMessage),
(error) => {
logger.error(`Error when committing ${repoName}: ${error}`)

Expand Down Expand Up @@ -1160,7 +1180,9 @@ export default class GitFileSystemService {
)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).mv(oldPath, newPath),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.mv(oldPath, newPath),
(error) => {
logger.error(`Error when moving ${oldPath} to ${newPath}: ${error}`)

Expand Down Expand Up @@ -1363,7 +1385,9 @@ export default class GitFileSystemService {
return this.ensureCorrectBranch(repoName, branchName)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).catFile(["-t", sha]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.catFile(["-t", sha]),
(error) => {
// An error is thrown if the SHA does not exist in the branch
if (error instanceof GitError) {
Expand All @@ -1376,7 +1400,9 @@ export default class GitFileSystemService {
)
.andThen(() =>
ResultAsync.fromPromise(
this.git.cwd(`${efsVolPath}/${repoName}`).reset(["--hard", sha]),
this.git
.cwd({ path: `${efsVolPath}/${repoName}`, root: false })
.reset(["--hard", sha]),
(error) => {
logger.error(`Error when updating repo state: ${error}`)

Expand Down
35 changes: 22 additions & 13 deletions src/services/identity/ReposService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,31 @@ export default class ReposService {

// 2. Commit changes in local repo
await this.simpleGit
.cwd(dir)
.cwd({ path: dir, root: false })
.checkout("staging") // ensure on staging branch
.add(".")
.addConfig("user.name", ISOMER_GITHUB_ORGANIZATION_NAME)
.addConfig("user.email", ISOMER_GITHUB_EMAIL)
.commit("Set URLs")

// 3. Push changes to staging branch
await this.simpleGit.cwd(dir).push("origin", "staging")
await this.simpleGit
.cwd({ path: dir, root: false })
.push("origin", "staging")

// 4. Merge these changes into master branch
await this.simpleGit.cwd(dir).checkout("master").merge(["staging"])
await this.simpleGit
.cwd({ path: dir, root: false })
.checkout("master")
.merge(["staging"])

// 5. Push changes to master branch
await this.simpleGit.cwd(dir).push("origin", "master")
await this.simpleGit
.cwd({ path: dir, root: false })
.push("origin", "master")

// 6. Checkout back to staging branch
await this.simpleGit.cwd(dir).checkout("staging")
await this.simpleGit.cwd({ path: dir, root: false }).checkout("staging")
}

private setUrlsInLocalConfig(
Expand Down Expand Up @@ -225,15 +232,15 @@ export default class ReposService {
// Clone base repo locally
fs.mkdirSync(stgDir)
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.clone(SITE_CREATION_BASE_REPO_URL, stgDir, ["-b", "staging"])

// Clear git
fs.rmSync(`${stgDir}/.git`, { recursive: true, force: true })

// Prepare git repo
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.init(["--initial-branch=staging"])
.checkoutLocalBranch("staging")

Expand All @@ -242,14 +249,14 @@ export default class ReposService {

// Commit
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.addConfig("user.name", "isomeradmin")
.addConfig("user.email", ISOMER_GITHUB_EMAIL)
.commit("Initial commit")

// Push to origin
await this.simpleGit
.cwd(stgDir)
.cwd({ path: stgDir, root: false })
.addRemote("origin", repoUrl)
.checkout("staging")
.push(["-u", "origin", "staging"]) // push to staging first to make it the default branch on GitHub
Expand Down Expand Up @@ -376,10 +383,12 @@ export const createRecords = (zoneId: string): Record[] => {
// note: for some reason, combining below commands led to race conditions
// so we have to do it separately
// Create staging lite branch in other repo path
await this.simpleGit.cwd(stgLiteDir).clone(repoUrl, stgLiteDir)
await this.simpleGit.cwd(stgLiteDir).pull() // some repos are large, clone takes time
await this.simpleGit
.cwd(stgLiteDir)
.cwd({ path: stgLiteDir, root: false })
.clone(repoUrl, stgLiteDir)
await this.simpleGit.cwd({ path: stgLiteDir, root: false }).pull() // some repos are large, clone takes time
await this.simpleGit
.cwd({ path: stgLiteDir, root: false })
.checkout("staging")
.rm(["-r", "images"])
.rm(["-r", "files"])
Expand All @@ -389,7 +398,7 @@ export const createRecords = (zoneId: string): Record[] => {

// Prepare git repo
await this.simpleGit
.cwd(stgLiteDir)
.cwd({ path: stgLiteDir, root: false })
.init()
.checkoutLocalBranch("staging-lite")
.add(".")
Expand Down

0 comments on commit 44663eb

Please sign in to comment.