-
Notifications
You must be signed in to change notification settings - Fork 8
/
libgit2_webworker.js
114 lines (100 loc) · 3.77 KB
/
libgit2_webworker.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var Module = {
locateFile: function(s) {
return 'https://unpkg.com/[email protected]/' + s;
},
'print': function(text) {
console.log(text);
postMessage({'stdout': text});
},
'printErr': function(text) {
console.error(text);
postMessage({'stderr': text});
}
};
let accessToken = 'ANONYMOUS';
XMLHttpRequest.prototype._open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
this._open(method, url, async, user, password);
this.setRequestHeader('Authorization', `Bearer ${accessToken}`);
}
importScripts('https://unpkg.com/[email protected]/lg2.js');
function writeGlobalConfig(username, useremail) {
FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
`name = ${username}\n` +
`email = ${useremail}`);
}
const lgPromise = new Promise(resolve => {
Module.onRuntimeInitialized = () => {
writeGlobalConfig('Test user', '[email protected]');
resolve(Module);
}
});
let currentRepoRootDir;
onmessage = async (msg) => {
const lg = await lgPromise;
if (msg.data.accessToken) {
accessToken = msg.data.accessToken;
writeGlobalConfig(msg.data.username, msg.data.useremail);
postMessage({accessTokenConfigured: true})
} else if (msg.data.command === 'writeandcommit') {
FS.writeFile(msg.data.filename, msg.data.contents);
lg.callMain(['add', '--verbose', msg.data.filename]);
lg.callMain(['commit','-m', msg.data.commitmessage]);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
postMessage({ dircontents: FS.readdir('.') });
});
} else if (msg.data.command === 'push') {
lg.callMain(['push']);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
postMessage({ dircontents: FS.readdir('.') });
});
} else if (msg.data.command === 'synclocal') {
currentRepoRootDir = msg.data.url.substring(msg.data.url.lastIndexOf('/') + 1);
console.log('synclocal', currentRepoRootDir);
FS.mkdir(`/${currentRepoRootDir}`);
FS.mount(IDBFS, { }, `/${currentRepoRootDir}`);
FS.syncfs(true, () => {
if( FS.readdir(`/${currentRepoRootDir}`).find(file => file === '.git') ) {
FS.chdir( `/${currentRepoRootDir}` );
postMessage({ dircontents: FS.readdir('.') });
console.log(currentRepoRootDir, 'restored from indexeddb');
} else {
FS.chdir( '/' );
console.log('no git repo in', currentRepoRootDir);
}
});
} else if (msg.data.command === 'deletelocal') {
FS.unmount(`/${currentRepoRootDir}`);
console.log('deleting database', currentRepoRootDir);
self.indexedDB.deleteDatabase('/' + currentRepoRootDir);
postMessage({ deleted: currentRepoRootDir});
} else if (msg.data.command === 'clone') {
currentRepoRootDir = msg.data.url.substring(msg.data.url.lastIndexOf('/') + 1);
postMessage({stdout: `git clone ${msg.data.url}`});
lg.callMain(['clone', msg.data.url, currentRepoRootDir]);
FS.chdir(currentRepoRootDir);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
});
postMessage({ dircontents: FS.readdir('.') });
} else if (msg.data.command === 'pull') {
lg.callMain(['fetch', 'origin']);
lg.callMain(['merge', 'origin/master']);
FS.syncfs(false, () => {
console.log(currentRepoRootDir, 'stored to indexeddb');
});
} else if (msg.data.command === 'readfile') {
try {
postMessage({
filename: msg.data.filename,
filecontents: FS.readFile(msg.data.filename, {encoding: 'utf8'})
});
} catch (e) {
postMessage({'stderr': JSON.stringify(e)});
}
} else {
lg.callMain([msg.data.command]);
}
};