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

Skip to content

Commit 6a8aa85

Browse files
committed
Fixed various glitches.
1 parent 96e6e8e commit 6a8aa85

File tree

1 file changed

+132
-91
lines changed

1 file changed

+132
-91
lines changed

github.js

Lines changed: 132 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.1.0
1+
// Github.js 0.1.1
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -18,10 +18,10 @@
1818
// Util
1919
// -------
2020

21-
function _request(method, route, data, cb) {
21+
function _request(method, path, data, cb) {
2222
$.ajax({
2323
type: method,
24-
url: API_URL + route,
24+
url: API_URL + path,
2525
data: JSON.stringify(data),
2626
dataType: 'json',
2727
contentType: 'application/x-www-form-urlencoded',
@@ -38,123 +38,164 @@
3838
this.username = options.username;
3939
var userPath = "/users/" + options.username;
4040
this.repos = function(cb) {
41-
_request("GET", userPath + "/repos", null, function(err, res) {
41+
_request("GET", userPath + "/repos?type=all", null, function(err, res) {
4242
cb(err, res);
4343
});
4444
}
4545
};
4646

47+
4748
// Repository API
4849
// -------
4950

5051
Github.Repository = function(options) {
52+
var repo = options.name;
53+
var branch = options.branch;
54+
55+
var that = this;
56+
var repoPath = "/repos/" + username + "/" + repo;
57+
58+
// Get latest commit from master
59+
function getLatestCommit(cb) {
60+
_request("GET", repoPath + "/git/refs/heads/" + branch, null, function(err, res) {
61+
if (err) return cb(err);
62+
cb(null, res.object.sha);
63+
});
64+
}
65+
66+
// Retrieve the contents of a blob
67+
function getBlob(sha, cb) {
68+
_request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) {
69+
cb(err, res);
70+
});
71+
}
72+
73+
// Retrieve the tree a commit points to
74+
75+
function getTree(commit, cb) {
76+
_request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
77+
if (err) return cb(err);
78+
cb(null, res.sha);
79+
});
80+
}
81+
82+
// Post a new blob object, getting a blob SHA back
83+
84+
function postBlob(content, cb) {
85+
var data = {
86+
"content": content,
87+
"encoding": "utf-8"
88+
};
89+
_request("POST", repoPath + "/git/blobs", data, function(err, res) {
90+
if (err) return cb(err);
91+
cb(null, res.sha);
92+
});
93+
}
94+
95+
// Post a new tree object having a file path pointer replaced
96+
// with a new blob SHA getting a tree SHA back
97+
98+
function postTree(baseTree, path, blob, cb) {
99+
var data = {
100+
"base_tree": baseTree,
101+
"tree": [
102+
{
103+
"path": path,
104+
"mode": "100644",
105+
"type": "blob",
106+
"sha": blob
107+
}
108+
]
109+
};
110+
_request("POST", repoPath + "/git/trees", data, function(err, res) {
111+
if (err) return cb(err);
112+
cb(null, res.sha);
113+
});
114+
};
115+
116+
// Create a new commit object with the current commit SHA as the parent
117+
// and the new tree SHA, getting a commit SHA back
118+
119+
function createCommit(parent, tree, cb) {
120+
var data = {
121+
"message": "Spooky. Isn't it?",
122+
"author": {
123+
"name": "Ghost"
124+
},
125+
"parents": [
126+
parent
127+
],
128+
"tree": tree
129+
};
130+
131+
_request("POST", repoPath + "/git/commits", data, function(err, res) {
132+
if (err) return cb(err);
133+
cb(null, res.sha);
134+
});
135+
}
136+
137+
// Update the reference of your head to point to the new commit SHA
138+
139+
function updateHead(commit, cb) {
140+
_request("PATCH", repoPath + "/git/refs/heads/" + branch, { "sha": commit }, function(err, res) {
141+
cb(err);
142+
});
143+
}
144+
145+
51146

52147
// Show repository information
53148
// -------
54149

55-
var repoPath = "/repos/" + username + "/" + options.name;
56-
57150
this.show = function(cb) {
58151
_request("GET", repoPath, null, function(err, res) {
59152
cb();
60153
});
61154
};
62155

156+
// List all files
157+
// -------
158+
159+
this.list = function(cb) {
160+
_request("GET", repoPath + "/git/trees/" + branch + "?recursive=1", null, function(err, res) {
161+
cb(err, res ? res.tree : null);
162+
});
163+
};
164+
63165

64166
// Read file at given path
65167
// -------
66168

67169
this.read = function(path, cb) {
170+
that.list(function(err, tree) {
171+
var file = _.select(tree, function(file) {
172+
return file.path === path;
173+
})[0];
174+
175+
if (!file) return cb("not found", null);
176+
177+
getBlob(file.sha, function(err, blob) {
178+
function decode(blob) {
179+
if (blob.content) {
180+
var data = blob.encoding == 'base64' ?
181+
atob(blob.content.replace(/\s/g, '')) :
182+
blob.content;
183+
184+
return data;
185+
} else {
186+
return "";
187+
}
188+
}
68189

190+
cb(null, decode(blob));
191+
});
192+
});
69193
};
70194

71195
// Write file contents on a given path
72196
// -------
73197

74198
this.write = function(path, content, cb) {
75-
76-
// Get latest commit from master
77-
function getLatestCommit(cb) {
78-
_request("GET", repoPath + "/git/refs/heads/master", null, function(err, res) {
79-
if (err) return cb(err);
80-
cb(null, res.object.sha);
81-
});
82-
}
83-
84-
// Retrieve the tree a commit points to
85-
86-
function getTree(commit, cb) {
87-
_request("GET", repoPath + "/git/trees/"+commit, null, function(err, res) {
88-
if (err) return cb(err);
89-
cb(null, res.sha);
90-
});
91-
}
92-
93-
// Post a new blob object, getting a blob SHA back
94-
95-
function postBlob(content, cb) {
96-
var data = {
97-
"content": content,
98-
"encoding": "utf-8"
99-
};
100-
_request("POST", repoPath + "/git/blobs", data, function(err, res) {
101-
if (err) return cb(err);
102-
cb(null, res.sha);
103-
});
104-
}
105-
106-
// Post a new tree object having a file path pointer replaced
107-
// with a new blob SHA getting a tree SHA back
108-
109-
function postTree(baseTree, path, blob, cb) {
110-
var data = {
111-
"base_tree": baseTree,
112-
"tree": [
113-
{
114-
"path": path,
115-
"mode": "100644",
116-
"type": "blob",
117-
"sha": blob
118-
}
119-
]
120-
};
121-
_request("POST", repoPath + "/git/trees", data, function(err, res) {
122-
if (err) return cb(err);
123-
cb(null, res.sha);
124-
});
125-
};
126-
127-
// Create a new commit object with the current commit SHA as the parent
128-
// and the new tree SHA, getting a commit SHA back
129-
130-
function createCommit(parent, tree, cb) {
131-
var data = {
132-
"message": "Spooky. Isn't it?",
133-
"author": {
134-
"name": "Ghost"
135-
},
136-
"parents": [
137-
parent
138-
],
139-
"tree": tree
140-
};
141-
142-
_request("POST", repoPath + "/git/commits", data, function(err, res) {
143-
if (err) return cb(err);
144-
cb(null, res.sha);
145-
});
146-
}
147-
148-
// Update the reference of your head to point to the new commit SHA
149-
150-
function updateHead(commit, cb) {
151-
_request("PATCH", repoPath + "/git/refs/heads/master", { "sha": commit }, function(err, res) {
152-
cb(err);
153-
});
154-
}
155-
156-
// Write it.
157-
158199
getLatestCommit(function(err, latestCommit) {
159200
getTree(latestCommit, function(err, tree) {
160201
postBlob(content, function(err, blob) {
@@ -174,8 +215,8 @@
174215
// Top Level API
175216
// -------
176217

177-
this.getRepo = function(repo) {
178-
return new Github.Repository({name: repo});
218+
this.getRepo = function(repo, branch) {
219+
return new Github.Repository({name: repo, branch: branch || "master"});
179220
};
180221

181222
this.getUser = function(user) {

0 commit comments

Comments
 (0)