|
100 | 100 | } |
101 | 101 |
|
102 | 102 |
|
103 | | - |
104 | 103 | // User API |
105 | 104 | // ======= |
106 | 105 |
|
|
196 | 195 | cb(err, res); |
197 | 196 | }); |
198 | 197 | }; |
199 | | - }; |
200 | 198 |
|
| 199 | + // Create a repo |
| 200 | + // ------- |
| 201 | + this.createRepo = function(options, cb) { |
| 202 | + _request("POST", "/user/repos", options, cb); |
| 203 | + }; |
| 204 | + |
| 205 | + }; |
201 | 206 |
|
202 | 207 | // Repository API |
203 | 208 | // ======= |
|
214 | 219 | "sha": null |
215 | 220 | }; |
216 | 221 |
|
| 222 | + |
| 223 | + // Delete a repo |
| 224 | + // -------- |
| 225 | + |
| 226 | + this.deleteRepo = function(cb) { |
| 227 | + _request("DELETE", repoPath, options, cb); |
| 228 | + }; |
| 229 | + |
217 | 230 | // Uses the cache if branch has not been changed |
218 | 231 | // ------- |
219 | 232 |
|
|
333 | 346 | // ------- |
334 | 347 |
|
335 | 348 | this.getSha = function(branch, path, cb) { |
336 | | - // Just use head if path is empty |
337 | | - if (path === "") return that.getRef("heads/"+branch, cb); |
338 | | - that.getTree(branch+"?recursive=true", function(err, tree) { |
| 349 | + if (!path || path === "") return that.getRef("heads/"+branch, cb); |
| 350 | + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, pathContent) { |
339 | 351 | if (err) return cb(err); |
340 | | - var file = _.select(tree, function(file) { |
341 | | - return file.path === path; |
342 | | - })[0]; |
343 | | - cb(null, file ? file.sha : null); |
| 352 | + cb(null, pathContent.sha); |
344 | 353 | }); |
345 | 354 | }; |
346 | 355 |
|
|
454 | 463 | // Get contents |
455 | 464 | // -------- |
456 | 465 |
|
457 | | - this.contents = function(branch, path, cb, sync) { |
458 | | - return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync); |
| 466 | + this.contents = function(ref, path, cb) { |
| 467 | + _request("GET", repoPath + "/contents/"+path, { ref: ref }, cb); |
459 | 468 | }; |
460 | 469 |
|
461 | 470 | // Fork repository |
|
529 | 538 | // ------- |
530 | 539 |
|
531 | 540 | this.read = function(branch, path, cb) { |
532 | | - that.getSha(branch, path, function(err, sha) { |
533 | | - if (!sha) return cb("not found", null); |
534 | | - that.getBlob(sha, function(err, content) { |
535 | | - cb(err, content, sha); |
536 | | - }); |
| 541 | + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, obj) { |
| 542 | + if (err && err.error === 404) return cb("not found", null, null); |
| 543 | + |
| 544 | + if (err) return cb(err); |
| 545 | + var sha = obj.sha, |
| 546 | + content = atob(obj.content); |
| 547 | + |
| 548 | + cb(null, content, sha); |
537 | 549 | }); |
538 | 550 | }; |
539 | 551 |
|
540 | | - // Remove a file from the tree |
| 552 | + |
| 553 | + // Remove a file |
541 | 554 | // ------- |
542 | 555 |
|
543 | 556 | this.remove = function(branch, path, cb) { |
544 | | - updateTree(branch, function(err, latestCommit) { |
545 | | - that.getTree(latestCommit+"?recursive=true", function(err, tree) { |
546 | | - // Update Tree |
547 | | - var newTree = _.reject(tree, function(ref) { return ref.path === path; }); |
548 | | - _.each(newTree, function(ref) { |
549 | | - if (ref.type === "tree") delete ref.sha; |
550 | | - }); |
551 | | - |
552 | | - that.postTree(newTree, function(err, rootTree) { |
553 | | - that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) { |
554 | | - that.updateHead(branch, commit, function(err) { |
555 | | - cb(err); |
556 | | - }); |
557 | | - }); |
558 | | - }); |
559 | | - }); |
| 557 | + that.getSha(branch, path, function(err, sha) { |
| 558 | + if (err) return cb(err); |
| 559 | + _request("DELETE", repoPath + "/contents/" + path, { |
| 560 | + message: path + " is removed", |
| 561 | + sha: sha, |
| 562 | + branch: branch |
| 563 | + }, cb); |
560 | 564 | }); |
561 | 565 | }; |
562 | 566 |
|
|
604 | 608 | // ------- |
605 | 609 |
|
606 | 610 | this.write = function(branch, path, content, message, cb) { |
607 | | - updateTree(branch, function(err, latestCommit) { |
608 | | - if (err) return cb(err); |
609 | | - that.postBlob(content, function(err, blob) { |
610 | | - if (err) return cb(err); |
611 | | - that.updateTree(latestCommit, path, blob, function(err, tree) { |
612 | | - if (err) return cb(err); |
613 | | - that.commit(latestCommit, tree, message, function(err, commit) { |
614 | | - if (err) return cb(err); |
615 | | - that.updateHead(branch, commit, cb); |
616 | | - }); |
617 | | - }); |
618 | | - }); |
| 611 | + that.getSha(branch, path, function(err, sha) { |
| 612 | + if (err && err.error!=404) return cb(err); |
| 613 | + _request("PUT", repoPath + "/contents/" + path, { |
| 614 | + message: message, |
| 615 | + content: btoa(content), |
| 616 | + branch: branch, |
| 617 | + sha: sha |
| 618 | + }, cb); |
619 | 619 | }); |
620 | 620 | }; |
621 | 621 |
|
|
0 commit comments