11---
22layout : full
33menu_item : guides
4- title : HTTP Clone Guide
4+ title : GitHub Two Factor Auth Guide
55description : How to clone with GitHub Two Factor Authorization
66---
77
@@ -10,8 +10,8 @@ first.**
1010
1111[ Return to cloning guides] ( ../ )
1212
13- HTTP/HTTPS clone
14- ----------------
13+ GitHub Two Factor Auth
14+ ----------------------
1515
1616This guide explains how to clone a repository, and in the case of failure,
1717attempt to open the existing path.
@@ -33,16 +33,35 @@ However, in your project you will most likely be using the following command:
3333var Git = require (" nodegit" );
3434```
3535
36+ ### GitHub Personal OAuth Token
37+
38+ Before you can clone a repository, you'll need a GitHub OAuth application
39+ token. You can find more information on generating one here:
40+
41+ [ Creating an access token for command-line use] (
42+ https://help.github.com/articles/creating-an-access-token-for-command-line-use/
43+ )
44+
45+ Once you have this token you'll assign it to a variable in your project, for
46+ this example, we'll call it ` GITHUB_TOKEN ` .
47+
48+ ``` javascript
49+ var GITHUB_TOKEN = " <GH_TOKEN>" ;
50+ ```
51+
52+ Keep this variable a secret. If you accidentally commit this key to a public
53+ GitHub repository they will immediately revoke it.
54+
3655### Clone URL
3756
3857The first argument to the ` clone ` method is a URL.
3958
40- In this example we're going to clone one of our test repositories from GitHub.
41- You could easily substitute this with any valid http or https Git repository
42- URL.
59+ In this example we're going to clone one of our private test repositories from
60+ GitHub. You could easily substitute this with any valid http or https Git
61+ repository URL.
4362
4463``` javascript
45- var cloneURL = " https://github.com/nodegit/test " ;
64+ var cloneURL = " https://github.com/nodegit/private " ;
4665```
4766
4867### Clone path
@@ -84,22 +103,38 @@ cloneOptions.remoteCallbacks = {
84103};
85104```
86105
87- ### Invoking the clone method
106+ #### GitHub credentials for Two Factor Auth
88107
89- The way NodeGit is structured is that all [ libgit2] ( http://libgit2.org ) C
90- methods are dynamically generated into C++. Since we're taking a
91- class-oriented approach, we make a top level class named ` Clone ` . This class
92- has a static method ` clone ` that we can use to bring down a repository.
108+ In order to authorize the clone operation, we'll need to respond to a low-level
109+ callback that expects credentials to be passed.
110+
111+ This function will be attached below, the above ` certificateCheck ` and will
112+ respond back with the OAuth token.
113+
114+ The ` remoteCallbacks ` object now looks like this:
115+
116+ ``` javascript
117+ cloneOptions .remoteCallbacks = {
118+ certificateCheck : function () { return 1 ; },
119+ credentials : function () {
120+ return Git .Cred .userpassPlaintextNew (GITHUB_TOKEN , " x-oauth-basic" );
121+ }
122+ };
123+ ```
124+
125+ ### Invoking the clone method
93126
94- While it may look a bit verbose, it is symptomatic of a rigid convention.
127+ You can easily invoke our top-level Clone as a function passing along the three
128+ aforementioned arguments.
95129
96130``` javascript
97131var cloneRepository = Git .Clone (cloneURL, localPath, cloneOptions);
98132```
99133
100- Notice how we store the return value from ` Clone.clone ` . This is a [ Promise] ( )
101- to represent the asynchronous operation. It offers finer control flow by
102- allowing us to capture errors and fallback if there is a clone failure.
134+ Notice how we store the return value from ` Git.Clone ` . This is a
135+ [ Promise] ( https://www.promisejs.org/ ) to represent the asynchronous operation.
136+ It offers finer control flow by allowing us to capture errors and fallback if
137+ there is a clone failure.
103138
104139### Handling clone failure
105140
0 commit comments