From b46d1dc0902cfa59c3e863c7b7c7bc520aa63f13 Mon Sep 17 00:00:00 2001 From: Varun Sivapalan <5470233+sivapalan@users.noreply.github.com> Date: Fri, 29 Oct 2021 18:50:27 +0200 Subject: [PATCH 1/5] refactor: optimize clone and checkout in deploy command --- packages/docusaurus/src/commands/deploy.ts | 38 +++++++++------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 7c157b164f83..4f03871ba5bd 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -169,22 +169,24 @@ 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 ) { @@ -192,19 +194,10 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); `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); shellExecLog('git rm -rf .'); try { await fs.copy(fromPath, toPath); @@ -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 = From 92170650684ca0ffaaa216dc95130627678d0425 Mon Sep 17 00:00:00 2001 From: Varun Sivapalan <5470233+sivapalan@users.noreply.github.com> Date: Tue, 9 Nov 2021 22:41:21 +0100 Subject: [PATCH 2/5] refactor: remove obsolete check for default branch and simplify flow --- packages/docusaurus/src/commands/deploy.ts | 38 +++++++++------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 4f03871ba5bd..566af0edce6c 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -167,37 +167,31 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); const toPath = await fs.mkdtemp( path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), ); + + // Check out deployment branch when cloning repository. If the command fails, + // assume that the deployment branch doesn't exist, clone the default + // branch of the repository and then check out a new orphan branch. if ( shellExecLog( `git clone --depth 1 --branch ${deploymentBranch} ${remoteBranch} ${toPath}`, - ).code !== 0 + ).code === 0 + ) { + shell.cd(toPath); + } else if ( + shellExecLog(`git clone --depth 1 ${remoteBranch} ${toPath}`).code === 0 ) { + shell.cd(toPath); if ( - shellExecLog(`git clone --depth 1 ${remoteBranch} ${toPath}`).code !== 0 + shellExecLog(`git checkout --orphan ${deploymentBranch}`).code !== 0 ) { - throw new Error(`Running "git clone" command in "${toPath}" failed.`); - } - - 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 --orphan ${deploymentBranch}`).code !== 0 - ) { - throw new Error( - `Running "git checkout ${deploymentBranch}" command failed.`, - ); - } + throw new Error( + `Running "git checkout ${deploymentBranch}" command failed.`, + ); } + } else { + throw new Error(`Running "git clone" command in "${toPath}" failed.`); } - shell.cd(toPath); shellExecLog('git rm -rf .'); try { await fs.copy(fromPath, toPath); From 63334e29f1c3f55321ab6978e9ebe57b26061114 Mon Sep 17 00:00:00 2001 From: Varun Sivapalan <5470233+sivapalan@users.noreply.github.com> Date: Tue, 9 Nov 2021 22:42:39 +0100 Subject: [PATCH 3/5] refactor: skip cloning repository if deployment branch doesn't exist --- packages/docusaurus/src/commands/deploy.ts | 26 +++++++++------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index 566af0edce6c..d1cd8a154f09 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -168,31 +168,25 @@ Try using DEPLOYMENT_BRANCH=main or DEPLOYMENT_BRANCH=master`); path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), ); - // Check out deployment branch when cloning repository. If the command fails, - // assume that the deployment branch doesn't exist, clone the default - // branch of the repository and then check out a new orphan branch. + // Check out deployment branch when cloning repository, and then remove all the files in + // the directory. If the 'clone' command fails, assume that the deployment + // branch doesn't exist, and initialize git in an empty directory, check out a clean + // deployment branch and add remote. if ( shellExecLog( `git clone --depth 1 --branch ${deploymentBranch} ${remoteBranch} ${toPath}`, ).code === 0 ) { shell.cd(toPath); - } else if ( - shellExecLog(`git clone --depth 1 ${remoteBranch} ${toPath}`).code === 0 - ) { - shell.cd(toPath); - if ( - shellExecLog(`git checkout --orphan ${deploymentBranch}`).code !== 0 - ) { - throw new Error( - `Running "git checkout ${deploymentBranch}" command failed.`, - ); - } + shellExecLog('git rm -rf .'); } else { - throw new Error(`Running "git clone" command in "${toPath}" failed.`); + shell.mkdir(toPath); + shell.cd(toPath); + shellExecLog('git init'); + shellExecLog(`git checkout -b ${deploymentBranch}`); + shellExecLog(`git remote add origin ${remoteBranch}`); } - shellExecLog('git rm -rf .'); try { await fs.copy(fromPath, toPath); } catch (error) { From 405568d48b8a4565cf5be828e92f3d57f1604f05 Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Thu, 11 Nov 2021 09:14:51 +0800 Subject: [PATCH 4/5] Refactors --- packages/docusaurus/src/commands/deploy.ts | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index b5dbe1db2c98..cb1c00ad700e 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -179,9 +179,9 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com'; const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort; - let remoteBranch: string; + let deploymentRepoURL: string; if (useSSH) { - remoteBranch = buildSshUrl( + deploymentRepoURL = buildSshUrl( githubHost, organizationName, projectName, @@ -190,7 +190,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); } else { const gitPass = process.env.GIT_PASS; const gitCredentials = gitPass ? `${gitUser!}:${gitPass}` : gitUser!; - remoteBranch = buildHttpsUrl( + deploymentRepoURL = buildHttpsUrl( gitCredentials, githubHost, organizationName, @@ -200,7 +200,7 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); } console.log( - `${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`, + `${chalk.cyan('Remote repo URL:')} ${obfuscateGitPass(deploymentRepoURL)}`, ); // Check if this is a cross-repo publish. @@ -225,24 +225,22 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); const toPath = await fs.mkdtemp( path.join(os.tmpdir(), `${projectName}-${deploymentBranch}`), ); + shell.cd(toPath); - // Check out deployment branch when cloning repository, and then remove all the files in - // the directory. If the 'clone' command fails, assume that the deployment - // branch doesn't exist, and initialize git in an empty directory, check out a clean - // deployment branch and add remote. + // Check out deployment branch when cloning repository, and then remove all + // the files in the directory. If the 'clone' command fails, assume that + // the deployment branch doesn't exist, and initialize git in an empty + // directory, check out a clean deployment branch and add remote. if ( shellExecLog( - `git clone --depth 1 --branch ${deploymentBranch} ${remoteBranch} ${toPath}`, + `git clone --depth 1 --branch ${deploymentBranch} ${deploymentRepoURL} ${toPath}`, ).code === 0 ) { - shell.cd(toPath); shellExecLog('git rm -rf .'); } else { - shell.mkdir(toPath); - shell.cd(toPath); shellExecLog('git init'); shellExecLog(`git checkout -b ${deploymentBranch}`); - shellExecLog(`git remote add origin ${remoteBranch}`); + shellExecLog(`git remote add origin ${deploymentRepoURL}`); } try { From a2e29b02cb9f1e33b0aec5e580a4e8151af4773c Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Thu, 11 Nov 2021 09:21:31 +0800 Subject: [PATCH 5/5] More tip about failure --- packages/docusaurus/src/commands/deploy.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/src/commands/deploy.ts b/packages/docusaurus/src/commands/deploy.ts index cb1c00ad700e..e98f01bdec5e 100644 --- a/packages/docusaurus/src/commands/deploy.ts +++ b/packages/docusaurus/src/commands/deploy.ts @@ -259,7 +259,9 @@ You can also set the deploymentBranch property in docusaurus.config.js .`); if ( shellExecLog(`git push --force origin ${deploymentBranch}`).code !== 0 ) { - throw new Error('Running "git push" command failed.'); + throw new Error( + 'Running "git push" command failed. Does the GitHub user account you are using have push access to the repository?', + ); } else if (commitResults.code === 0) { // The commit might return a non-zero value when site is up to date. let websiteURL = '';