Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize clone and checkout in deploy command #5829

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions packages/docusaurus/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,42 +169,35 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`);
);
if (
shellExecLog(
`git clone --depth 1 --no-single-branch ${remoteBranch} ${toPath}`,
`git clone --depth 1 --branch ${deploymentBranch} ${remoteBranch} ${toPath}`,
).code !== 0
) {
throw new Error(`Running "git clone" command in "${toPath}" failed.`);
}
if (
shellExecLog(`git clone --depth 1 ${remoteBranch} ${toPath}`).code !== 0
) {
throw new Error(`Running "git clone" command in "${toPath}" failed.`);
}

shell.cd(toPath);
shell.cd(toPath);

// If the default branch is the one we're deploying to, then we'll fail
// to create it. This is the case of a cross-repo publish, where we clone
// a github.io repo with a default branch.
const defaultBranch = shell
.exec('git rev-parse --abbrev-ref HEAD')
.stdout.trim();
if (defaultBranch !== deploymentBranch) {
if (shellExecLog(`git checkout origin/${deploymentBranch}`).code !== 0) {
// If the default branch is the one we're deploying to, then we'll fail
// to create it. This is the case of a cross-repo publish, where we clone
// a github.io repo with a default branch.
const defaultBranch = shell
.exec('git rev-parse --abbrev-ref HEAD')
.stdout.trim();
if (defaultBranch !== deploymentBranch) {
if (
shellExecLog(`git checkout --orphan ${deploymentBranch}`).code !== 0
) {
throw new Error(
`Running "git checkout ${deploymentBranch}" command failed.`,
);
}
} else if (
shellExecLog(`git checkout -b ${deploymentBranch}`).code +
shellExecLog(
`git branch --set-upstream-to=origin/${deploymentBranch}`,
).code !==
0
) {
throw new Error(
`Running "git checkout ${deploymentBranch}" command failed.`,
);
}
}

shell.cd(toPath);
sivapalan marked this conversation as resolved.
Show resolved Hide resolved
shellExecLog('git rm -rf .');
try {
await fs.copy(fromPath, toPath);
Expand All @@ -213,7 +206,6 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`);
`Copying build assets from "${fromPath}" to "${toPath}" failed with error "${error}".`,
);
}
shell.cd(toPath);
shellExecLog('git add --all');

const commitMessage =
Expand Down