Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #36: Enable cloning a repo from a specific commit #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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