Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NodeGit CloneOptions to clone a branch

I am trying to work out how to pass clone options to the nodegit clone method.

The node git documentation states that the 3rd param to the clone method is the clone options object http://www.nodegit.org/nodegit/#Repo-clone

git.Repo.clone(URL, path, CloneOptions, callback);

However this object is not included in the standard build of nodegit.

I have added the binding for the clone_options.cc file into the bindings.gyp file and I can get access to the clone options object. However I can not work out how to instantiate it with a valid branch name. The libgit2 api shows that the option is checkout_branch http://libgit2.github.com/libgit2/#HEAD/type/git_clone_options

Anyone have any insight on how to do this? Or on an alternative library that supports cloning of git branches in node?

var CloneOptions = nodegit.CloneOptions;
var options = new CloneOptions({checkout_branch: branchName});
git.Repo.clone(url, temp, options, function (err, repo) {...});

results in

Error: git_clone_options is required.

There is also an open thread on the github issues page for nodegit

https://github.com/nodegit/nodegit/issues/127

like image 572
Anthony McCormick Avatar asked Feb 26 '14 22:02

Anthony McCormick


People also ask

How do I clone a single branch in Git?

git clone -b <branchname> --single-branch <remote-repo-url> Here -b is just an alias for --branch This performs the same action as option one, except that the --single-branch option was introduced in Git version 1.7.10 and later. It allows you to only fetch files from the specified branch without fetching other branches.

How do I get all branches of a git repository?

git clone -b <branchname> <remote-repo-url>. Here -b is just an alias for --branch. With this, you fetch all the branches in the repository, checkout to the one you specified, and the specific branch becomes the configured local branch for git push and git pull . But you still fetched all files from each branch.

What happens when you clone a repository in Git?

While you can clone repositories with the git clone command, keep in mind that this clones the branch and the remote HEAD. This is usually master by default and includes all other branches in the repository. So when you clone a repository, you clone the master and all other branches.

How to clone a specific branch from the demo repository?

Now let's clone a specific branch from our demo repository. There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.


1 Answers

You can try this...

    var Git = require('nodegit');
    var clone = Git.Clone.clone;
    var branch = 'development';
    var cloneOptions = new Git.CloneOptions();    

    cloneOptions.checkoutBranch = branch;  
    clone(url, directory, cloneOptions)
        .then(function(repository){
            console.log(repository);
        });
like image 91
Leandro William Avatar answered Sep 28 '22 01:09

Leandro William