Skip to content

Commit fee7818

Browse files
tbranyenJohn Haley
authored andcommitted
[skip-ci]
1 parent 6e0b412 commit fee7818

File tree

8 files changed

+208
-7
lines changed

8 files changed

+208
-7
lines changed

guides/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# Guides
1+
---
2+
layout: default
3+
menu_item: guides
4+
title: Guides
5+
description: Learning NodeGit
6+
---
27

38
## [Install](install/)
49
## [Cloning](cloning/)

guides/cloning/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
Cloning
2-
=======
1+
---
2+
layout: default
3+
menu_item: guides
4+
return_to:
5+
"All Guides": ../
6+
title: Cloning Guides
7+
description: How to clone repositories
8+
---
39

410
### [HTTP/HTTPS clone](http/)
511

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Require in NodeGit, since we want to use the local copy, we're using a
2+
// relative path. In your project, you will use:
3+
//
4+
// var NodeGit = require('nodegit');
5+
var Git = require('../../../');
6+
7+
// To clone with two factor auth enabled, you have to use a GitHub OAuth token
8+
// over HTTPS.
9+
var GITHUB_TOKEN = '<GH_TOKEN>';
10+
11+
// Using the `clone` method from the `Git.Clone` module, bring down the NodeGit
12+
// test repository from GitHub.
13+
var cloneURL = 'https://github.com/nodegit/private';
14+
15+
// Ensure that the `tmp` directory is local to this file and not the CWD.
16+
var localPath = require('path').join(__dirname, 'tmp');
17+
18+
// Simple object to store clone options.
19+
var cloneOptions = {};
20+
21+
// This is a required callback for OS X machines. There is a known issue
22+
// with libgit2 being able to verify certificates from GitHub.
23+
cloneOptions.remoteCallbacks = {
24+
certificateCheck: function() { return 1; },
25+
credentials: function() {
26+
return Git.Cred.userpassPlaintextNew(GITHUB_TOKEN, 'x-oauth-basic');
27+
}
28+
};
29+
30+
// Invoke the clone operation and store the returned Promise.
31+
var cloneRepository = Git.Clone.clone(cloneURL, localPath, cloneOptions);
32+
33+
// If the repository already exists, the clone above will fail. You can simply
34+
// open the repository in this case to continue execution.
35+
var errorAndAttemptOpen = function() {
36+
return Git.Repository.open(localPath);
37+
};
38+
39+
// Once the repository has been cloned or opened, you can work with a returned
40+
// `Git.Repository` instance.
41+
cloneRepository.catch(errorAndAttemptOpen)
42+
.then(function(repository) {
43+
// Access any repository methods here.
44+
console.log('Is the repository bare? %s', Boolean(repository.isBare()));
45+
})
46+
.catch(function(ex) {
47+
console.log(ex, ex.stack);
48+
});

guides/cloning/http/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
Cloning
2-
=======
1+
---
2+
layout: default
3+
menu_item: guides
4+
return_to:
5+
"Clone": ../
6+
"All Guides": ../../
7+
title: HTTP Clone Guide
8+
description: How to clone with HTTP
9+
---
310

411
**In order to run examples, you will need to [Install NodeGit](../../install)
512
first.**

guides/cloning/http/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ cloneOptions.remoteCallbacks = {
2121
};
2222

2323
// Invoke the clone operation and store the returned Promise.
24-
var cloneRepo = Git.Clone.clone(cloneURL, localPath, cloneOptions);
24+
var cloneRepository = Git.Clone.clone(cloneURL, localPath, cloneOptions);
2525

2626
// If the repository already exists, the clone above will fail. You can simply
2727
// open the repository in this case to continue execution.
2828
var errorAndAttemptOpen = function() {
29-
return Git.Repository.open(local);
29+
return Git.Repository.open(localPath);
3030
};
3131

3232
// Once the repository has been cloned or opened, you can work with a returned

guides/cloning/ssh-with-agent/README.md

Whitespace-only changes.

guides/install/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
---
2+
layout: default
3+
menu_item: guides
4+
return_to:
5+
"All Guides": /guides/
6+
title: Install Guide
7+
description: How to install NodeGit
8+
---
9+
110
Install
211
=======
312

0 commit comments

Comments
 (0)