-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymcode.js
83 lines (76 loc) · 2.04 KB
/
symcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Live code project
* Utilities library
*/
var exec = require('child_process').exec;
var fs = require('fs');
/**
* Repo check (private method)
*/
function verifyRepo(repo)
{
// TODO check that directory exists, and has a ".git" subdir
return true;
};
/**
* Convert the git log into a JSON object of commit hashes/summaries.
*
* @return
* If error occurred, JSON with a single key of 'error'. Otherwise,
* JSON with numerical keys indicating step number, and a value of
* a nested object containing { hash: hash, summary: summary }.
*/
module.exports.getSteps = function(repo, callback) {
// Verify valid repo name
if (!verifyRepo(repo))
{
process.nextTick(function() {
callback({'error': {'on': 'verifyRepo', 'command': 'steps', 'message': 'Bad repo name "' + repo + '"'}});
});
return;
}
// Perform the log parsing
exec('git --no-pager log --oneline --reverse master', { cwd: 'data/' + repo }, function(error, stdout, stderr) {
var ret = {};
if (error !== null)
{
ret = {'error': {'on': 'log', 'command': 'steps', 'message': error}};
}
else
{
ret.steps = [];
var lines = stdout.split(/\r?\n/);
var numSteps = 0;
for (var i = 0; i < lines.length; i++)
{
var hash = lines[i].substring(0, 7);
var summary = lines[i].substring(8);
if (hash)
{
ret.steps[numSteps] = {'hash': hash, 'summary': summary};
numSteps++;
}
}
}
// Result to caller
process.nextTick(function() { callback(ret) });
});
};
/**
* Looking at the lockfile, get the current step of the symcode session.
*/
module.exports.getCurrentStep = function (repo, callback) {
fs.readFile('data/' + repo + '.lock', function(error, data) {
var ret = {};
if (error !== null)
{
ret = {'error': {'on': 'log', 'command': 'steps', 'message': error}};
}
else
{
ret = { 'step': data.toString('utf-8').trim() };
}
// Result to caller
process.nextTick(function() { callback(ret) });
});
};