//
// Keep versions of files in EECS 482 github repos
//
const vscode = require('vscode');
const os = require('os');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const child_process = require('child_process');
const version = 'vscode-1';
const minTimeInterval = 1; // minimum time between versions
let prior = {};
let priorTime = {};
let timer = -1;
const username = os.userInfo().username;
const sessionStart = time().toString();
let hashInitial = crypto.createHash('sha256').update(username + ' ' + sessionStart);
let hash = {};
let github = {};
exports.activate = () => {
vscode.workspace.textDocuments.forEach(doc => initFile(doc));
vscode.workspace.onDidOpenTextDocument(doc => {
initFile(doc);
});
vscode.workspace.onDidChangeTextDocument(e => {
textChanged(e.document);
});
}
// return current time in seconds
function time() {
return(Math.trunc(Date.now()/1000));
}
// see if directory is in an EECS 482 git repo
function checkGithub(dir) {
if (! (dir in github)) {
child_process.exec('cd "' + dir + '"; git remote -v',
(err, stdout, stderr) => {
github[dir] = (stdout.indexOf('git@github.com:eecs482') >= 0);
});
}
}
function initFile(doc) {
checkGithub(path.dirname(doc.fileName));
}
function textChanged(doc) {
// limit the rate of versioning events
if (doc.fileName in priorTime
&& time() - priorTime[doc.fileName] < minTimeInterval) {
// make sure this version is eventually saved
if (timer >= 0) {
clearTimeout(timer);
}
// replace any pending timer event, so these don't pile up
timer = setTimeout(textChanged, minTimeInterval, doc);
return;
}
checkGithub(path.dirname(doc.fileName));
// make sure file is in an EECS 482 git repo
if (! github[path.dirname(doc.fileName)]) {
return;
}
// make sure file isn't too big
if (doc.getText().length > 10 * 1024 * 1024) {
return;
}
const dir = path.dirname(doc.fileName) + path.sep + '.version482';
try {
if (! fs.statSync(dir).isDirectory) {
return;
}
} catch (err) {
fs.mkdirSync(dir);
}
if (! (dir in hash)) {
hash[dir] = hashInitial.copy();
}
if (! (doc.fileName in prior)) {
prior[doc.fileName] = '';
}
const priorname = dir + path.sep + sessionStart + '.' + username + '.prior';
const currentname = dir + path.sep + sessionStart + '.' + username + '.current';
fs.writeFileSync(priorname, prior[doc.fileName]);
const current = doc.getText();
fs.writeFileSync(currentname, current);
const versionfile = dir + path.sep + sessionStart + '.' + username;
child_process.exec('diff "' + priorname + '" "' + currentname + '"; rm "' + priorname + '" "' + currentname + '"',
(err, stdout, stderr) => {
if (stdout != '') {
const line = version + ' ' + time() + ' '
+ hash[dir].copy().digest('hex') + ' '
+ doc.fileName + ' '
+ JSON.stringify(stdout);
try {
fs.appendFileSync(versionfile, line + "\n", () => {});
} catch (err) {
return;
}
hash[dir] = crypto.createHash('sha256').update(line);
prior[doc.fileName] = current;
priorTime[doc.fileName] = time();
}
});
}
| |