@arifbiswas/git-pre-push 1.0.16 → 1.0.18
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 +11 -1
- package/bin/cli.js +25 -1
- package/package.json +1 -1
- package/scripts/postinstall.js +6 -0
- package/src/installer.js +50 -0
- package/src/shellIntegration.js +17 -0
- package/src/updateNotifier.js +50 -8
package/README.md
CHANGED
|
@@ -30,6 +30,14 @@ Whenever `git push` is invoked—from terminal, VS Code UI, or any Git GUI—it
|
|
|
30
30
|
|
|
31
31
|
## 🚀 Quick Start
|
|
32
32
|
|
|
33
|
+
> **Install globally (`-g`).** `npm i @arifbiswas/git-pre-push` without `-g` only puts GitSafty into that project's `node_modules`: there is no `gitsafty` command and other repos stay unprotected. npm does not let a package install itself globally, so when GitSafty notices it is running from a project (or from `npx`) it asks *"Install GitSafty globally now? (Y/n)"* and does it for you. The recommended command is:
|
|
34
|
+
>
|
|
35
|
+
> ```bash
|
|
36
|
+
> npm install -g @arifbiswas/git-pre-push
|
|
37
|
+
> gitsafty init --global
|
|
38
|
+
> ```
|
|
39
|
+
|
|
40
|
+
|
|
33
41
|
### Option 1: Automatic Setup via npm (Recommended)
|
|
34
42
|
|
|
35
43
|
Add `git-pre-push` to your project:
|
|
@@ -100,7 +108,9 @@ Enter your choice (1-16, 0 to exit):
|
|
|
100
108
|
|
|
101
109
|
---
|
|
102
110
|
|
|
103
|
-
**Updates:**
|
|
111
|
+
**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).
|
|
112
|
+
|
|
113
|
+
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
114
|
|
|
105
115
|
### Option 3: Lock Down an Entire Machine (Global Protection)
|
|
106
116
|
|
package/bin/cli.js
CHANGED
|
@@ -51,6 +51,29 @@ 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
|
+
|
|
66
|
+
// Installed as a project dependency (`npm i` without -g)? Offer the global
|
|
67
|
+
// install once per command run (remembered for 30 days when declined).
|
|
68
|
+
if (!hookCommands.includes(command) && command !== 'init' && command !== 'install') {
|
|
69
|
+
try {
|
|
70
|
+
const un = require('../src/updateNotifier');
|
|
71
|
+
if (un.looksLikeProjectCopy() && un.installKind() === 'local-dependency') {
|
|
72
|
+
await un.offerPermanentInstall(['init', '--global'], { kind: 'local-dependency', remember: true });
|
|
73
|
+
}
|
|
74
|
+
} catch (_) {}
|
|
75
|
+
}
|
|
76
|
+
|
|
54
77
|
// `gitsafty update` does its own check below.
|
|
55
78
|
if (!hookCommands.includes(command) && command !== 'update' && command !== 'upgrade') {
|
|
56
79
|
// Opening the menu always asks the registry, so a fresh release is never missed.
|
|
@@ -305,7 +328,8 @@ async function main() {
|
|
|
305
328
|
console.log(`${colors.cyan}Your git environment is now protected!${colors.reset}`);
|
|
306
329
|
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
330
|
const { offerPermanentInstall } = require('../src/updateNotifier');
|
|
308
|
-
|
|
331
|
+
// npx copy: re-run the same init globally. Project copy: protect the whole PC.
|
|
332
|
+
await offerPermanentInstall(['init', '--global']);
|
|
309
333
|
} else {
|
|
310
334
|
console.error(`\n${colors.red}${colors.bold}✖ ${result.message}${colors.reset}\n`);
|
|
311
335
|
process.exit(1);
|
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -20,6 +20,12 @@ function main() {
|
|
|
20
20
|
console.log(`[git-safty] 🛡️ Global hooks updated (push + clone + pull malware scan) in ${refreshed}`);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
if (!isGlobalInstall) {
|
|
24
|
+
// npm can't make a package install globally on its own; say it clearly.
|
|
25
|
+
console.log('[git-safty] Installed in this project only. To protect every repo on this PC and get the `gitsafty` command:');
|
|
26
|
+
console.log('[git-safty] npm install -g @arifbiswas/git-pre-push && gitsafty init --global');
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
// When installed as a dependency, process.env.INIT_CWD points to the parent project
|
|
24
30
|
const targetDir = process.env.INIT_CWD || process.cwd();
|
|
25
31
|
|
package/src/installer.js
CHANGED
|
@@ -339,9 +339,59 @@ 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
|
+
// unix prefix (nvm, Homebrew, /usr/local), Windows npm prefix (AppData/Roaming/npm),
|
|
352
|
+
// Program Files/nodejs, nvm-windows (AppData/Roaming/nvm/v20.x/node_modules).
|
|
353
|
+
/\/lib\/node_modules\/|\/npm\/node_modules\/|\/nodejs\/node_modules\/|\/nvm\/v[0-9][^/]*\/node_modules\//i.test(cli);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Brings the global hooks up to date with THIS version, but only the ones
|
|
358
|
+
* that differ. Runs on every GitSafty command, because newer npm versions
|
|
359
|
+
* block install scripts (postinstall), so an update alone can't refresh them.
|
|
360
|
+
* @returns {string[]} hook types that were rewritten (empty when up to date)
|
|
361
|
+
*/
|
|
362
|
+
function refreshOutdatedHooks() {
|
|
363
|
+
const homeDir = process.env.HOME || process.env.USERPROFILE || '';
|
|
364
|
+
const globalDir = path.join(homeDir, '.git-hooks-safty');
|
|
365
|
+
const updated = [];
|
|
366
|
+
try {
|
|
367
|
+
const prePush = path.join(globalDir, 'pre-push');
|
|
368
|
+
if (!fs.existsSync(prePush)) return updated;
|
|
369
|
+
const prePushText = fs.readFileSync(prePush, 'utf8');
|
|
370
|
+
if (!prePushText.includes(HOOK_MARKER)) return updated;
|
|
371
|
+
|
|
372
|
+
// Only touch hooks that already belong to this copy, or move them to this
|
|
373
|
+
// copy when it is a real global npm install. Never point them at an npx
|
|
374
|
+
// cache or a source checkout someone happens to run.
|
|
375
|
+
const m = prePushText.match(/^GITSAFTY_CLI='([^']*)'/m);
|
|
376
|
+
const hookCli = m ? m[1] : null;
|
|
377
|
+
if (hookCli !== CLI_PATH && !isPermanentGlobalInstall()) return updated;
|
|
378
|
+
|
|
379
|
+
for (const type of HOOK_TYPES) {
|
|
380
|
+
const file = path.join(globalDir, type);
|
|
381
|
+
const current = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
|
|
382
|
+
if (current.includes(generateHookContent(type).trim())) continue;
|
|
383
|
+
writeHook(globalDir, type);
|
|
384
|
+
updated.push(type);
|
|
385
|
+
}
|
|
386
|
+
} catch (_) {}
|
|
387
|
+
return updated;
|
|
388
|
+
}
|
|
389
|
+
|
|
342
390
|
module.exports = {
|
|
343
391
|
installHook,
|
|
344
392
|
uninstallHook,
|
|
393
|
+
refreshOutdatedHooks,
|
|
394
|
+
isPermanentGlobalInstall,
|
|
345
395
|
uninstallGlobalHook,
|
|
346
396
|
refreshInstalledHooks,
|
|
347
397
|
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,
|
package/src/updateNotifier.js
CHANGED
|
@@ -275,17 +275,44 @@ function globalCliPath() {
|
|
|
275
275
|
* re-runs init with the permanent copy so the hooks point there.
|
|
276
276
|
* @param {string[]} initArgs e.g. ['init', '--global']
|
|
277
277
|
*/
|
|
278
|
-
|
|
279
|
-
|
|
278
|
+
/**
|
|
279
|
+
* Offers to install GitSafty globally when this copy is not a global install:
|
|
280
|
+
* - npx: runs from a temporary cache, no `gitsafty` command, hooks point
|
|
281
|
+
* into that cache (which npm may delete)
|
|
282
|
+
* - project dependency (`npm i @arifbiswas/git-pre-push` without -g):
|
|
283
|
+
* only this project has it, no `gitsafty` command, other repos unprotected
|
|
284
|
+
* npm can't make a package install globally by itself, so GitSafty asks.
|
|
285
|
+
* Yes runs `npm install -g` and then `init` with the global copy.
|
|
286
|
+
* @param {string[]} initArgs init arguments to re-run with the global copy
|
|
287
|
+
* @param {{ kind?: string, remember?: boolean }} [opts]
|
|
288
|
+
*/
|
|
289
|
+
async function offerPermanentInstall(initArgs, opts = {}) {
|
|
290
|
+
const kind = opts.kind || installKind();
|
|
291
|
+
if (kind !== 'npx' && kind !== 'local-dependency') return;
|
|
280
292
|
const isWin = process.platform === 'win32';
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
293
|
+
const cache = readCache() || {};
|
|
294
|
+
const declinedRecently = opts.remember && cache.declinedGlobalAt && Date.now() - cache.declinedGlobalAt < 30 * 24 * 60 * 60 * 1000;
|
|
295
|
+
if (declinedRecently) return;
|
|
296
|
+
|
|
297
|
+
if (kind === 'npx') {
|
|
298
|
+
console.log(`${colors.yellow}${colors.bold}⚠ You ran GitSafty with npx.${colors.reset}`);
|
|
299
|
+
console.log(` npx only runs it from a temporary cache: the ${colors.bold}gitsafty${colors.reset} command is not installed,`);
|
|
300
|
+
console.log(` and the hooks point into that cache, which npm may delete later.`);
|
|
301
|
+
} else {
|
|
302
|
+
console.log(`${colors.yellow}${colors.bold}⚠ GitSafty is installed in this project only${colors.reset} ${colors.dim}(${path.resolve(__dirname, '..')})${colors.reset}`);
|
|
303
|
+
console.log(` \`npm i\` without -g puts it in node_modules: no ${colors.bold}gitsafty${colors.reset} command, and other repos on this PC are not protected.`);
|
|
304
|
+
}
|
|
305
|
+
console.log(` ${colors.dim}Install it for the whole PC:${colors.reset} ${colors.cyan}npm install -g ${PACKAGE_NAME}${colors.reset} ${colors.dim}then${colors.reset} ${colors.cyan}gitsafty ${initArgs.join(' ')}${colors.reset}\n`);
|
|
285
306
|
|
|
286
307
|
if (!isInteractive() || process.env.GITSAFTY_NO_UPDATE_PROMPT === '1') return;
|
|
287
|
-
const yes = await askYesNo(`${colors.bold}${colors.cyan}? Install GitSafty
|
|
288
|
-
if (!yes)
|
|
308
|
+
const yes = await askYesNo(`${colors.bold}${colors.cyan}? Install GitSafty globally now (gitsafty command + protect every repo)? (Y/n): ${colors.reset}`);
|
|
309
|
+
if (!yes) {
|
|
310
|
+
if (opts.remember) {
|
|
311
|
+
writeCache(cache.latestVersion || null, { declinedGlobalAt: Date.now() });
|
|
312
|
+
console.log(`${colors.dim}OK, I won't ask again for 30 days.${colors.reset}\n`);
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
289
316
|
if (!installVersion('latest')) return;
|
|
290
317
|
|
|
291
318
|
const cli = globalCliPath();
|
|
@@ -303,9 +330,24 @@ async function offerPermanentInstall(initArgs) {
|
|
|
303
330
|
console.log('');
|
|
304
331
|
}
|
|
305
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Fast check (no npm call) whether this copy might be a project dependency.
|
|
335
|
+
* Confirmed with installKind() before asking anything.
|
|
336
|
+
*/
|
|
337
|
+
function looksLikeProjectCopy() {
|
|
338
|
+
const cli = path.resolve(__dirname, '..', 'bin', 'cli.js').replace(/\\/g, '/');
|
|
339
|
+
if (!/\/node_modules\/@arifbiswas\/git-pre-push\/bin\/cli\.js$/.test(cli) || /\/_npx\//.test(cli)) return false;
|
|
340
|
+
try {
|
|
341
|
+
return !require('./installer').isPermanentGlobalInstall(cli);
|
|
342
|
+
} catch (_) {
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
306
347
|
module.exports = {
|
|
307
348
|
isNewerVersion,
|
|
308
349
|
offerPermanentInstall,
|
|
350
|
+
looksLikeProjectCopy,
|
|
309
351
|
globalCliPath,
|
|
310
352
|
installKind,
|
|
311
353
|
offerUpdate,
|