Skip to content

Commit 9355827

Browse files
committed
Repository#getSha - for a given file path, return SHA.
1 parent 12cbeed commit 9355827

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

github.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.2.0
1+
// Github.js 0.3.0
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -17,14 +17,14 @@
1717

1818
function _request(method, path, data, cb) {
1919
$.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) }
2828
});
2929
}
3030

@@ -36,7 +36,7 @@
3636
_request("GET", "/user/repos?type=all&per_page=100", null, function(err, res) {
3737
cb(err, res);
3838
});
39-
}
39+
};
4040
};
4141

4242

@@ -80,6 +80,20 @@
8080
});
8181
};
8282

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+
8397
// Retrieve the tree a commit points to
8498
// -------
8599

@@ -161,31 +175,24 @@
161175
// -------
162176

163177
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);
166180
});
167181
};
168182

169-
170183
// Read file at given path
171184
// -------
172185

173186
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);
181189

182-
that.getBlob(file.sha, function(err, blob) {
190+
that.getBlob(sha, function(err, blob) {
183191
function decode(blob) {
184192
if (blob.content) {
185193
var data = blob.encoding == 'base64' ?
186194
atob(blob.content.replace(/\s/g, '')) :
187195
blob.content;
188-
189196
return data;
190197
} else {
191198
return "";

0 commit comments

Comments
 (0)