|
1 |
| -// Github.js 0.5.1 |
| 1 | +// Github.js 0.5.2 |
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:
|
|
14 | 14 | // =======
|
15 | 15 |
|
16 | 16 | function headers() {
|
| 17 | + var headers = {} |
| 18 | + if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' }; |
| 19 | + if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' }; |
17 | 20 | return options.auth == 'oauth'
|
18 | 21 | ? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
|
19 |
| - : { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password) } |
| 22 | + : { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' } |
20 | 23 | }
|
21 | 24 |
|
22 | 25 | function _request(method, path, data, cb) {
|
|
66 | 69 | var that = this;
|
67 | 70 | var repoPath = "/repos/" + user + "/" + repo;
|
68 | 71 |
|
| 72 | + var currentTree = { |
| 73 | + "branch": null, |
| 74 | + "sha": null |
| 75 | + }; |
| 76 | + |
| 77 | + // Uses the cache if branch has not been changed |
| 78 | + // ------- |
| 79 | + |
| 80 | + function updateTree(branch, cb) { |
| 81 | + if (branch === currentTree.branch && currentTree.sha) return cb(null, currentTree.sha); |
| 82 | + that.getRef(branch, function(err, sha) { |
| 83 | + currentTree.branch = branch; |
| 84 | + currentTree.sha = sha; |
| 85 | + cb(err, sha); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
69 | 89 | // Get a particular reference
|
70 | 90 | // -------
|
71 | 91 |
|
|
90 | 110 | // -------
|
91 | 111 |
|
92 | 112 | this.getBlob = function(sha, cb) {
|
93 |
| - _raw_request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) { |
94 |
| - cb(err, res); |
95 |
| - }); |
| 113 | + _raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb); |
96 | 114 | };
|
97 | 115 |
|
98 | 116 | // For a given file path, get the corresponding sha (blob for files, tree for dirs)
|
99 | 117 | // TODO: So inefficient, it sucks hairy donkey balls.
|
100 | 118 | // -------
|
101 | 119 |
|
102 | 120 | this.getSha = function(branch, path, cb) {
|
| 121 | + // Just use head if path is empty |
| 122 | + if (path === "") return that.getRef(branch, cb); |
103 | 123 | that.getTree(branch+"?recursive=true", function(err, tree) {
|
104 | 124 | var file = _.select(tree, function(file) {
|
105 | 125 | return file.path === path;
|
|
181 | 201 | };
|
182 | 202 |
|
183 | 203 | _request("POST", repoPath + "/git/commits", data, function(err, res) {
|
| 204 | + currentTree.sha = res.sha; // update latest commit |
184 | 205 | if (err) return cb(err);
|
185 | 206 | cb(null, res.sha);
|
186 | 207 | });
|
|
210 | 231 | this.read = function(branch, path, cb) {
|
211 | 232 | that.getSha(branch, path, function(err, sha) {
|
212 | 233 | if (!sha) return cb("not found", null);
|
213 |
| - |
214 | 234 | that.getBlob(sha, cb);
|
215 | 235 | });
|
216 | 236 | };
|
|
219 | 239 | // -------
|
220 | 240 |
|
221 | 241 | this.remove = function(branch, path, cb) {
|
222 |
| - that.getRef(branch, function(err, latestCommit) { |
| 242 | + updateTree(branch, function(err, latestCommit) { |
223 | 243 | that.getTree(latestCommit+"?recursive=true", function(err, tree) {
|
224 | 244 | // Update Tree
|
225 | 245 | var newTree = _.reject(tree, function(ref) { return ref.path === path });
|
|
242 | 262 | // -------
|
243 | 263 |
|
244 | 264 | this.move = function(branch, path, newPath, cb) {
|
245 |
| - that.getRef(branch, function(err, latestCommit) { |
| 265 | + updateTree(branch, function(err, latestCommit) { |
246 | 266 | that.getTree(latestCommit+"?recursive=true", function(err, tree) {
|
247 | 267 | // Update Tree
|
248 | 268 | _.each(tree, function(ref) {
|
|
265 | 285 | // -------
|
266 | 286 |
|
267 | 287 | this.write = function(branch, path, content, message, cb) {
|
268 |
| - that.getRef(branch, function(err, latestCommit) { |
| 288 | + updateTree(branch, function(err, latestCommit) { |
269 | 289 | that.postBlob(content, function(err, blob) {
|
270 | 290 | that.updateTree(latestCommit, path, blob, function(err, tree) {
|
271 | 291 | that.commit(latestCommit, tree, message, function(err, commit) {
|
272 |
| - that.updateHead(branch, commit, function(err) { |
273 |
| - cb(err); |
274 |
| - }); |
| 292 | + that.updateHead(branch, commit, cb); |
275 | 293 | });
|
276 | 294 | });
|
277 | 295 | });
|
|
0 commit comments