1- Cloning
2- =======
1+ ---
2+ layout : full
3+ menu_item : guides
4+ title : GitHub Two Factor Auth Guide
5+ description : How to clone with GitHub Two Factor Authorization
6+ ---
37
48** In order to run examples, you will need to [ Install NodeGit] ( ../../install )
59first.**
610
7- [ Return to cloning examples ] ( ../ )
11+ [ Return to cloning guides ] ( ../ )
812
9- HTTP/HTTPS clone
10- ----------------
13+ * * *
14+
15+ GitHub Two Factor Auth
16+ ----------------------
1117
1218This guide explains how to clone a repository, and in the case of failure,
1319attempt to open the existing path.
@@ -29,16 +35,35 @@ However, in your project you will most likely be using the following command:
2935var Git = require (" nodegit" );
3036```
3137
38+ ### GitHub Personal OAuth Token
39+
40+ Before you can clone a repository, you'll need a GitHub OAuth application
41+ token. You can find more information on generating one here:
42+
43+ [ Creating an access token for command-line use] (
44+ https://help.github.com/articles/creating-an-access-token-for-command-line-use/
45+ )
46+
47+ Once you have this token you'll assign it to a variable in your project, for
48+ this example, we'll call it ` GITHUB_TOKEN ` .
49+
50+ ``` javascript
51+ var GITHUB_TOKEN = " <GH_TOKEN>" ;
52+ ```
53+
54+ Keep this variable a secret. If you accidentally commit this key to a public
55+ GitHub repository they will immediately revoke it.
56+
3257### Clone URL
3358
3459The first argument to the ` clone ` method is a URL.
3560
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.
61+ In this example we're going to clone one of our private test repositories from
62+ GitHub. You could easily substitute this with any valid http or https Git
63+ repository URL.
3964
4065``` javascript
41- var cloneURL = " https://github.com/nodegit/test " ;
66+ var cloneURL = " https://github.com/nodegit/private " ;
4267```
4368
4469### Clone path
@@ -70,7 +95,7 @@ var cloneOptions = {};
7095
7196Unfortunately in OS X there is a problem where libgit2 is unable to look up
7297GitHub certificates correctly. In order to bypass this problem, we're going
73- to bypass the certificate check.
98+ to passthrough the certificate check.
7499
75100* Note: this is not a problem with Windows or Linux*
76101
@@ -80,22 +105,38 @@ cloneOptions.remoteCallbacks = {
80105};
81106```
82107
83- ### Invoking the clone method
108+ #### GitHub credentials for Two Factor Auth
84109
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.
110+ In order to authorize the clone operation, we'll need to respond to a low-level
111+ callback that expects credentials to be passed.
112+
113+ This function will be attached below the above ` certificateCheck ` , and will
114+ respond back with the OAuth token.
115+
116+ The ` remoteCallbacks ` object now looks like this:
117+
118+ ``` javascript
119+ cloneOptions .remoteCallbacks = {
120+ certificateCheck : function () { return 1 ; },
121+ credentials : function () {
122+ return Git .Cred .userpassPlaintextNew (GITHUB_TOKEN , " x-oauth-basic" );
123+ }
124+ };
125+ ```
126+
127+ ### Invoking the clone method
89128
90- While it may look a bit verbose, it is symptomatic of a rigid convention.
129+ You can easily invoke our top-level Clone as a function passing along the three
130+ aforementioned arguments.
91131
92132``` javascript
93133var cloneRepository = Git .Clone (cloneURL, localPath, cloneOptions);
94134```
95135
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.
136+ Notice how we store the return value from ` Git.Clone ` . This is a
137+ [ Promise] ( https://www.promisejs.org/ ) to represent the asynchronous operation.
138+ It offers finer control flow by allowing us to capture errors and fallback if
139+ there is a clone failure.
99140
100141### Handling clone failure
101142
0 commit comments