From a75499b3d82aaf698727d99bd3d318047581177c Mon Sep 17 00:00:00 2001 From: Tamimi Date: Fri, 22 Jul 2022 16:02:26 +0300 Subject: [PATCH 1/2] Closes #36: Enable cloning a repo from a specific commit --- gatsby-node.js | 16 ++++++++++++---- package.json | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index eedfc0e..d88a050 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -17,7 +17,7 @@ async function getTargetBranch(repo, branch) { } } -async function getRepo(path, remote, branch) { +async function getRepo(path, remote, branch, commit) { // If the directory doesn't exist or is empty, clone. This will be the case if // our config has changed because Gatsby trashes the cache dir automatically // in that case. @@ -27,10 +27,18 @@ async function getRepo(path, remote, branch) { opts.push(`--branch`, branch); } await Git().clone(remote, path, opts); + + if (typeof commit == `string`) { + const repo = await Git(path); + await repo + .fetch(['--unshallow']) + .then(() => repo.reset([`--hard`, commit])); + } + return Git(path); } else if (await isAlreadyCloned(remote, path)) { const repo = await Git(path); - const target = await getTargetBranch(repo, branch); + const target = typeof commit == `string` ? commit : await getTargetBranch(repo, branch); // Refresh our shallow clone with the latest commit. await repo .fetch([`--depth`, `1`]) @@ -49,7 +57,7 @@ exports.sourceNodes = async ( createContentDigest, reporter }, - { name, remote, branch, patterns = `**`, local } + { name, remote, branch, patterns = `**`, local, commit } ) => { const programDir = store.getState().program.directory; const localPath = local || require("path").join( @@ -62,7 +70,7 @@ exports.sourceNodes = async ( let repo; try { - repo = await getRepo(localPath, remote, branch); + repo = await getRepo(localPath, remote, branch, commit); } catch (e) { return reporter.error(e); } diff --git a/package.json b/package.json index bf7f942..1150b1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gatsby-source-git", - "version": "1.1.0", + "version": "1.2.0", "description": "Gatsby plugin for pulling files into Gatsby from Git repositories", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" From 7d98321a77ee09b897948d0e0877f325039f6b20 Mon Sep 17 00:00:00 2001 From: Tamimi Date: Fri, 22 Jul 2022 16:03:59 +0300 Subject: [PATCH 2/2] update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b593f91..db587d0 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Ideally we'd use [nodegit](https://github.com/nodegit/nodegit), but it doesn't s - `remote`: The url to clone from. - `branch` (optional): The branch or tag to use. If none supplied, we try to use the 'default' branch. +- `commit` (optional): The commit to checkout after cloning - `patterns` (optional): Passed to [fast-glob](https://github.com/mrmlnc/fast-glob) to determine which files get sucked into the graph.