Skip to content

Commit f2052fd

Browse files
committed
Merge branch 'mkw/treeish' into civitas-patched
2 parents dd904cf + fa67f17 commit f2052fd

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

lib/git/commands.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,42 @@ exports.tag = function(version, message, cwd, cb) {
4848
run_command('git tag -a "' + version + '" -m "' + message + '"', cwd, cb);
4949
};
5050

51-
exports.checkout_branch = function(branch_name, cwd, cb) {
52-
run_command('git checkout -b ' + branch_name, cwd, cb);
51+
exports.checkout_treeish = function(treeish_name, cwd, cb) {
52+
// See what branch we are on
53+
run_command('git symbolic-ref --quiet --short HEAD', cwd, function(err, current_branch) {
54+
if (err) {
55+
// If we are not on a branch, this will error out, but ignore it
56+
logger.debug('Repo is not currently on a branch: %s', cwd);
57+
}
58+
59+
// If the selected name is the current branch, just check it out.
60+
if (current_branch === treeish_name) {
61+
// We are checking out the current branch, so just checkout.
62+
run_command('git checkout ' + treeish_name, cwd, cb);
63+
return;
64+
}
65+
66+
// Assume that the checkout is not a branch (tree or hash) and try check it out
67+
run_command('git checkout ' + treeish_name, cwd, function(err, output) {
68+
if (err) {
69+
logger.debug('Could not checkout %s; creating branch', treeish_name);
70+
run_command('git checkout -b ' + treeish_name, cwd, cb);
71+
return;
72+
}
73+
cb(null, output);
74+
});
75+
});
5376
};
5477

55-
exports.clone = function(repo_url, branch_name, cwd, cb) {
56-
run_command('git clone -b ' + branch_name + ' ' + repo_url + ' ' + branch_name, cwd, cb);
78+
exports.clone = function(repo_url, treeish_name, cwd, cb) {
79+
run_command('git clone -n ' + repo_url + ' ' + treeish_name, cwd, function(err, output) {
80+
if (err) {
81+
cb(err);
82+
return;
83+
}
84+
85+
exports.checkout_treeish(treeish_name, cwd + '/' + treeish_name, cb);
86+
});
5787
};
5888

5989
exports.pull = function(branch_name, cwd, cb) {
@@ -122,8 +152,8 @@ exports.listAdditionalPropertyFiles = function(modifiedRecords, cwd, cb) {
122152

123153
function is_properties_not_modified(modifiedRecords, file) {
124154
return file.substr(file.lastIndexOf('.') + 1) == "properties" && modifiedRecords.filter(function(record) {
125-
return record.path == file;
126-
}).length <= 0;
155+
return record.path == file;
156+
}).length <= 0;
127157
}
128158

129159
run_command('git ls-tree --name-status -r HEAD', cwd, function(err, output) {
@@ -133,7 +163,7 @@ exports.listAdditionalPropertyFiles = function(modifiedRecords, cwd, cb) {
133163
var records = [];
134164
var files = output.split('\n');
135165
files.forEach(function(file) {
136-
if(is_properties_not_modified(modifiedRecords, file)) {
166+
if (is_properties_not_modified(modifiedRecords, file)) {
137167
records.push({'type': 'M', 'path': file});
138168
}
139169
});

test/git2consul_init_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('Initializing git2consul', function() {
3131

3232
var create_branch_and_add = function(branch_name, done) {
3333
return function() {
34-
git_commands.checkout_branch(branch_name, git_utils.TEST_REMOTE_REPO, function(err) {
34+
git_commands.checkout_treeish(branch_name, git_utils.TEST_REMOTE_REPO, function(err) {
3535
if (err) return done(err);
3636

3737
git_utils.addFileToGitRepo("readme.md", "Test file in " + branch_name + " branch", branch_name + " commit", function(err) {

0 commit comments

Comments
 (0)