Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c45743e

Browse files
committed
Added Repository#listBranches, and refined overall API.
1 parent 7c54291 commit c45743e

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

github.js

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.1.2
1+
// Github.js 0.2.0
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -7,16 +7,13 @@
77
(function() {
88
var Github;
99
var API_URL = 'https://api.github.com';
10-
11-
// Github API
12-
// -------
1310

1411
Github = window.Github = function(options) {
1512
var username = options.username;
1613
var password = options.password;
1714

1815
// Util
19-
// -------
16+
// =======
2017

2118
function _request(method, path, data, cb) {
2219
$.ajax({
@@ -32,56 +29,71 @@
3229
}
3330

3431
// USER API
35-
// -------
32+
// =======
3633

37-
Github.User = function(options) {
38-
this.username = options.username;
39-
var userPath = "/users/" + options.username;
34+
Github.User = function() {
4035
this.repos = function(cb) {
41-
_request("GET", userPath + "/repos?type=all", null, function(err, res) {
36+
_request("GET", "/user/repos?type=all", null, function(err, res) {
4237
cb(err, res);
4338
});
4439
}
4540
};
4641

4742

4843
// Repository API
49-
// -------
44+
// =======
5045

5146
Github.Repository = function(options) {
5247
var repo = options.name;
5348
var branch = options.branch;
49+
var user = options.user;
5450

5551
var that = this;
56-
var repoPath = "/repos/" + username + "/" + repo;
52+
var repoPath = "/repos/" + user + "/" + repo;
53+
54+
// Get a particular reference
55+
// -------
5756

58-
// Get latest commit from master
59-
function getLatestCommit(cb) {
60-
_request("GET", repoPath + "/git/refs/heads/" + branch, null, function(err, res) {
57+
this.getRef = function(ref, cb) {
58+
_request("GET", repoPath + "/git/refs/heads/" + ref, null, function(err, res) {
6159
if (err) return cb(err);
6260
cb(null, res.object.sha);
6361
});
64-
}
62+
};
63+
64+
// List all branches of a repository
65+
// -------
66+
67+
this.listBranches = function(cb) {
68+
_request("GET", repoPath + "/git/refs/heads", null, function(err, heads) {
69+
if (err) return cb(err);
70+
cb(null, _.map(heads, function(head) { return _.last(head.ref.split('/')); }));
71+
});
72+
};
6573

6674
// Retrieve the contents of a blob
67-
function getBlob(sha, cb) {
75+
// -------
76+
77+
this.getBlob = function(sha, cb) {
6878
_request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) {
6979
cb(err, res);
7080
});
71-
}
81+
};
7282

7383
// Retrieve the tree a commit points to
84+
// -------
7485

75-
function getTree(commit, cb) {
86+
this.getTree = function(commit, cb) {
7687
_request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
7788
if (err) return cb(err);
7889
cb(null, res.sha);
7990
});
80-
}
91+
};
8192

8293
// Post a new blob object, getting a blob SHA back
94+
// -------
8395

84-
function postBlob(content, cb) {
96+
this.postBlob = function(content, cb) {
8597
var data = {
8698
"content": content,
8799
"encoding": "utf-8"
@@ -90,12 +102,13 @@
90102
if (err) return cb(err);
91103
cb(null, res.sha);
92104
});
93-
}
105+
};
94106

95107
// Post a new tree object having a file path pointer replaced
96108
// with a new blob SHA getting a tree SHA back
109+
// -------
97110

98-
function postTree(baseTree, path, blob, cb) {
111+
this.postTree = function(baseTree, path, blob, cb) {
99112
var data = {
100113
"base_tree": baseTree,
101114
"tree": [
@@ -107,16 +120,17 @@
107120
}
108121
]
109122
};
110-
_request("POST", repoPath + "/git/trees", data, function(err, res) {
123+
_request("POST", repoPath + "/git/trees", data, function(err, res) {
111124
if (err) return cb(err);
112125
cb(null, res.sha);
113126
});
114127
};
115128

116129
// Create a new commit object with the current commit SHA as the parent
117130
// and the new tree SHA, getting a commit SHA back
131+
// -------
118132

119-
function createCommit(parent, tree, message, cb) {
133+
this.commit = function(parent, tree, message, cb) {
120134
var data = {
121135
"message": message,
122136
"author": {
@@ -132,17 +146,16 @@
132146
if (err) return cb(err);
133147
cb(null, res.sha);
134148
});
135-
}
149+
};
136150

137151
// Update the reference of your head to point to the new commit SHA
152+
// -------
138153

139-
function updateHead(commit, cb) {
140-
_request("PATCH", repoPath + "/git/refs/heads/" + branch, { "sha": commit }, function(err, res) {
154+
this.updateHead = function(head, commit, cb) {
155+
_request("PATCH", repoPath + "/git/refs/heads/" + head, { "sha": commit }, function(err, res) {
141156
cb(err);
142157
});
143-
}
144-
145-
158+
};
146159

147160
// Show repository information
148161
// -------
@@ -153,10 +166,10 @@
153166
});
154167
};
155168

156-
// List all files
169+
// List all files of a branch
157170
// -------
158171

159-
this.list = function(cb) {
172+
this.list = function(branch, cb) {
160173
_request("GET", repoPath + "/git/trees/" + branch + "?recursive=1", null, function(err, res) {
161174
cb(err, res ? res.tree : null);
162175
});
@@ -166,15 +179,15 @@
166179
// Read file at given path
167180
// -------
168181

169-
this.read = function(path, cb) {
170-
that.list(function(err, tree) {
182+
this.read = function(branch, path, cb) {
183+
that.list(branch, function(err, tree) {
171184
var file = _.select(tree, function(file) {
172185
return file.path === path;
173186
})[0];
174187

175188
if (!file) return cb("not found", null);
176189

177-
getBlob(file.sha, function(err, blob) {
190+
that.getBlob(file.sha, function(err, blob) {
178191
function decode(blob) {
179192
if (blob.content) {
180193
var data = blob.encoding == 'base64' ?
@@ -192,26 +205,16 @@
192205
});
193206
};
194207

195-
// List all commits
196-
// -------
197-
198-
this.list_commits = function (cb) {
199-
_request("GET", repoPath + "/commits", null, function(err, res) {
200-
if (err) return cb(err);
201-
cb(null, res);
202-
});
203-
};
204-
205-
// Write file contents on a given path
208+
// Write file contents to a given branch and path
206209
// -------
207210

208-
this.write = function(path, content, message, cb) {
209-
getLatestCommit(function(err, latestCommit) {
210-
getTree(latestCommit, function(err, tree) {
211-
postBlob(content, function(err, blob) {
212-
postTree(tree, path, blob, function(err, tree) {
213-
createCommit(latestCommit, tree, message, function(err, commit) {
214-
updateHead(commit, function(err) {
211+
this.write = function(branch, path, content, message, cb) {
212+
that.getRef(branch, function(err, latestCommit) {
213+
that.getTree(latestCommit, function(err, tree) {
214+
that.postBlob(content, function(err, blob) {
215+
that.postTree(tree, path, blob, function(err, tree) {
216+
that.commit(latestCommit, tree, message, function(err, commit) {
217+
that.updateHead(branch, commit, function(err) {
215218
cb(err);
216219
});
217220
});
@@ -225,12 +228,12 @@
225228
// Top Level API
226229
// -------
227230

228-
this.getRepo = function(repo, branch) {
229-
return new Github.Repository({name: repo, branch: branch || "master"});
231+
this.getRepo = function(user, repo, branch) {
232+
return new Github.Repository({user: user, name: repo, branch: branch || "master"});
230233
};
231234

232-
this.getUser = function(user) {
233-
return new Github.User({username: user});
235+
this.getUser = function() {
236+
return new Github.User();
234237
};
235238
};
236239
}).call(this);

0 commit comments

Comments
 (0)