From 3fbca6d271bf503de53921e5342ebc0c8b5b9e62 Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Fri, 8 Jan 2016 01:17:00 +0000 Subject: [PATCH] Ignore bower.json version in `bower version` Fixes #2139. Make `bower version` operate solely on git tags, ignoring the version field in bower.json (if it exists). --- lib/commands/version.js | 46 +++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/commands/version.js b/lib/commands/version.js index 778d907b6..28fde95a5 100644 --- a/lib/commands/version.js +++ b/lib/commands/version.js @@ -21,22 +21,20 @@ function version(logger, versionArg, options, config) { function bump(project, versionArg, message) { var cwd = project._config.cwd || process.cwd(); var newVersion; - var doGitCommit = false; return checkGit(cwd) .then(function (hasGit) { - doGitCommit = hasGit; + if (!hasGit) { + throw createError('This directory does not appear to be a git repository', 'ENOTGITREPOSITORY'); + } }) .then(project.getJson.bind(project)) - .then(function (json) { - newVersion = getNewVersion(json.version, versionArg); - json.version = newVersion; + .then(function(/*json (ignored)*/) { + return currentGitVersion(cwd); }) - .then(project.saveJson.bind(project)) - .then(function () { - if (doGitCommit) { - return gitCommitAndTag(cwd, newVersion, message); - } + .then(function (currentVersion) { + newVersion = getNewVersion(currentVersion, versionArg); + return gitCommitAndTag(cwd, newVersion, message); }) .then(function () { console.log('v' + newVersion); @@ -104,15 +102,33 @@ function gitCommitAndTag(cwd, newVersion, message) { var tag = 'v' + newVersion; message = message || tag; message = message.replace(/%s/g, newVersion); - return Q.nfcall(execFile, 'git', ['add', 'bower.json'], {env: process.env, cwd: cwd}) + return Q.nfcall(execFile, 'git', ['commit', '-m', message, '--allow-empty'], {env: process.env, cwd: cwd}) .then(function () { - return Q.nfcall(execFile, 'git', ['commit', '-m', message], {env: process.env, cwd: cwd}); - }) - .then(function () { - return Q.nfcall(execFile, 'git', ['tag', tag, '-am', message], {env: process.env, cwd: cwd}); + Q.nfcall(execFile, 'git', ['tag', tag, '-am', message], {env: process.env, cwd: cwd}); }); } +function currentGitVersion(cwd) { + return Q.nfcall(execFile, 'git', ['tag'], {env: process.env, cwd: cwd}) + .then(function(res) { + var versions = res[0] + .split(/\r?\n/) + .map(stripV) + .filter(semver.valid) + .sort(semver.compare); + + return versions[versions.length - 1] || '0.0.0'; + }); +} + +function stripV(s) { + if (s[0] === 'v') { + return s.slice(1); + } else { + return s; + } +} + // ------------------- version.readOptions = function (argv) {