//
// 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-7';
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 versionDir = {};
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
// store version directory in versionDir
function initVersionDir(dir) {
if (! (dir in versionDir)) {
try {
const stdout = child_process.execSync('cd "' + dir + '"; git remote -v 2> /dev/null');
if (stdout.indexOf('eecs482') < 0) {
versionDir[dir] = '';
} else {
versionDir[dir] = child_process.execSync('cd "' + dir + '"; git rev-parse --show-toplevel').toString().trimEnd() + path.sep + '.version482';
}
} catch (err) {
versionDir[dir] = '';
}
}
}
function initFile(doc) {
initVersionDir(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;
}
initVersionDir(path.dirname(doc.fileName));
// make sure file is in an EECS 482 git repo
if (versionDir[path.dirname(doc.fileName)] == '') {
return;
}
// make sure file is a program source file, i.e., has extension {cpp,cc,h,hpp,py}
const ext = path.extname(doc.fileName);
if (ext != '.cpp' && ext != '.cc' && ext != '.h' && ext != '.hpp' && ext != '.py') {
return;
}
// make sure file isn't too big
if (doc.getText().length > 10 * 1024 * 1024) {
return;
}
const dir = versionDir[path.dirname(doc.fileName)];
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;
const stdout = child_process.execSync('diff "' + priorname + '" "' + currentname + '"; rm "' + priorname + '" "' + currentname + '"').toString();
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();
}
}
| |