From cb2cb7ea75cfac0b38c8ee252c4291d4716c0741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Sun, 14 Oct 2012 15:54:26 +0100 Subject: [PATCH] Fix bug with -f option, add compatibility with repos that do not have tags. This fixes the fact that if a repo do not have tags, install/update wouldn't fetch new commits. --- lib/core/manager.js | 4 +-- lib/core/package.js | 67 ++++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/lib/core/manager.js b/lib/core/manager.js index e52029d2..48dac091 100644 --- a/lib/core/manager.js +++ b/lib/core/manager.js @@ -91,8 +91,8 @@ Manager.prototype.loadJSON = function () { fs.readFile(json, 'utf8', function (err, json) { if (err) return this.emit('error', err); this.json = JSON.parse(json); - this.name = this.json.name; - this.version = this.json.version; + this.version = json.version; + if (!this.name) this.name = json.name; this.emit('loadJSON'); }.bind(this)); }.bind(this)); diff --git a/lib/core/package.js b/lib/core/package.js index 7d303949..53ed2d4b 100644 --- a/lib/core/package.js +++ b/lib/core/package.js @@ -193,9 +193,20 @@ Package.prototype.install = function () { }.bind(this)); }; Package.prototype.cleanUpLocal = function () { + if (!this.json.name) { + this.json.name = this.name; + this.json.version = semver.valid(this.version) ? this.version : '0.0.0'; + } + if (this.json.version === '0.0.0' && this.version !== '0.0.0') this.json.commit = this.version; + else delete this.json.commit; + if (this.gitUrl) this.json.repository = { type: "git", url: this.gitUrl }; if (this.assetUrl) this.json = this.generateAssetJSON(); - fs.writeFile(path.join(this.localPath, config.json), JSON.stringify(this.json, null, 2)); + + var jsonStr = JSON.stringify(this.json, null, 2); + fs.writeFile(path.join(this.localPath, config.json), jsonStr); + fs.writeFile(path.join(path.resolve(cache, this.name), config.json), jsonStr); + rimraf(path.join(this.localPath, '.git'), this.emit.bind(this, 'install')); }; Package.prototype.generateAssetJSON = function () { @@ -222,18 +233,19 @@ Package.prototype.loadJSON = function (name) { var pathname = name || ( this.assetType ? 'index' + this.assetType : config.json ); readJSON(path.join(this.path, pathname), function (err, json) { - if (err) { if (!name) return this.loadJSON('package.json'); return this.assetUrl ? this.emit('loadJSON') : this.path && this.on('describeTag', function (tag) { - this.version = this.tag = semver.clean(tag); + tag = semver.clean(tag); + if (tag) this.version = this.tag = tag; + else this.version = this.tag; this.emit('loadJSON'); }.bind(this)).describeTag(); } this.json = json; + this.version = json.commit ? this.tag || json.commit || json.version : json.version; + if (!this.name) this.name = json.name; - this.name = this.json.name; - this.version = this.json.version; this.emit('loadJSON'); }.bind(this), this); }; @@ -354,12 +366,18 @@ Package.prototype.cache = function () { // Be aware that a similar package might already flushed it // To prevent that we check the unit of work storage if (this.opts.force && !this.unitWork.retrieve('flushed#' + this.name)) { - rimraf(this.path, function (err) { - if (err) this.emit('error', err); - this.unitWork.store('flushed#' + this.name, true); - this.cache(); + fs.stat(this.path, function (err, stat) { + if (!err) { + rimraf(this.path, function (err) { + if (err) return this.emit('error', err); + this.unitWork.store('flushed#' + this.name, true); + this.cache(); + }.bind(this)); + } else { + this.unitWork.store('flushed#' + this.name, true); + this.cache(); + } }.bind(this)); - return this; } @@ -419,13 +437,7 @@ Package.prototype.checkout = function () { shizzle: this.name + '#' + this.tag }).on('data', this.emit.bind(this, 'data')); - spawn('git', [ 'checkout', '-b', this.tag, this.tag], { cwd: this.path }).on('close', function (code) { - if (code == 128) { - return spawn('git', [ 'checkout', this.tag], { cwd: this.path }).on('close', function (code) { - this.emit('checkout'); - this.loadJSON(); - }.bind(this)); - } + spawn('git', [ 'checkout', this.tag], { cwd: this.path }).on('close', function (code) { if (code != 0) return this.emit('error', new Error('Git status: ' + code)); this.emit('checkout'); this.loadJSON(); @@ -470,7 +482,20 @@ Package.prototype.versions = function () { versions = versions.sort(function (a, b) { return semver.gt(a, b) ? -1 : 1; }); - this.emit('versions', versions); + + if (versions.length) return this.emit('versions', versions); + + versions = ''; + cp = spawn('git', ['log', '-n', 1, '--format=%H'], { cwd: path.resolve(cache, this.name) }); + + cp.stdout.setEncoding('utf8'); + cp.stdout.on('data', function (data) { + versions += data; + }); + cp.on('close', function () { + versions = versions.split("\n"); + this.emit('versions', versions); + }.bind(this)); }.bind(this)); }.bind(this)).fetch(); }; @@ -479,7 +504,11 @@ Package.prototype.fetch = function () { var cp = spawn('git', ['fetch'], { cwd: path.resolve(cache, this.name) }); cp.on('close', function (code) { if (code != 0) return this.emit('error', new Error('Git status: ' + code)); - this.emit('fetch'); + cp = spawn('git', ['reset', '--hard', 'origin/HEAD'], { cwd: path.resolve(cache, this.name) }); + cp.on('close', function (code) { + if (code != 0) return this.emit('error', new Error('Git status: ' + code)); + this.emit('fetch'); + }.bind(this)); }.bind(this)); };