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