|
1 | | -// Github.js 0.2.0 |
| 1 | +// Github.js 0.3.0 |
2 | 2 | // (c) 2012 Michael Aufreiter, Development Seed |
3 | 3 | // Github.js is freely distributable under the MIT license. |
4 | 4 | // For all details and documentation: |
|
17 | 17 |
|
18 | 18 | function _request(method, path, data, cb) { |
19 | 19 | $.ajax({ |
20 | | - type: method, |
21 | | - url: API_URL + path, |
22 | | - data: JSON.stringify(data), |
23 | | - dataType: 'json', |
24 | | - contentType: 'application/x-www-form-urlencoded', |
25 | | - success: function(res) { cb(null, res); }, |
26 | | - error: function(err) { cb(err); }, |
27 | | - headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) } |
| 20 | + type: method, |
| 21 | + url: API_URL + path, |
| 22 | + data: JSON.stringify(data), |
| 23 | + dataType: 'json', |
| 24 | + contentType: 'application/x-www-form-urlencoded', |
| 25 | + success: function(res) { cb(null, res); }, |
| 26 | + error: function(err) { cb(err); }, |
| 27 | + headers : { Authorization : 'Basic ' + Base64.encode(username + ':' + password) } |
28 | 28 | }); |
29 | 29 | } |
30 | 30 |
|
|
36 | 36 | _request("GET", "/user/repos?type=all&per_page=100", null, function(err, res) { |
37 | 37 | cb(err, res); |
38 | 38 | }); |
39 | | - } |
| 39 | + }; |
40 | 40 | }; |
41 | 41 |
|
42 | 42 |
|
|
80 | 80 | }); |
81 | 81 | }; |
82 | 82 |
|
| 83 | + |
| 84 | + // For a given file path, get the corresponding sha (blob for files, tree for dirs) |
| 85 | + // TODO: So inefficient, it sucks hairy donkey balls. |
| 86 | + // ------- |
| 87 | + |
| 88 | + this.getSha = function(branch, path, cb) { |
| 89 | + that.getTree(branch+"?recursive=true", function(err, tree) { |
| 90 | + var file = _.select(tree, function(file) { |
| 91 | + return file.path === path; |
| 92 | + })[0]; |
| 93 | + cb(null, file ? file.sha : null); |
| 94 | + }); |
| 95 | + }; |
| 96 | + |
83 | 97 | // Retrieve the tree a commit points to |
84 | 98 | // ------- |
85 | 99 |
|
|
161 | 175 | // ------- |
162 | 176 |
|
163 | 177 | this.show = function(cb) { |
164 | | - _request("GET", repoPath, null, function(err, res) { |
165 | | - cb(); |
| 178 | + _request("GET", repoPath, null, function(err, info) { |
| 179 | + cb(null, info); |
166 | 180 | }); |
167 | 181 | }; |
168 | 182 |
|
169 | | - |
170 | 183 | // Read file at given path |
171 | 184 | // ------- |
172 | 185 |
|
173 | 186 | this.read = function(branch, path, cb) { |
174 | | - that.getTree(branch+"?recursive=true", function(err, tree) { |
175 | | - console.log(tree); |
176 | | - var file = _.select(tree, function(file) { |
177 | | - return file.path === path; |
178 | | - })[0]; |
179 | | - |
180 | | - if (!file) return cb("not found", null); |
| 187 | + that.getSha(branch, path, function(err, sha) { |
| 188 | + if (!sha) return cb("not found", null); |
181 | 189 |
|
182 | | - that.getBlob(file.sha, function(err, blob) { |
| 190 | + that.getBlob(sha, function(err, blob) { |
183 | 191 | function decode(blob) { |
184 | 192 | if (blob.content) { |
185 | 193 | var data = blob.encoding == 'base64' ? |
186 | 194 | atob(blob.content.replace(/\s/g, '')) : |
187 | 195 | blob.content; |
188 | | - |
189 | 196 | return data; |
190 | 197 | } else { |
191 | 198 | return ""; |
|
0 commit comments