mecano
Version:
Common functions for system deployment.
70 lines (65 loc) • 1.84 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var fs, misc, path;
module.exports = function(options, callback) {
var repo_exists, repo_uptodate;
options.log({
message: "Entering git",
level: 'DEBUG',
module: 'mecano/lib/git'
});
if (options.revision == null) {
options.revision = 'HEAD';
}
repo_exists = false;
repo_uptodate = false;
return this.call(function(_, callback) {
return fs.exists(options.ssh, options.target, function(err, exists) {
var gitDir;
if (err) {
return callback(err);
}
repo_exists = exists;
if (!exists) {
return callback();
}
gitDir = options.target + "/.git";
return fs.exists(options.ssh, gitDir, function(err, exists) {
if (!exists) {
return callback(Error("Not a git repository"));
}
return callback();
});
});
}).execute({
cmd: "git clone " + options.source + " " + options.target,
cwd: path.dirname(options.target),
unless: function() {
return repo_exists;
}
}).execute({
cmd: "current=`git log --pretty=format:'%H' -n 1`\ntarget=`git rev-list --max-count=1 " + options.revision + "`\necho \"current revision: $current\"\necho \"expected revision: $target\"\nif [ $current != $target ]; then exit 3; fi",
cwd: options.target,
trap: true,
code_skipped: 3,
"if": function() {
return repo_exists;
},
shy: true
}, function(err, uptodate) {
if (err) {
throw err;
}
return repo_uptodate = uptodate;
}).execute({
cmd: "git checkout " + options.revision,
cwd: options.target,
unless: function() {
return repo_uptodate;
}
}).then(function(err, status) {
return callback(err, status);
});
};
fs = require('ssh2-fs');
path = require('path');
misc = require('../misc');