mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-02-11 08:39:17 +00:00
add git support
This commit is contained in:
parent
5f0d089e00
commit
1c718f772d
136
lib/scm/git.js
Normal file
136
lib/scm/git.js
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var ParentScm = require('./base').Scm,
|
||||||
|
inherits = require('util').inherits,
|
||||||
|
Steppy = require('twostep').Steppy,
|
||||||
|
_ = require('underscore');
|
||||||
|
|
||||||
|
function Scm(params) {
|
||||||
|
ParentScm.call(this, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.Scm = Scm;
|
||||||
|
|
||||||
|
inherits(Scm, ParentScm);
|
||||||
|
|
||||||
|
Scm.prototype.defaultRev = 'master';
|
||||||
|
|
||||||
|
// use 2 invisible separators as fields separator
|
||||||
|
Scm.prototype._fieldsSeparator = String.fromCharCode(2063);
|
||||||
|
Scm.prototype._fieldsSeparator += Scm.prototype._fieldsSeparator;
|
||||||
|
|
||||||
|
Scm.prototype._revTemplate = [
|
||||||
|
'%H', '%cn', '%cd', '%s', '%d'
|
||||||
|
].join(Scm.prototype._fieldsSeparator);
|
||||||
|
|
||||||
|
Scm.prototype._parseRev = function(str) {
|
||||||
|
var parts = str.split(this._fieldsSeparator);
|
||||||
|
|
||||||
|
var rev = {
|
||||||
|
id: parts[0],
|
||||||
|
author: parts[1],
|
||||||
|
date: new Date(parts[2]).getTime(),
|
||||||
|
comment: parts[3]
|
||||||
|
};
|
||||||
|
|
||||||
|
var refsStr = parts[4];
|
||||||
|
if (refsStr) {
|
||||||
|
var refs = refsStr
|
||||||
|
.replace(/^ *\(/, '')
|
||||||
|
.replace(/\) *$/, '')
|
||||||
|
.split(', ');
|
||||||
|
|
||||||
|
var tags = _(refs).chain().filter(function(ref) {
|
||||||
|
return /^tag: /.test(ref)
|
||||||
|
}).map(function(ref) {
|
||||||
|
return ref.replace(/^tag: /, '');
|
||||||
|
}).value();
|
||||||
|
|
||||||
|
if (tags.length) {
|
||||||
|
// sort tags alphabetically
|
||||||
|
rev.tags = _(tags).sortBy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rev;
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.clone = function(dst, rev, callback) {
|
||||||
|
var self = this;
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
self.run({
|
||||||
|
cmd: 'git',
|
||||||
|
args: ['clone', '--recursive', self.repository, dst]
|
||||||
|
}, this.slot());
|
||||||
|
self.cwd = dst;
|
||||||
|
},
|
||||||
|
function() {
|
||||||
|
self.run({
|
||||||
|
cmd: 'git',
|
||||||
|
args: ['reset', '--hard', rev]
|
||||||
|
}, this.slot());
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.pull = function(rev, callback) {
|
||||||
|
// TODO: pull changes without update
|
||||||
|
this.run({cmd: 'git', args: ['pull']}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.getRev = function(rev, callback) {
|
||||||
|
var self = this;
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
self.run({cmd: 'git', args: [
|
||||||
|
'show', rev,
|
||||||
|
'--pretty=' + self._revTemplate
|
||||||
|
]}, this.slot());
|
||||||
|
},
|
||||||
|
function(err, stdout) {
|
||||||
|
var row = stdout.split('\n')[0];
|
||||||
|
|
||||||
|
this.pass(self._parseRev(row));
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.getCurrent = function(callback) {
|
||||||
|
var self = this;
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
self.getRev('HEAD', this.slot());
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.getChanges = function(rev1, rev2, callback) {
|
||||||
|
var self = this;
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
self.run({cmd: 'git', args: [
|
||||||
|
'log', rev1 + '..' + rev2,
|
||||||
|
'--pretty=' + self._revTemplate
|
||||||
|
]}, this.slot());
|
||||||
|
},
|
||||||
|
function(err, stdout) {
|
||||||
|
// always skip last line - it's empty
|
||||||
|
var rows = stdout.split('\n').slice(0, -1);
|
||||||
|
|
||||||
|
var changes = _(rows).map(function(str) {
|
||||||
|
return self._parseRev(str);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.pass(changes);
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Scm.prototype.update = function(rev, callback) {
|
||||||
|
this.run({cmd: 'git', args: ['checkout', rev]}, callback);
|
||||||
|
};
|
@ -44,6 +44,25 @@ exports.mercurialRevs = [{
|
|||||||
comment: 'add tags'
|
comment: 'add tags'
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
exports.gitRevs = [{
|
||||||
|
id: '4ec4643d2044871177d758b3b0e7962b5c4f40d1',
|
||||||
|
tags: ['zero'],
|
||||||
|
author: 'oleg',
|
||||||
|
date: new Date('Mon Jul 13 22:30:58 2015 +0300').getTime(),
|
||||||
|
comment: 'zero revision'
|
||||||
|
}, {
|
||||||
|
id: 'f76bae67efc6fd5a43392517646bb9d685f4266f',
|
||||||
|
author: 'oleg',
|
||||||
|
date: new Date('Mon Jul 13 22:31:58 2015 +0300').getTime(),
|
||||||
|
comment: 'first revision'
|
||||||
|
}, {
|
||||||
|
id: '39245d9b93bcd2a0c6708d483b83c98a7bff1d3e',
|
||||||
|
tags: ['release-0.1.0', 'second-revision'],
|
||||||
|
author: 'oleg',
|
||||||
|
date: new Date('Mon Jul 13 22:32:59 2015 +0300').getTime(),
|
||||||
|
comment: 'third revision'
|
||||||
|
}];
|
||||||
|
|
||||||
exports.initDb = function(callback) {
|
exports.initDb = function(callback) {
|
||||||
db.init('path/to/db/ignored/for/memdown', {
|
db.init('path/to/db/ignored/for/memdown', {
|
||||||
db: require('memdown'),
|
db: require('memdown'),
|
||||||
|
1
test/repos/git
Submodule
1
test/repos/git
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 39245d9b93bcd2a0c6708d483b83c98a7bff1d3e
|
25
test/scm.js
25
test/scm.js
@ -6,14 +6,16 @@ var expect = require('expect.js'),
|
|||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
createScm = require('../lib/scm').createScm,
|
createScm = require('../lib/scm').createScm,
|
||||||
SpawnCommand = require('../lib/command/spawn').Command,
|
SpawnCommand = require('../lib/command/spawn').Command,
|
||||||
mercurialRevs = require('./helpers').mercurialRevs;
|
mercurialRevs = require('./helpers').mercurialRevs,
|
||||||
|
gitRevs = require('./helpers').gitRevs;
|
||||||
|
|
||||||
|
|
||||||
var getTestData = function(type) {
|
var getTestData = function(type) {
|
||||||
if (type === 'mercurial') return mercurialRevs;
|
if (type === 'mercurial') return mercurialRevs;
|
||||||
|
if (type === 'git') return gitRevs;
|
||||||
};
|
};
|
||||||
|
|
||||||
['mercurial'].forEach(function(type) {
|
['mercurial', 'git'].forEach(function(type) {
|
||||||
describe(type, function() {
|
describe(type, function() {
|
||||||
var data = getTestData(type),
|
var data = getTestData(type),
|
||||||
repositoryName = 'test-repository',
|
repositoryName = 'test-repository',
|
||||||
@ -58,7 +60,10 @@ var getTestData = function(type) {
|
|||||||
// specified revision no later revision will be cloned
|
// specified revision no later revision will be cloned
|
||||||
// including those one with add tag (it's after rev 0 in our
|
// including those one with add tag (it's after rev 0 in our
|
||||||
// repo)
|
// repo)
|
||||||
expect(rev).eql(_(data[0]).omit('tags'));
|
var expectedRev = (
|
||||||
|
type === 'mercurial' ? _(data[0]).omit('tags') : data[0]
|
||||||
|
);
|
||||||
|
expect(rev).eql(expectedRev);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -67,7 +72,10 @@ var getTestData = function(type) {
|
|||||||
scm.getRev(data[0].id, function(err, rev) {
|
scm.getRev(data[0].id, function(err, rev) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
// no tag here, see note above
|
// no tag here, see note above
|
||||||
expect(rev).eql(_(data[0]).omit('tags'));
|
var expectedRev = (
|
||||||
|
type === 'mercurial' ? _(data[0]).omit('tags') : data[0]
|
||||||
|
);
|
||||||
|
expect(rev).eql(expectedRev);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -95,6 +103,15 @@ var getTestData = function(type) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var itOrSkip = type === 'git' ? it.skip : it;
|
||||||
|
itOrSkip('expect current revision still equals to rev0', function(done) {
|
||||||
|
scm.getCurrent(function(err, rev) {
|
||||||
|
if (err) return done(err);
|
||||||
|
expect(rev).eql(data[0]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('update to default revision (should update to last) without error',
|
it('update to default revision (should update to last) without error',
|
||||||
function(done) {
|
function(done) {
|
||||||
scm.update(scm.defaultRev, done);
|
scm.update(scm.defaultRev, done);
|
||||||
|
Loading…
Reference in New Issue
Block a user