This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**In order to run examples, you will need to [Install NodeGit](../../install)
first.**
[Return to cloning guides](../)
SSH clone
----------------
This guide explains how to clone a repository, and in the case of failure,
attempt to open the existing path.
[View example source](index.js)
### Requiring NodeGit
In the guides directory, we like to keep our NodeGit relative to the project
root.
``` javascript
var Git = require("../../../");
```
However, in your project you will most likely be using the following command:
``` javascript
var Git = require("nodegit");
```
### Clone URL
The first argument to the `clone` method is a URL.
In this example we're going to clone one of our test repositories from GitHub.
You could easily substitute this with any valid http or https Git repository
URL.
``` javascript
var cloneURL = "https://github.com/nodegit/test";
```
### Clone path
The second argument to the `clone` method is a path.
Ideally your application will clone a repository into the same folder path
regardless of how or where you execute it from. Paths are relative to the
current working directory in NodeGit, so you will need to normalize it first.
This is very simple in Node:
``` javascript
var localPath = require("path").join(__dirname, "tmp");
```
Now this `tmp` directory will be created along side your script, no matter how
or where you execute it from.
### Clone options
The third argument to the `clone` method is an optional simple object.
``` javascript
var cloneOptions = {};
```
#### GitHub certificate issue in OS X
Unfortunately in OS X there is a problem where libgit2 is unable to look up
GitHub certificates correctly. In order to bypass this problem, we're going
to passthrough the certificate check.
*Note: this is not a problem with Windows or Linux*
``` javascript
cloneOptions.remoteCallbacks = {
certificateCheck: function() { return 1; }
};
```
### Invoking the clone method
You can easily invoke our top-level Clone as a function passing along the three
aforementioned arguments.
``` javascript
var cloneRepository = Git.Clone(cloneURL, localPath, cloneOptions);
```
Notice how we store the return value from `Git.Clone`. This is a
[Promise](https://www.promisejs.org/) to represent the asynchronous operation.
It offers finer control flow by allowing us to capture errors and fallback if
there is a clone failure.
### Handling clone failure
A naive way to handle a clone failure is to try opening the same path. Clones
will most commonly fail when the directory already exists. We can define
a function to attempt opening in this case.
``` javascript
var errorAndAttemptOpen = function() {
return Git.Repository.open(local);
};
```
This will be called as part of the Promise resolution in the final step.
### The Promise chain
Lastly in our clone operation, we'll assemble a Promise chain to handle errors
and work with the `Git.Repository` instance result.
``` javascript
cloneRepository.catch(errorAndAttemptOpen)
.then(function(repository) {
// Access any repository methods here.
console.log("Is the repository bare? %s", Boolean(repository.isBare()));
});
```
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Converting Examples to Guides #417
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
Uh oh!
There was an error while loading. Please reload this page.
Converting Examples to Guides #417
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing