@arifbiswas/git-pre-push 1.0.15 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/bin/cli.js +12 -1
- package/package.json +1 -1
- package/src/installer.js +48 -0
- package/src/shellIntegration.js +17 -0
package/README.md
CHANGED
|
@@ -100,7 +100,9 @@ Enter your choice (1-16, 0 to exit):
|
|
|
100
100
|
|
|
101
101
|
---
|
|
102
102
|
|
|
103
|
-
**Updates:**
|
|
103
|
+
**Updates:** npm 11+ may print `install scripts not yet covered by allowScripts` after `npm install -g`. The update itself worked; only GitSafty's postinstall step was skipped. That is fine: the next time any `gitsafty` command or git hook runs, GitSafty brings your global hooks and the pull-scan shell function up to date by itself (only from a real global install, never from npx or a source checkout).
|
|
104
|
+
|
|
105
|
+
Every time you open `gitsafty` it checks npm. When a new version is out it asks `Install GitSafty x.y.z now? (Y/n)`; answer `y` and it installs and reopens the new version (global installs only; never inside git hooks or CI). Turn the question off with `GITSAFTY_NO_UPDATE_PROMPT=1`.
|
|
104
106
|
|
|
105
107
|
### Option 3: Lock Down an Entire Machine (Global Protection)
|
|
106
108
|
|
package/bin/cli.js
CHANGED
|
@@ -51,6 +51,18 @@ async function offerPullScan() {
|
|
|
51
51
|
async function main() {
|
|
52
52
|
// Check for updates non-blocking on interactive commands
|
|
53
53
|
const hookCommands = ['scan-push', 'scan-checkout', 'scan-merge', 'scan-rewrite', 'scan-pull', 'credential'];
|
|
54
|
+
// Keep the global hooks and the pull-scan shell function in step with this
|
|
55
|
+
// version. npm 11+ blocks postinstall scripts, so updates can't do it.
|
|
56
|
+
if (command !== 'credential' && process.env.GITSAFTY_NO_SELF_REFRESH !== '1') {
|
|
57
|
+
try {
|
|
58
|
+
const refreshed = require('../src/installer').refreshOutdatedHooks();
|
|
59
|
+
const shells = require('../src/shellIntegration').refreshShellIntegration();
|
|
60
|
+
if ((refreshed.length || shells.length) && !hookCommands.includes(command)) {
|
|
61
|
+
console.log(`${colors.green}✔ GitSafty ${pkg.version}: updated ${[...refreshed.map(t => `${t} hook`), ...shells.map(f => require('path').basename(f))].join(', ')} to this version.${colors.reset}`);
|
|
62
|
+
}
|
|
63
|
+
} catch (_) {}
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
// `gitsafty update` does its own check below.
|
|
55
67
|
if (!hookCommands.includes(command) && command !== 'update' && command !== 'upgrade') {
|
|
56
68
|
// Opening the menu always asks the registry, so a fresh release is never missed.
|
|
@@ -304,7 +316,6 @@ async function main() {
|
|
|
304
316
|
console.log(`\n${colors.green}${colors.bold}✔ ${result.message}${colors.reset}\n`);
|
|
305
317
|
console.log(`${colors.cyan}Your git environment is now protected!${colors.reset}`);
|
|
306
318
|
console.log(`${colors.dim}Every git push, clone and pull${isGlobalFlag ? ' on this PC' : ' in this repository'} is scanned for malware & secret leaks.${colors.reset}\n`);
|
|
307
|
-
if (isGlobalFlag) await offerPullScan();
|
|
308
319
|
const { offerPermanentInstall } = require('../src/updateNotifier');
|
|
309
320
|
await offerPermanentInstall(['init', ...(isGlobalFlag ? ['--global'] : [])]);
|
|
310
321
|
} else {
|
package/package.json
CHANGED
package/src/installer.js
CHANGED
|
@@ -339,9 +339,57 @@ function refreshInstalledHooks() {
|
|
|
339
339
|
return null;
|
|
340
340
|
}
|
|
341
341
|
|
|
342
|
+
/**
|
|
343
|
+
* True when this copy of GitSafty is a real global npm install (not an npx
|
|
344
|
+
* cache, a project dependency or a source checkout). Only such a copy may
|
|
345
|
+
* take over hooks / shell functions that point somewhere else.
|
|
346
|
+
*/
|
|
347
|
+
function isPermanentGlobalInstall(cli = CLI_PATH) {
|
|
348
|
+
return /\/node_modules\/@arifbiswas\/git-pre-push\/bin\/cli\.js$/.test(cli) &&
|
|
349
|
+
!/\/_npx\//.test(cli) &&
|
|
350
|
+
!/\/node_modules\/.+\/node_modules\//.test(cli) &&
|
|
351
|
+
/\/lib\/node_modules\/|\/npm\/node_modules\/|\/nodejs\/node_modules\/|^[A-Za-z]:\/.*\/node_modules\//.test(cli);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Brings the global hooks up to date with THIS version, but only the ones
|
|
356
|
+
* that differ. Runs on every GitSafty command, because newer npm versions
|
|
357
|
+
* block install scripts (postinstall), so an update alone can't refresh them.
|
|
358
|
+
* @returns {string[]} hook types that were rewritten (empty when up to date)
|
|
359
|
+
*/
|
|
360
|
+
function refreshOutdatedHooks() {
|
|
361
|
+
const homeDir = process.env.HOME || process.env.USERPROFILE || '';
|
|
362
|
+
const globalDir = path.join(homeDir, '.git-hooks-safty');
|
|
363
|
+
const updated = [];
|
|
364
|
+
try {
|
|
365
|
+
const prePush = path.join(globalDir, 'pre-push');
|
|
366
|
+
if (!fs.existsSync(prePush)) return updated;
|
|
367
|
+
const prePushText = fs.readFileSync(prePush, 'utf8');
|
|
368
|
+
if (!prePushText.includes(HOOK_MARKER)) return updated;
|
|
369
|
+
|
|
370
|
+
// Only touch hooks that already belong to this copy, or move them to this
|
|
371
|
+
// copy when it is a real global npm install. Never point them at an npx
|
|
372
|
+
// cache or a source checkout someone happens to run.
|
|
373
|
+
const m = prePushText.match(/^GITSAFTY_CLI='([^']*)'/m);
|
|
374
|
+
const hookCli = m ? m[1] : null;
|
|
375
|
+
if (hookCli !== CLI_PATH && !isPermanentGlobalInstall()) return updated;
|
|
376
|
+
|
|
377
|
+
for (const type of HOOK_TYPES) {
|
|
378
|
+
const file = path.join(globalDir, type);
|
|
379
|
+
const current = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
|
|
380
|
+
if (current.includes(generateHookContent(type).trim())) continue;
|
|
381
|
+
writeHook(globalDir, type);
|
|
382
|
+
updated.push(type);
|
|
383
|
+
}
|
|
384
|
+
} catch (_) {}
|
|
385
|
+
return updated;
|
|
386
|
+
}
|
|
387
|
+
|
|
342
388
|
module.exports = {
|
|
343
389
|
installHook,
|
|
344
390
|
uninstallHook,
|
|
391
|
+
refreshOutdatedHooks,
|
|
392
|
+
isPermanentGlobalInstall,
|
|
345
393
|
uninstallGlobalHook,
|
|
346
394
|
refreshInstalledHooks,
|
|
347
395
|
generateHookContent,
|
package/src/shellIntegration.js
CHANGED
|
@@ -180,6 +180,22 @@ function removeShellIntegration() {
|
|
|
180
180
|
return { files };
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
/** Re-writes installed blocks that are from another version. @returns {string[]} files updated */
|
|
184
|
+
function refreshShellIntegration() {
|
|
185
|
+
const updated = [];
|
|
186
|
+
for (const t of targets()) {
|
|
187
|
+
if (!fs.existsSync(t.file)) continue;
|
|
188
|
+
const text = fs.readFileSync(t.file, 'utf8');
|
|
189
|
+
if (!text.includes(START) || text.includes(blockFor(t.kind).trim())) continue;
|
|
190
|
+
// Same rule as the hooks: only this copy's own block, or a real global install may take it over.
|
|
191
|
+
const ownBlock = text.includes(cliPath()) || text.includes(cliPath().replace(/'/g, "''"));
|
|
192
|
+
if (!ownBlock && !require('./installer').isPermanentGlobalInstall(cliPath())) continue;
|
|
193
|
+
fs.writeFileSync(t.file, stripBlock(text).replace(/\s*$/, '') + '\n\n' + blockFor(t.kind));
|
|
194
|
+
updated.push(t.file);
|
|
195
|
+
}
|
|
196
|
+
return updated;
|
|
197
|
+
}
|
|
198
|
+
|
|
183
199
|
function shellIntegrationStatus() {
|
|
184
200
|
const installed = targets().filter(t => fs.existsSync(t.file) && fs.readFileSync(t.file, 'utf8').includes(START)).map(t => t.file);
|
|
185
201
|
return { installed, cliExists: fs.existsSync(cliPath()) };
|
|
@@ -204,6 +220,7 @@ module.exports = {
|
|
|
204
220
|
END,
|
|
205
221
|
installShellIntegration,
|
|
206
222
|
removeShellIntegration,
|
|
223
|
+
refreshShellIntegration,
|
|
207
224
|
shellIntegrationStatus,
|
|
208
225
|
reloadHint,
|
|
209
226
|
posixBlock,
|