From eddd53bf6e7e91e3efad1587433dc8fe531616e8 Mon Sep 17 00:00:00 2001 From: Turing Eret Date: Wed, 22 Feb 2017 08:19:33 -0700 Subject: [PATCH 1/2] Add support for the "support_tags" command when running with no hooks or in "no daemon" mode. --- lib/git/commands.js | 25 +++++++++++++++++++++++++ lib/git/repo.js | 15 +++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/git/commands.js b/lib/git/commands.js index 2df8890..4c2e95a 100644 --- a/lib/git/commands.js +++ b/lib/git/commands.js @@ -1,4 +1,5 @@ var exec = require('child_process').exec; +var execSync = require('child_process').execSync; var logger = require('../logging.js'); @@ -24,6 +25,18 @@ var run_command = function(cmd, cwd, cb) { }); }; +var run_command_sync = function(cmd, cwd, cb) { + logger.trace('Running synchronously %s in %s', cmd, cwd); + try { + var stdout = execSync(cmd, {cwd: cwd}); + if (stdout.length > 0) logger.trace("stdout:\n" + stdout); + cb(null, stdout); + } + catch(err) { + cb(new Error(err)); + } +}; + exports.init = function(cwd, cb) { run_command('git init', cwd, cb); }; @@ -72,6 +85,18 @@ exports.extractTags = function (repo_url, cwd, cb) { }); } +exports.extractTagsSync = function (repo_url, cwd, cb) { + run_command_sync('git ls-remote --tags' + ' ' + repo_url, cwd, function (err, output) { + var extracted_tags = []; + var one_tag; + var tag_regex = /refs\/tags\/(.*)\^\{\}/ig; + while ((one_tag = tag_regex.exec(output)) !== null) { + extracted_tags.push(one_tag[1]); + } + cb(null, extracted_tags) + }); +} + var fixup_path = function(path) { if ((path.charAt(0) === '"') && (path.charAt(path.length-1) === '"')) { path = path.substring(1, path.length-1); diff --git a/lib/git/repo.js b/lib/git/repo.js index 948e85a..19bfbb3 100644 --- a/lib/git/repo.js +++ b/lib/git/repo.js @@ -66,6 +66,21 @@ function Repo(repo_config) { // TODO: Switch to passing in the repo, not the repo_config. branches[branch] = new Branch(repo_config, branch); }); + + if (repo_config.support_tags === true) { + git_commands.extractTagsSync(this_obj.url, this_obj.repo_config.local_store, function (err, tags) { + if (err) throw new Error(err) + + var new_tags = tags.filter(function (tag) { + return this_obj.branch_names.indexOf(tag) < 0 + }); + + new_tags.forEach(function (tag_name) { + this_obj.branches[tag_name] = new Branch(repo_config, tag_name); + this_obj.branch_names.push(tag_name); + }); + }); + } } Repo.prototype.getBranch = function(branch_name) { From db8e20ab3715ed74c3c7fba98262601c0e0d5183 Mon Sep 17 00:00:00 2001 From: Turing Eret Date: Thu, 2 Mar 2017 09:58:40 -0700 Subject: [PATCH 2/2] Add a test for the support of support_tags without using hooks. --- test/git2consul_support_tags_test.js | 48 ++++++++++++++++++++++++++++ test/utils/git_utils.js | 7 ++++ 2 files changed, 55 insertions(+) create mode 100644 test/git2consul_support_tags_test.js diff --git a/test/git2consul_support_tags_test.js b/test/git2consul_support_tags_test.js new file mode 100644 index 0000000..6ce7fcc --- /dev/null +++ b/test/git2consul_support_tags_test.js @@ -0,0 +1,48 @@ +var should = require('should'); +var _ = require('underscore'); + +// We want this above any git2consul module to make sure logging gets configured +require('./git2consul_bootstrap_test.js'); + +var git = require('../lib/git'); +var git_utils = require('./utils/git_utils.js'); +var consul_utils = require('./utils/consul_utils.js'); + +var git_commands = require('../lib/git/commands.js'); + +describe('Support tags', function() { + it ('should populate consul with the sample key/value pair under master and the tag', function(done) { + var repo_config = git_utils.createRepoConfig(); + repo_config.support_tags = true; + + git_commands.init(git_utils.TEST_REMOTE_REPO, function(err) { + if (err) return done(err); + + var sample_key = 'readme.md'; + var sample_value = 'stub data'; + git_utils.addFileToGitRepo(sample_key, sample_value, "Stub commit.", function(err) { + if (err) return done(err); + + git_utils.addTagToGitRepo("v0.1", "v0.1", function(err) { + if (err) return done(err); + var config = { + repos: [repo_config], + local_store: git_utils.TEST_WORKING_DIR, + no_daemon: true + }; + + // Now, create a repo. The repo should populate consul with the proper values. + git.createRepos(config, function(err) { + consul_utils.validateValue(repo_config.name + '/' + 'master' + '/' + sample_key, sample_value, function(err, value) { + consul_utils.validateValue(repo_config.name + '/' + 'v0.1' + '/' + sample_key, sample_value, function(err, value) { + if (err) return done(err); + (undefined === err).should.equal(true); + done(); + }); + }); + }); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/utils/git_utils.js b/test/utils/git_utils.js index 383823a..c2b0bc8 100644 --- a/test/utils/git_utils.js +++ b/test/utils/git_utils.js @@ -112,3 +112,10 @@ exports.symlinkFileInGitRepo = function(link, referrent, commit_message, cb) { }); }; + +exports.addTagToGitRepo = function(tag, commit_message, cb) { + git_commands.tag(tag, commit_message, git_utils.TEST_REMOTE_REPO, function (err) { + if (err) return cb(err); + cb(); + }); +}