Skip to content

Commit e9421cb

Browse files
committed
Support for moving and deleting files.
1 parent e892a25 commit e9421cb

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

github.js

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.3.0
1+
// Github.js 0.4.0
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -45,7 +45,6 @@
4545

4646
Github.Repository = function(options) {
4747
var repo = options.name;
48-
var branch = options.branch;
4948
var user = options.user;
5049

5150
var that = this;
@@ -80,7 +79,6 @@
8079
});
8180
};
8281

83-
8482
// For a given file path, get the corresponding sha (blob for files, tree for dirs)
8583
// TODO: So inefficient, it sucks hairy donkey balls.
8684
// -------
@@ -118,11 +116,10 @@
118116
});
119117
};
120118

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
123120
// -------
124121

125-
this.postTree = function(baseTree, path, blob, cb) {
122+
this.updateTree = function(baseTree, path, blob, cb) {
126123
var data = {
127124
"base_tree": baseTree,
128125
"tree": [
@@ -140,6 +137,17 @@
140137
});
141138
};
142139

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+
143151
// Create a new commit object with the current commit SHA as the parent
144152
// and the new tree SHA, getting a commit SHA back
145153
// -------
@@ -198,19 +206,64 @@
198206
return "";
199207
}
200208
}
201-
202209
cb(null, decode(blob));
203210
});
204211
});
205212
};
206213

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+
207260
// Write file contents to a given branch and path
208261
// -------
209262

210263
this.write = function(branch, path, content, message, cb) {
211264
that.getRef(branch, function(err, latestCommit) {
212265
that.postBlob(content, function(err, blob) {
213-
that.postTree(latestCommit, path, blob, function(err, tree) {
266+
that.updateTree(latestCommit, path, blob, function(err, tree) {
214267
that.commit(latestCommit, tree, message, function(err, commit) {
215268
that.updateHead(branch, commit, function(err) {
216269
cb(err);
@@ -225,8 +278,8 @@
225278
// Top Level API
226279
// -------
227280

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});
230283
};
231284

232285
this.getUser = function() {

0 commit comments

Comments
 (0)