Skip to content

Commit 9aa1560

Browse files
committed
Smart caching of latest commit sha to overcome fast-forward update issue.
1 parent 89de1b6 commit 9aa1560

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

github.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Github.js 0.5.1
1+
// Github.js 0.5.2
22
// (c) 2012 Michael Aufreiter, Development Seed
33
// Github.js is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -14,9 +14,12 @@
1414
// =======
1515

1616
function headers() {
17+
var headers = {}
18+
if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' };
19+
if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' };
1720
return options.auth == 'oauth'
1821
? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
19-
: { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password) }
22+
: { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' }
2023
}
2124

2225
function _request(method, path, data, cb) {
@@ -66,6 +69,23 @@
6669
var that = this;
6770
var repoPath = "/repos/" + user + "/" + repo;
6871

72+
var currentTree = {
73+
"branch": null,
74+
"sha": null
75+
};
76+
77+
// Uses the cache if branch has not been changed
78+
// -------
79+
80+
function updateTree(branch, cb) {
81+
if (branch === currentTree.branch && currentTree.sha) return cb(null, currentTree.sha);
82+
that.getRef(branch, function(err, sha) {
83+
currentTree.branch = branch;
84+
currentTree.sha = sha;
85+
cb(err, sha);
86+
});
87+
}
88+
6989
// Get a particular reference
7090
// -------
7191

@@ -90,16 +110,16 @@
90110
// -------
91111

92112
this.getBlob = function(sha, cb) {
93-
_raw_request("GET", repoPath + "/git/blobs/" + sha, null, function(err, res) {
94-
cb(err, res);
95-
});
113+
_raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb);
96114
};
97115

98116
// For a given file path, get the corresponding sha (blob for files, tree for dirs)
99117
// TODO: So inefficient, it sucks hairy donkey balls.
100118
// -------
101119

102120
this.getSha = function(branch, path, cb) {
121+
// Just use head if path is empty
122+
if (path === "") return that.getRef(branch, cb);
103123
that.getTree(branch+"?recursive=true", function(err, tree) {
104124
var file = _.select(tree, function(file) {
105125
return file.path === path;
@@ -181,6 +201,7 @@
181201
};
182202

183203
_request("POST", repoPath + "/git/commits", data, function(err, res) {
204+
currentTree.sha = res.sha; // update latest commit
184205
if (err) return cb(err);
185206
cb(null, res.sha);
186207
});
@@ -210,7 +231,6 @@
210231
this.read = function(branch, path, cb) {
211232
that.getSha(branch, path, function(err, sha) {
212233
if (!sha) return cb("not found", null);
213-
214234
that.getBlob(sha, cb);
215235
});
216236
};
@@ -219,7 +239,7 @@
219239
// -------
220240

221241
this.remove = function(branch, path, cb) {
222-
that.getRef(branch, function(err, latestCommit) {
242+
updateTree(branch, function(err, latestCommit) {
223243
that.getTree(latestCommit+"?recursive=true", function(err, tree) {
224244
// Update Tree
225245
var newTree = _.reject(tree, function(ref) { return ref.path === path });
@@ -242,7 +262,7 @@
242262
// -------
243263

244264
this.move = function(branch, path, newPath, cb) {
245-
that.getRef(branch, function(err, latestCommit) {
265+
updateTree(branch, function(err, latestCommit) {
246266
that.getTree(latestCommit+"?recursive=true", function(err, tree) {
247267
// Update Tree
248268
_.each(tree, function(ref) {
@@ -265,13 +285,11 @@
265285
// -------
266286

267287
this.write = function(branch, path, content, message, cb) {
268-
that.getRef(branch, function(err, latestCommit) {
288+
updateTree(branch, function(err, latestCommit) {
269289
that.postBlob(content, function(err, blob) {
270290
that.updateTree(latestCommit, path, blob, function(err, tree) {
271291
that.commit(latestCommit, tree, message, function(err, commit) {
272-
that.updateHead(branch, commit, function(err) {
273-
cb(err);
274-
});
292+
that.updateHead(branch, commit, cb);
275293
});
276294
});
277295
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy