Skip to content

Commit 2d0d4d2

Browse files
committed
Use version in git clone
1 parent cade026 commit 2d0d4d2

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

templates/cli/lib/commands/init.js.twig

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,32 +363,46 @@ const initSite = async () => {
363363
const repo = `https://github.com/${templateDetails.providerOwner}/${templateDetails.providerRepositoryId}`;
364364
let selected = { template: templateDetails.frameworks[0].providerRootDirectory };
365365

366-
let gitInitCommands = '';
367-
let gitPullCommands = '';
366+
let gitCloneCommands = '';
368367

369368
const sparse = selected.template.startsWith('./') ? selected.template.substring(2) : selected.template;
370369

371370
log('Fetching site code ...');
372371

373372
if(selected.template === './') {
374-
gitInitCommands = `git clone --single-branch --depth 1 ${repo} .`;
373+
gitCloneCommands = `
374+
mkdir -p .
375+
cd .
376+
git init
377+
git remote add origin ${repo}
378+
git config --global init.defaultBranch main
379+
git fetch --depth=1 origin refs/tags/$(git ls-remote --tags origin "${templateDetails.providerVersion}" | tail -n 1 | awk -F '/' '{print $3}')
380+
git checkout FETCH_HEAD
381+
`.trim();
375382
} else {
376-
gitInitCommands = `git clone --single-branch --depth 1 --sparse ${repo} .`; // depth prevents fetching older commits reducing the amount fetched
377-
gitPullCommands = `git sparse-checkout add ${sparse}`;
383+
gitCloneCommands = `
384+
mkdir -p .
385+
cd .
386+
git init
387+
git remote add origin ${repo}
388+
git config --global init.defaultBranch main
389+
git config core.sparseCheckout true
390+
echo "${sparse}" >> .git/info/sparse-checkout
391+
git config --add remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
392+
git config remote.origin.tagopt --no-tags
393+
git fetch --depth=1 origin refs/tags/$(git ls-remote --tags origin "${templateDetails.providerVersion}" | tail -n 1 | awk -F '/' '{print $3}')
394+
git checkout FETCH_HEAD
395+
`.trim();
378396
}
379397

380398
/* Force use CMD as powershell does not support && */
381399
if (process.platform === 'win32') {
382-
gitInitCommands = 'cmd /c "' + gitInitCommands + '"';
383-
if(gitPullCommands)
384-
gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
400+
gitCloneCommands = 'cmd /c "' + gitCloneCommands + '"';
385401
}
386402

387403
/* Execute the child process but do not print any std output */
388404
try {
389-
childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: templatesDir });
390-
if(gitPullCommands)
391-
childProcess.execSync(gitPullCommands, { stdio: 'pipe', cwd: templatesDir });
405+
childProcess.execSync(gitCloneCommands, { stdio: 'pipe', cwd: templatesDir });
392406
} catch (error) {
393407
/* Specialised errors with recommended actions to take */
394408
if (error.message.includes('error: unknown option')) {

0 commit comments

Comments
 (0)