|
1 | | -// Github.js 0.3.0 |
| 1 | +// Github.js 0.4.0 |
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: |
|
45 | 45 |
|
46 | 46 | Github.Repository = function(options) { |
47 | 47 | var repo = options.name; |
48 | | - var branch = options.branch; |
49 | 48 | var user = options.user; |
50 | 49 |
|
51 | 50 | var that = this; |
|
80 | 79 | }); |
81 | 80 | }; |
82 | 81 |
|
83 | | - |
84 | 82 | // For a given file path, get the corresponding sha (blob for files, tree for dirs) |
85 | 83 | // TODO: So inefficient, it sucks hairy donkey balls. |
86 | 84 | // ------- |
|
118 | 116 | }); |
119 | 117 | }; |
120 | 118 |
|
121 | | - // Post a new tree object having a file path pointer replaced |
122 | | - // with a new blob SHA getting a tree SHA back |
| 119 | + // Update an existing tree adding a new blob object getting a tree SHA back |
123 | 120 | // ------- |
124 | 121 |
|
125 | | - this.postTree = function(baseTree, path, blob, cb) { |
| 122 | + this.updateTree = function(baseTree, path, blob, cb) { |
126 | 123 | var data = { |
127 | 124 | "base_tree": baseTree, |
128 | 125 | "tree": [ |
|
140 | 137 | }); |
141 | 138 | }; |
142 | 139 |
|
| 140 | + // Post a new tree object having a file path pointer replaced |
| 141 | + // with a new blob SHA getting a tree SHA back |
| 142 | + // ------- |
| 143 | + |
| 144 | + this.postTree = function(tree, cb) { |
| 145 | + _request("POST", repoPath + "/git/trees", { "tree": tree }, function(err, res) { |
| 146 | + if (err) return cb(err); |
| 147 | + cb(null, res.sha); |
| 148 | + }); |
| 149 | + }; |
| 150 | + |
143 | 151 | // Create a new commit object with the current commit SHA as the parent |
144 | 152 | // and the new tree SHA, getting a commit SHA back |
145 | 153 | // ------- |
|
198 | 206 | return ""; |
199 | 207 | } |
200 | 208 | } |
201 | | - |
202 | 209 | cb(null, decode(blob)); |
203 | 210 | }); |
204 | 211 | }); |
205 | 212 | }; |
206 | 213 |
|
| 214 | + // Remove a file from the tree |
| 215 | + // ------- |
| 216 | + |
| 217 | + this.remove = function(branch, path, cb) { |
| 218 | + that.getRef(branch, function(err, latestCommit) { |
| 219 | + that.getTree(latestCommit+"?recursive=true", function(err, tree) { |
| 220 | + // Update Tree |
| 221 | + var newTree = _.reject(tree, function(ref) { return ref.path === path }); |
| 222 | + _.each(newTree, function(ref) { |
| 223 | + if (ref.type === "tree") delete ref.sha; |
| 224 | + }); |
| 225 | + |
| 226 | + that.postTree(newTree, function(err, rootTree) { |
| 227 | + that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) { |
| 228 | + that.updateHead(branch, commit, function(err) { |
| 229 | + cb(err); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | + }); |
| 234 | + }); |
| 235 | + }; |
| 236 | + |
| 237 | + // Move a file to a new location |
| 238 | + // ------- |
| 239 | + |
| 240 | + this.move = function(branch, path, newPath, cb) { |
| 241 | + that.getRef(branch, function(err, latestCommit) { |
| 242 | + that.getTree(latestCommit+"?recursive=true", function(err, tree) { |
| 243 | + // Update Tree |
| 244 | + _.each(tree, function(ref) { |
| 245 | + if (ref.path === path) ref.path = newPath; |
| 246 | + if (ref.type === "tree") delete ref.sha; |
| 247 | + }); |
| 248 | + |
| 249 | + that.postTree(tree, function(err, rootTree) { |
| 250 | + that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) { |
| 251 | + that.updateHead(branch, commit, function(err) { |
| 252 | + cb(err); |
| 253 | + }); |
| 254 | + }); |
| 255 | + }); |
| 256 | + }); |
| 257 | + }); |
| 258 | + }; |
| 259 | + |
207 | 260 | // Write file contents to a given branch and path |
208 | 261 | // ------- |
209 | 262 |
|
210 | 263 | this.write = function(branch, path, content, message, cb) { |
211 | 264 | that.getRef(branch, function(err, latestCommit) { |
212 | 265 | that.postBlob(content, function(err, blob) { |
213 | | - that.postTree(latestCommit, path, blob, function(err, tree) { |
| 266 | + that.updateTree(latestCommit, path, blob, function(err, tree) { |
214 | 267 | that.commit(latestCommit, tree, message, function(err, commit) { |
215 | 268 | that.updateHead(branch, commit, function(err) { |
216 | 269 | cb(err); |
|
225 | 278 | // Top Level API |
226 | 279 | // ------- |
227 | 280 |
|
228 | | - this.getRepo = function(user, repo, branch) { |
229 | | - return new Github.Repository({user: user, name: repo, branch: branch || "master"}); |
| 281 | + this.getRepo = function(user, repo) { |
| 282 | + return new Github.Repository({user: user, name: repo}); |
230 | 283 | }; |
231 | 284 |
|
232 | 285 | this.getUser = function() { |
|
0 commit comments