Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/git/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;

var logger = require('../logging.js');

Expand All @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions lib/git/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
48 changes: 48 additions & 0 deletions test/git2consul_support_tags_test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
});
});
});
});
7 changes: 7 additions & 0 deletions test/utils/git_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}