|
52 | 52 | } |
53 | 53 | } |
54 | 54 | }; |
55 | | - xhr.setRequestHeader('Accept','application/vnd.github.raw'); |
| 55 | + xhr.setRequestHeader('Accept','application/json'); |
56 | 56 | xhr.setRequestHeader('Content-Type','application/json'); |
57 | 57 | if ( |
58 | 58 | (options.auth == 'oauth' && options.token) || |
|
253 | 253 | // ------- |
254 | 254 |
|
255 | 255 | this.getSha = function(branch, path, cb) { |
256 | | - // Just use head if path is empty |
257 | | - if (path === "") return that.getRef("heads/"+branch, cb); |
258 | | - that.getTree(branch+"?recursive=true", function(err, tree) { |
259 | | - var file = _.select(tree, function(file) { |
260 | | - return file.path === path; |
261 | | - })[0]; |
262 | | - cb(null, file ? file.sha : null); |
| 256 | + if (!path || path === "") return that.getRef("heads/"+branch, cb); |
| 257 | + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, pathContent) { |
| 258 | + if (err) return cb(err); |
| 259 | + cb(null, pathContent.sha); |
263 | 260 | }); |
264 | 261 | }; |
265 | 262 |
|
|
372 | 369 | // ------- |
373 | 370 |
|
374 | 371 | this.fork = function(cb) { |
375 | | - console.error(repoPath); |
376 | 372 | _request("POST", repoPath + "/forks", null, cb); |
377 | 373 | }; |
378 | 374 |
|
|
387 | 383 | // ------- |
388 | 384 |
|
389 | 385 | this.read = function(branch, path, cb) { |
390 | | - that.getSha(branch, path, function(err, sha) { |
391 | | - if (!sha) return cb("not found", null); |
392 | | - that.getBlob(sha, function(err, content) { |
393 | | - cb(err, content, sha); |
394 | | - }); |
| 386 | + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, obj) { |
| 387 | + if (err && err.error === 404) return cb("not found", null, null); |
| 388 | + |
| 389 | + if (err) return cb(err); |
| 390 | + var sha = obj.sha |
| 391 | + , content = Base64.decode(obj.content); |
| 392 | + |
| 393 | + cb(null, content, sha); |
395 | 394 | }); |
396 | 395 | }; |
397 | 396 |
|
|
438 | 437 |
|
439 | 438 | this.write = function(branch, path, content, message, cb) { |
440 | 439 | that.getSha(branch, path, function(err, sha) { |
441 | | - if (err) return cb(err); |
| 440 | + if (err && err.error!=404) return cb(err); |
442 | 441 | _request("PUT", repoPath + "/contents/" + path, { |
443 | 442 | message: message, |
444 | 443 | content: Base64.encode(content), |
|
0 commit comments