Skip to content

Commit 5155fd1

Browse files
committed
Delete old tree and write new head on checkout
1 parent cc66916 commit 5155fd1

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/js-git-api.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var httpTransport = require('js-git/net/transport-http')(request);
99
var fetchPackProtocol = require('js-git/net/git-fetch-pack');
1010
var modes = require('js-git/lib/modes');
1111
var archiver = require('archiver');
12+
var del = require('del');
1213
var mkdirp = require('mkdirp');
1314
var ncp = require('ncp').ncp;
1415
var utils = require('../lib/utils');
@@ -206,8 +207,18 @@ module.exports = {
206207
return self.readRef(repo, ref, callback);
207208
});
208209
},
210+
writeHead: function(repo, ref, callback) {
211+
var headPath = path.join(repo.rootPath, 'HEAD');
212+
if (!this.isHash(ref)) {
213+
ref = 'ref: ' + ref;
214+
}
215+
fs.writeFile(headPath, ref, callback);
216+
},
217+
isHash: function(str) {
218+
return str.match(/^[0-9a-f]{40}$/i);
219+
},
209220
readRef: function(repo, ref, callback) {
210-
if (ref.match(/^[0-9a-f]{40}$/i)) {
221+
if (this.isHash(ref)) {
211222
return callback(null, ref);
212223
}
213224

@@ -268,18 +279,33 @@ module.exports = {
268279

269280
function writeTree(err, tree) {
270281
if (err) return callback(err);
271-
if (!tree) return callback();
282+
if (!tree) {
283+
if (ref) {
284+
return self.writeHead(repo, ref, callback);
285+
}
286+
return callback();
287+
}
272288

273289
var repoPath = repo.rootPath;
274290
if (path.basename(repoPath) == '.git') {
275291
repoPath = path.dirname(repoPath);
276292
}
277293

278-
tree.read(onEntry);
294+
del([
295+
path.join(repoPath, '*'),
296+
'!' + path.join(repoPath, '.git')
297+
], function(err, paths) {
298+
tree.read(onEntry);
299+
});
279300

280301
function onEntry(err, entry) {
281302
if (err) return callback(err);
282-
if (!entry) return callback();
303+
if (!entry) {
304+
if (ref) {
305+
return self.writeHead(repo, ref, callback);
306+
}
307+
return callback();
308+
}
283309

284310
if (entry.path == '/') {
285311
return nextEntry();

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"archiver": "^0.14.4",
1919
"commander": "https://github.com/whyleee/commander.js/tarball/master",
20+
"del": "^1.2.1",
2021
"global-tunnel": "https://github.com/whyleee/global-tunnel/tarball/master",
2122
"ini": "^1.3.4",
2223
"js-git": "^0.7.7",

0 commit comments

Comments
 (0)