From a224f17e878b3b1afc66e406439e2b59090fd5f1 Mon Sep 17 00:00:00 2001 From: Rasso Hilber Date: Tue, 5 Aug 2025 09:17:40 +0200 Subject: [PATCH 1/5] Fix typo --- eleventy.config.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eleventy.config.cjs b/eleventy.config.cjs index 1e27726a..88b93ab5 100644 --- a/eleventy.config.cjs +++ b/eleventy.config.cjs @@ -211,7 +211,7 @@ async function loadGitHubRepoFile(repoLink, filePath, type = 'text') { try { return await EleventyFetch(repoURL.toLowerCase(), { duration: '60s', type }); } catch (error) { - console.error(`Erro loading ${filePath} from ${repoURL}: ${error.message}`); + console.error(`Error loading ${filePath} from ${repoURL}: ${error.message}`); return null; } } From 5cdfb61a95fe105c1050dccd0f62dfcf2c5bdfaf Mon Sep 17 00:00:00 2001 From: Rasso Hilber Date: Tue, 5 Aug 2025 09:54:07 +0200 Subject: [PATCH 2/5] Detect the default branch automatically when loading GitHub repo files Not using the GitHub API here to get around rate limiting. Instead resorting to an on-the-fly `git clone` + `git remote show origin` --- eleventy.config.cjs | 10 +++++-- lib/helpers.js | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 lib/helpers.js diff --git a/eleventy.config.cjs b/eleventy.config.cjs index 88b93ab5..14ddaba0 100644 --- a/eleventy.config.cjs +++ b/eleventy.config.cjs @@ -16,6 +16,7 @@ const { prepareInfoBlocks, prepareVideos } = require('./lib/eleventy-transforms'); +const { getDefaultBranch } = require('./lib/helpers'); const customMarkdownIt = markdownIt({ html: true, @@ -202,8 +203,13 @@ async function maybeLoadPackageInfo(ctx) { * @param {string} type */ async function loadGitHubRepoFile(repoLink, filePath, type = 'text') { - const repoBase = repoLink.replace('github.com', 'raw.githubusercontent.com'); - const repoURL = `${repoBase}/master/${filePath}`; + /** Remove the trailing slash */ + repoLink = repoLink.replace(/\/$/, ''); + + const defaultBranch = await getDefaultBranch(repoLink); + + const rawBase = repoLink.replace('//github.com', '//raw.githubusercontent.com'); + const repoURL = `${rawBase}/${defaultBranch}/${filePath}`; try { return await EleventyFetch(repoURL, { duration: '60s', type }); diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 00000000..bed54f9c --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,71 @@ +// @ts-check + +const { execSync } = require('child_process'); +const { mkdtempSync, rmSync } = require('fs'); +const { tmpdir } = require('os'); +const { join } = require('path'); + +/** + * Log and exit + */ +function dd(...args) { + console.log(...args); + process.exit(); +} + +/** + * A cache for default branches + * @type {Map} + */ +const defaultBranches = new Map(); + +/** + * Detect the default branch of a GitHub repo by cloning it + * + * @param {string} repoURL - HTTPS clone URL of the repo (e.g. https://github.com/user/repo.git) + * @return {Promise} - Default branch name + */ +function getDefaultBranch(repoURL) { + repoURL = repoURL.replace(/\/$/, '') + '.git'; + + return new Promise((resolve) => { + const cached = defaultBranches.get(repoURL); + if (!!cached) { + resolve(cached); + return; + } + + const tmpDir = mkdtempSync(join(tmpdir(), 'repo-')); + + try { + // Bare, shallow clone for speed + execSync(`git clone --bare --depth=1 ${repoURL} .`, { + cwd: tmpDir, + stdio: 'ignore' + }); + + const output = execSync(`git remote show origin`, { + cwd: tmpDir + }).toString(); + + const match = output.match(/HEAD branch: (.+)/); + if (match) { + defaultBranches.set(repoURL, match[1].trim()); + resolve(match[1].trim()); + return; + } + } catch (error) { + console.error(`Error detecting default branch for ${repoURL}: ${error.message}`); + } finally { + rmSync(tmpDir, { recursive: true, force: true }); + } + defaultBranches.set(repoURL, 'master'); + resolve('master'); + }); +} + + +module.exports = { + dd, + getDefaultBranch, +}; From 8af618823bbae89d5107d5e070b0db1e35161c56 Mon Sep 17 00:00:00 2001 From: Rasso Hilber Date: Tue, 5 Aug 2025 09:57:42 +0200 Subject: [PATCH 3/5] Prepare for renaming of `master` to `main` --- .github/workflows/deploy.yml | 2 +- .github/workflows/test.yml | 4 ++-- src/_includes/page.njk | 4 ++-- src/docs/getting-started/getting-started.md | 2 +- src/docs/getting-started/showcase.md | 2 +- src/docs/other/changelog.md | 2 +- src/docs/other/ci-cd.md | 2 +- src/showcase/README.md | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0c50ae1d..1e7b29ad 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,7 +2,7 @@ name: Deploy to GH pages on: push: - branches: [ "master" ] + branches: [ "main" ] repository_dispatch: types: [ "redeploy_docs" ] workflow_dispatch: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4f496f8f..cfa9e3b6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: tests on: push: - branches: ["master", "next"] + branches: ["main", "next"] pull_request: - branches: ["master", "next"] + branches: ["main", "next"] workflow_dispatch: jobs: diff --git a/src/_includes/page.njk b/src/_includes/page.njk index 18385c92..acdd9e16 100644 --- a/src/_includes/page.njk +++ b/src/_includes/page.njk @@ -21,9 +21,9 @@

{% feather "edit" %} {% if repo_link %} - Suggest changes to this page + Suggest changes to this page {% else %} - Suggest changes to this page + Suggest changes to this page {% endif %}

diff --git a/src/docs/getting-started/getting-started.md b/src/docs/getting-started/getting-started.md index 80b01200..134af9ed 100644 --- a/src/docs/getting-started/getting-started.md +++ b/src/docs/getting-started/getting-started.md @@ -130,4 +130,4 @@ Become a sponsor on [Open Collective](https://opencollective.com/swup) or suppor ## License -Swup is released under the [MIT license](https://github.com/swup/swup/blob/master/LICENSE). +Swup is released under the [MIT license](https://github.com/swup/swup/blob/main/LICENSE). diff --git a/src/docs/getting-started/showcase.md b/src/docs/getting-started/showcase.md index b7cd867c..615a6ea8 100644 --- a/src/docs/getting-started/showcase.md +++ b/src/docs/getting-started/showcase.md @@ -14,4 +14,4 @@ permalink: /getting-started/showcase/ A small selection of sites using swup to enhance their navigation experience. Want to share a site you've built? -[Submit it to the showcase](https://github.com/swup/docs/blob/master/src/showcase/README.md). +[Submit it to the showcase](https://github.com/swup/docs/blob/main/src/showcase/README.md). diff --git a/src/docs/other/changelog.md b/src/docs/other/changelog.md index 5b355e98..ed4dccd7 100644 --- a/src/docs/other/changelog.md +++ b/src/docs/other/changelog.md @@ -5,6 +5,6 @@ eleventyNavigation: key: Changelog parent: Other order: 5 - url: https://github.com/swup/swup/blob/master/CHANGELOG.md + url: https://github.com/swup/swup/blob/main/CHANGELOG.md permalink: false --- diff --git a/src/docs/other/ci-cd.md b/src/docs/other/ci-cd.md index 5c168101..f0c3af1d 100644 --- a/src/docs/other/ci-cd.md +++ b/src/docs/other/ci-cd.md @@ -16,7 +16,7 @@ For this you can use [swup cli](/cli/), which now has a `validate` command with Note that `validate` command runs test against a live site, while checking the pages in a headless browser. This means the site needs to be running on some domain, or can be temporarily started just for checks. -You can use this docs site as an example, which is using a [CircleCI pipeline](https://github.com/swup/docs/blob/master/.circleci/config.yml) with [http-server package](https://github.com/swup/docs/blob/master/package.json#L20) to start a server and validate site on each PR. +You can use this docs site as an example, which is using a [CircleCI pipeline](https://github.com/swup/docs/blob/main/.circleci/config.yml) with [http-server package](https://github.com/swup/docs/blob/main/package.json#L20) to start a server and validate site on each PR. Instead of using the [command line options](/cli/), you can also define the options in a `swup.config.js` file in the root of your project. This file needs to default export an object, similar to the one below. diff --git a/src/showcase/README.md b/src/showcase/README.md index 261f3b38..eebc3480 100644 --- a/src/showcase/README.md +++ b/src/showcase/README.md @@ -17,8 +17,8 @@ Adding your site to the Swup Showcase is straightforward. - This will copy the repository to your account, allowing you to make changes. 2. **Prepare Your Showcase File**: - - Navigate to the [`showcase/`](https://github.com/swup/docs/tree/master/src/showcase) directory. - - Make a copy of the [`_showcase.md`](https://github.com/swup/docs/tree/master/src/showcase/_showcase.md) example file. + - Navigate to the [`showcase/`](https://github.com/swup/docs/tree/main/src/showcase) directory. + - Make a copy of the [`_showcase.md`](https://github.com/swup/docs/tree/main/src/showcase/_showcase.md) example file. - Rename your copy to something descriptive of your site (e.g., `your-site-name.md`). - Edit the file to include information about your website, following the template. From b94ba914ad9ec72833204272d1650e0fdd53b707 Mon Sep 17 00:00:00 2001 From: Rasso Hilber Date: Tue, 5 Aug 2025 10:00:18 +0200 Subject: [PATCH 4/5] Log the default branches of remote repos during build --- eleventy.config.cjs | 1 + 1 file changed, 1 insertion(+) diff --git a/eleventy.config.cjs b/eleventy.config.cjs index 14ddaba0..0b1d8fb5 100644 --- a/eleventy.config.cjs +++ b/eleventy.config.cjs @@ -207,6 +207,7 @@ async function loadGitHubRepoFile(repoLink, filePath, type = 'text') { repoLink = repoLink.replace(/\/$/, ''); const defaultBranch = await getDefaultBranch(repoLink); + console.log({ repoLink, defaultBranch }); const rawBase = repoLink.replace('//github.com', '//raw.githubusercontent.com'); const repoURL = `${rawBase}/${defaultBranch}/${filePath}`; From ba7bb7dccba17bd1f38c66cf425d3ca242784c71 Mon Sep 17 00:00:00 2001 From: Rasso Hilber Date: Tue, 5 Aug 2025 10:26:46 +0200 Subject: [PATCH 5/5] Run tests on any pull request --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cfa9e3b6..55bf9723 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,6 @@ on: push: branches: ["main", "next"] pull_request: - branches: ["main", "next"] workflow_dispatch: jobs: