Reputation: 164139
I'm trying to clone a repo to the local file system and then checkout a specific commit.
This is what I have:
Git.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS).then((repo) => {
return repo.getCommit(version).then((commit) => {
// use the local tree
});
}).catch((error) => {
// handler clone failure
});
This clones the repo just fine, but the local version I end up with is the current head of the master and not the commit I checked out (version
).
How do I update the local tree to match this commit?
Thanks.
Upvotes: 0
Views: 169
Reputation: 938
If you want to checkout a random commit, you need to use the Checkout.tree(*)
function instead.
var repository;
NodeGit.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS)
.then(function(repo) {
repository = repo;
return repo.getCommit(version);
})
.then(function(commit) {
return NodeGit.Checkout.tree(repository, commit, checkoutStrategy:
git.Checkout.STRATEGY.FORCE
});
}).catch((error) => {
// handler clone failure
});
You may or may not also want to detach your HEAD
.
Edit:
This will checkout and set HEAD
to the commit in question.
var repository;
NodeGit.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS)
.then(function(repo) {
repository = repo;
return repo.getCommit(version);
})
.then(function(commit) {
repository.setHeadDetached(commit.id());
}).catch((error) => {
// handler clone failure
});
Upvotes: 1
Reputation: 164139
I haven't managed to make it work, and as it turned out to be way too complicated to get something as trivial to work, I've decided to just do this:
import * as process from "child_process";
const command = `git clone ${ GIT_REPO_URL } ${ repoPath } && cd ${ repoPath } && git checkout ${ version }`;
process.exec(command, error => {
...
});
Upvotes: 0
Reputation: 40804
getCommit
does not modify the local tree; it merely retrieves the commit in memory in the script, so that you can work with it without modifying the local tree. (This would, for example, be useful if you wanted to walk through the git history and do some analysis, but didn't need to actually update the local tree to access the files themselves.)
To actually check out a specific branch or commit, you want to use the checkoutBranch
and checkoutRef
functions, which actually do update the local working tree. (Notice how their descriptions explicitly say so, unlike getCommit
, which doesn't say it modifies the working three because it doesn't actally do that).
Upvotes: 3