|
1 | 1 | --- |
2 | 2 | layout: full |
3 | 3 | menu_item: guides |
4 | | -title: Cloning Guides |
5 | | -description: How to clone repositories |
| 4 | +title: HTTP/HTTPS Guide |
| 5 | +description: How to clone with HTTP/HTTPS |
6 | 6 | --- |
7 | 7 |
|
8 | | -[Return to all guides](../) |
| 8 | +**In order to run examples, you will need to [Install NodeGit](../../install/basics) |
| 9 | +first.** |
| 10 | + |
| 11 | +[Return to cloning guides](../) |
9 | 12 |
|
10 | 13 | * * * |
11 | 14 |
|
12 | | -### [HTTP/HTTPS](http/) |
| 15 | +HTTP/HTTPS |
| 16 | +---------- |
| 17 | + |
| 18 | +This guide explains how to clone a repository, and in the case of failure, |
| 19 | +attempt to open the existing path. |
| 20 | + |
| 21 | +[View example source](index.js) |
| 22 | + |
| 23 | +### Requiring NodeGit |
| 24 | + |
| 25 | +In the guides directory, we like to keep our NodeGit relative to the project |
| 26 | +root. |
| 27 | + |
| 28 | +``` javascript |
| 29 | +var Git = require("../../../"); |
| 30 | +``` |
| 31 | + |
| 32 | +However, in your project you will most likely be using the following command: |
| 33 | + |
| 34 | +``` javascript |
| 35 | +var Git = require("nodegit"); |
| 36 | +``` |
| 37 | + |
| 38 | +### Clone URL |
| 39 | + |
| 40 | +The first argument to the `clone` method is a URL. |
| 41 | + |
| 42 | +In this example we're going to clone one of our test repositories from GitHub. |
| 43 | +You could easily substitute this with any valid http or https Git repository |
| 44 | +URL. |
| 45 | + |
| 46 | +``` javascript |
| 47 | +var cloneURL = "https://github.com/nodegit/test"; |
| 48 | +``` |
| 49 | + |
| 50 | +### Clone path |
| 51 | + |
| 52 | +The second argument to the `clone` method is a path. |
| 53 | + |
| 54 | +Ideally your application will clone a repository into the same folder path |
| 55 | +regardless of how or where you execute it from. Paths are relative to the |
| 56 | +current working directory in NodeGit, so you will need to normalize it first. |
| 57 | + |
| 58 | +This is very simple in Node: |
| 59 | + |
| 60 | +``` javascript |
| 61 | +var localPath = require("path").join(__dirname, "tmp"); |
| 62 | +``` |
| 63 | + |
| 64 | +Now this `tmp` directory will be created along side your script, no matter how |
| 65 | +or where you execute it from. |
| 66 | + |
| 67 | +### Clone options |
| 68 | + |
| 69 | +The third argument to the `clone` method is an optional simple object. |
| 70 | + |
| 71 | +``` javascript |
| 72 | +var cloneOptions = {}; |
| 73 | +``` |
| 74 | + |
| 75 | +**If you are using HTTP the OS X issue below does not affect you.** |
| 76 | + |
| 77 | +#### GitHub certificate issue in OS X |
| 78 | + |
| 79 | +Unfortunately in OS X there is a problem where libgit2 is unable to look up |
| 80 | +GitHub certificates correctly. In order to bypass this problem, we're going |
| 81 | +to passthrough the certificate check. |
| 82 | + |
| 83 | +*Note: this is not a problem with Windows or Linux* |
| 84 | + |
| 85 | +``` javascript |
| 86 | +cloneOptions.remoteCallbacks = { |
| 87 | + certificateCheck: function() { return 1; } |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +### Invoking the clone method |
| 92 | + |
| 93 | +You can easily invoke our top-level Clone as a function passing along the three |
| 94 | +aforementioned arguments. |
| 95 | + |
| 96 | +``` javascript |
| 97 | +var cloneRepository = Git.Clone(cloneURL, localPath, cloneOptions); |
| 98 | +``` |
13 | 99 |
|
14 | | - > Emulates `git clone https://github.com/nodegit/test` |
| 100 | +Notice how we store the return value from `Git.Clone`. This is a |
| 101 | +[Promise](https://www.promisejs.org/) to represent the asynchronous operation. |
| 102 | +It offers finer control flow by allowing us to capture errors and fallback if |
| 103 | +there is a clone failure. |
15 | 104 |
|
16 | | - For cloning HTTP or HTTPS repositories from servers like GitHub. |
| 105 | +### Handling clone failure |
17 | 106 |
|
18 | | -### [SSH w/ Agent](ssh-with-agent/) |
| 107 | +A naive way to handle a clone failure is to try opening the same path. Clones |
| 108 | +will most commonly fail when the directory already exists. We can define |
| 109 | +a function to attempt opening in this case. |
19 | 110 |
|
20 | | - > Emulates `git clone git@github.com:nodegit/test` |
| 111 | +``` javascript |
| 112 | +var errorAndAttemptOpen = function() { |
| 113 | + return Git.Repository.open(local); |
| 114 | +}; |
| 115 | +``` |
21 | 116 |
|
22 | | - For cloning SSH repositories using an SSH agent. |
| 117 | +This will be called as part of the Promise resolution in the final step. |
23 | 118 |
|
24 | | -### [GitHub Two Factor Auth](gh-two-factor/) |
| 119 | +### The Promise chain |
25 | 120 |
|
26 | | - > Emulates `git clone https://gh-token:-oauth-basic@github.com/nodegit/test` |
| 121 | +Lastly in our clone operation, we'll assemble a Promise chain to handle errors |
| 122 | +and work with the `Git.Repository` instance result. |
27 | 123 |
|
28 | | - For cloning repositories from GitHub when two-factor authorization is |
29 | | - enabled. |
| 124 | +``` javascript |
| 125 | +cloneRepository.catch(errorAndAttemptOpen) |
| 126 | + .then(function(repository) { |
| 127 | + // Access any repository methods here. |
| 128 | + console.log("Is the repository bare? %s", Boolean(repository.isBare())); |
| 129 | + }); |
| 130 | +``` |
0 commit comments