Fallback to latest commit on master if the repo has no tags and target is *.

This commit is contained in:
André Cruz
2013-04-15 00:59:53 +01:00
parent 2fc014af03
commit 36c3cac484
2 changed files with 36 additions and 10 deletions

View File

@@ -51,21 +51,29 @@ GitResolver.fetchRefs = function (source) {};
// -----------------
GitResolver.prototype._findResolution = function () {
GitResolver.prototype._findResolution = function (target) {
var branches,
self = this.constructor;
target = target || this._target;
// Target is a range/version
if (semver.valid(this._target) || semver.validRange(this._target)) {
if (semver.valid(target) != null || semver.validRange(target) != null) {
return self.fetchVersions(this._sourcePath)
.then(function (versions) {
// If there are no tags and target is *,
// fallback to the latest commit on master
if (!versions.length && target === '*') {
return this._findResolution('master');
}
// Find the highest one that satifies the target
var version = mout.array.find(versions, function (version) {
return semver.satisfies(version, this._target);
return semver.satisfies(version, target);
}, this);
if (!version) {
throw createError('No tag found that was able to satisfy "' + this._target + '"', 'ENORESTARGET', {
throw createError('No tag found that was able to satisfy "' + target + '"', 'ENORESTARGET', {
details: !versions.length ?
'No tags found in "' + this._source + '"' :
'Available tags in "' + this._source + '" are: ' + versions.join(', ')
@@ -78,23 +86,23 @@ GitResolver.prototype._findResolution = function () {
// Target is a commit, so it's a stale target (not a moving target)
// There's nothing to do in this case
if ((/^[a-f0-9]{40}$/).test(this._target)) {
return Q.resolve({ type: 'commit', commit: this._target });
if ((/^[a-f0-9]{40}$/).test(target)) {
return Q.resolve({ type: 'commit', commit: target });
}
// Otherwise, assume target is a branch
return self.fetchHeads(this._sourcePath)
.then(function (heads) {
if (!heads[this._target]) {
if (!heads[target]) {
branches = Object.keys(heads);
throw createError('Branch "' + this._target + '" does not exist', 'ENORESTARGET', {
throw createError('Branch "' + target + '" does not exist', 'ENORESTARGET', {
details: !branches.length ?
'No branches found in "' + this._source + '"' :
'Available branches in "' + this._source + '" are: ' + branches.join(', ')
});
}
return { type: 'branch', branch: this._target, commit: heads[this._target] };
return { type: 'branch', branch: target, commit: heads[target] };
}.bind(this));
};