Skip to content

Commit

Permalink
Closes stevetweeddale#36: Enable cloning a repo from a specific commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TamimiGitHub committed Jul 22, 2022
1 parent a7fee1a commit a75499b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`])
Expand All @@ -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(
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit a75499b

Please sign in to comment.