Skip to content

Commit 1ad6f3b

Browse files
committed
better getSha and read functions
1 parent ea2095a commit 1ad6f3b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

github.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
}
5353
}
5454
};
55-
xhr.setRequestHeader('Accept','application/vnd.github.raw');
55+
xhr.setRequestHeader('Accept','application/json');
5656
xhr.setRequestHeader('Content-Type','application/json');
5757
if (
5858
(options.auth == 'oauth' && options.token) ||
@@ -253,13 +253,10 @@
253253
// -------
254254

255255
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);
263260
});
264261
};
265262

@@ -372,7 +369,6 @@
372369
// -------
373370

374371
this.fork = function(cb) {
375-
console.error(repoPath);
376372
_request("POST", repoPath + "/forks", null, cb);
377373
};
378374

@@ -387,11 +383,14 @@
387383
// -------
388384

389385
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);
395394
});
396395
};
397396

@@ -438,7 +437,7 @@
438437

439438
this.write = function(branch, path, content, message, cb) {
440439
that.getSha(branch, path, function(err, sha) {
441-
if (err) return cb(err);
440+
if (err && err.error!=404) return cb(err);
442441
_request("PUT", repoPath + "/contents/" + path, {
443442
message: message,
444443
content: Base64.encode(content),

0 commit comments

Comments
 (0)