forked from opista/svn-blamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubversion.js
70 lines (57 loc) · 2.21 KB
/
subversion.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
const vscode = require('vscode');
const path = require('path');
const child_process = require('child_process');
const formatDate = require('./functions/formatDate');
const _MAX_BUFFER = 1024 * 500; //500kb
const subversion = {
path: '',
name: '',
revisions: {},
init(editor) {
this.destroy();
this.path = editor.document.fileName.replace(/\$/g,'\\$');
this.name = path.basename(this.path);
if (this.path === this.name) return vscode.window.showInformationMessage('Blamer: Cannot identify file');
return this.blame();
},
destroy() {
this.path = '';
this.name = '';
this.revisions = {};
},
blame() {
return new Promise((resolve, reject) => {
const script = `svn blame -x "-w --ignore-eol-style" "${this.path}"`;
child_process.exec(script, {maxBuffer: _MAX_BUFFER}, (error, stdout, stderr) => {
if (error) { reject(stderr); }
const revisions = this.getRevisions(stdout);
resolve(revisions);
});
})
},
getRevisions(data) {
const lines = data.split(/\n/);
lines.forEach((line, index) => {
if (line.substring(5, 6) === '-') return;
const revision = line.split(' ').filter(s => s)[0];
if (revision) this.revisions[index] = parseInt(revision);
});
return this.revisions;
},
getLog(revision) {
if (Object.keys(this.revisions).length === 0) return;
return new Promise((resolve, reject) => {
const script = `svn log -r${revision} "${this.path}" --xml`;
child_process.exec(script, {maxBuffer: _MAX_BUFFER}, (error, stdout, stderr) => {
if (error) { reject(stderr); }
const revision = stdout.match(/revision="(.*)">/)[1];
const email = stdout.match(/<author>([^<]*)<\/author>/)[1];
const message = stdout.match(/<msg>([^<]*)<\/msg>/)[1];
let date = stdout.match(/<date>([^<]*)<\/date>/)[1];
date = formatDate(date);
resolve({ email, revision, date, message });
});
});
},
};
module.exports = subversion;