4 const Promise = require('bluebird');
5 const path = require('path');
6 const fs = require('fs').promises;
7 const ReadStream = require('fs').ReadStream;
8 const crypto = require('crypto');
10 function getHashForFilepath(filePath) {
11 return new Promise(resolve => {
12 var shasum = crypto.createHash('sha256');
14 const s = ReadStream(filePath);
15 s.on('data', function(d) {
18 s.on('end', function() {
19 const hash = shasum.digest('hex');
25 const hookSourcePath = path.resolve(__dirname, './security-harness-hook.sh');
26 const hookPath = path.resolve(__dirname, '../.git/hooks/pre-push');
31 await fs.stat(hookPath);
34 doesHookExist = false;
38 const sourceHash = await getHashForFilepath(hookSourcePath);
39 const destHash = await getHashForFilepath(hookPath);
41 if (sourceHash === destHash) {
42 await fs.unlink(hookPath);
43 console.log('Security harness removed -- you can now push to all remotes.');
45 console.log(`${hookPath} exists and is different from our hook!`);
46 console.log('Remove it and re-run this script to continue.');
51 const hookSource = await fs.readFile(hookSourcePath);
52 await fs.writeFile(hookPath, hookSource, {
56 console.log('Security harness installed -- you will only be able to push to dev.gitlab.org!');