@arifbiswas/git-pre-push 1.0.20 → 1.0.21

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 CHANGED
@@ -36,7 +36,7 @@ That's it. Every git project on this computer is now protected.
36
36
 
37
37
  - Use `-g`. Without it GitSafty only lands inside one project and the `gitsafty` command does not exist. If you forget, GitSafty asks to install itself globally the first time it runs.
38
38
  - **Windows: `'gitsafty' is not recognized`?** Open a new terminal. Still not found? Add the folder shown by `npm config get prefix` to your `Path`.
39
- - After `init --global`, GitSafty offers one extra step: checking pulls that say *"Already up to date"* too. Answer `y` for full coverage.
39
+ - `init --global` also turns on the check for pulls that say *"Already up to date"*. Open a new terminal once so it takes effect.
40
40
 
41
41
  Protect only one project instead:
42
42
 
@@ -57,7 +57,7 @@ What do you want to do?
57
57
  Protect your code
58
58
  1. Protect this project Check every push, clone and pull in this project
59
59
  2. Protect all projects on this PC Same, for every git project on this computer
60
- 3. Scan every git pull Also when git says "Already up to date" (on/off)
60
+ 3. Scan every git pull On by default, also "Already up to date". Turn off/on
61
61
  4. Remove protection Uninstall GitSafty from this project or this PC
62
62
 
63
63
  Scan and clean
@@ -99,19 +99,21 @@ After `gitsafty init --global` you don't need to do anything. Use git as usual:
99
99
 
100
100
  When something is removed, you see what it was and where the backup is. Files inside a project are changed only in your working folder: check `git status`, then commit the clean version.
101
101
 
102
- ### Scan every pull
102
+ ### Scan every pull (on by default)
103
103
 
104
- Git runs nothing when a pull says *"Already up to date"*. To check those pulls too:
104
+ Git runs nothing when a pull says *"Already up to date"*, so GitSafty adds a small `git` helper to your shell profile (`.zshrc`, `.bashrc`, fish or PowerShell) that checks those pulls too. It is **on by default** with the global install, and existing installs get it after updating. Open a new terminal once so it takes effect.
105
+
106
+ Don't want it? Turn it off. GitSafty remembers this and never turns it back on by itself:
105
107
 
106
108
  ```bash
107
- gitsafty pull-scan on
109
+ gitsafty pull-scan off
108
110
  ```
109
111
 
110
112
  ```bash
111
- gitsafty pull-scan off
113
+ gitsafty pull-scan on
112
114
  ```
113
115
 
114
- Open a new terminal afterwards. Menu option **3** does the same.
116
+ Menu option **3** does the same. Pulls that bring new code are always checked, even with this off.
115
117
 
116
118
  ---
117
119
 
@@ -286,7 +288,7 @@ git config --global --remove-section credential.https://github.com
286
288
  | `gitsafty init --global` | Protect every project on this computer |
287
289
  | `gitsafty init` | Protect only this project |
288
290
  | `gitsafty uninstall` | Remove protection from this project (`--global` for the whole computer) |
289
- | `gitsafty pull-scan on` / `off` | Also check pulls that say "Already up to date" |
291
+ | `gitsafty pull-scan off` / `on` | Check pulls that say "Already up to date" (on by default) |
290
292
  | `gitsafty scan` | Scan this project and remove confirmed malware |
291
293
  | `gitsafty scan-projects <folder>` | Scan all projects in a folder (`--clean`, `--deep`, `--json`) |
292
294
  | `gitsafty full-scan <folder or file>` | Scan everything, including zip/rar files and non-git folders (`--clean`, `--json`) |
package/bin/cli.js CHANGED
@@ -23,29 +23,24 @@ const args = process.argv.slice(2);
23
23
  const command = args[0] ? args[0].toLowerCase() : 'menu';
24
24
  const isGlobalFlag = args.includes('--global') || args.includes('-g');
25
25
 
26
- /** After a global install: offer the shell function that scans every git pull. */
27
- async function offerPullScan() {
26
+ /** After a global install: pull scan is on by default (also "Already up to date" pulls). */
27
+ function enablePullScan() {
28
28
  const shell = require('../src/shellIntegration');
29
29
  if (shell.shellIntegrationStatus().installed.length) {
30
30
  shell.installShellIntegration(); // refresh with this version's paths
31
+ console.log(`${colors.green}✔ Every git pull is scanned${colors.reset} ${colors.dim}(also "Already up to date"). Turn off: gitsafty pull-scan off${colors.reset}\n`);
31
32
  return;
32
33
  }
33
- const interactive = process.stdin.isTTY && process.stdout.isTTY && !process.env.CI;
34
- console.log(`${colors.dim}git runs no hook when a pull is "Already up to date". To scan after EVERY git pull,${colors.reset}`);
35
- console.log(`${colors.dim}GitSafty can add a small git function to your shell profile (.zshrc / .bashrc / PowerShell profile).${colors.reset}`);
36
- if (!interactive) {
37
- console.log(` Turn it on: ${colors.cyan}gitsafty pull-scan on${colors.reset}\n`);
34
+ if (shell.isOptedOut()) {
35
+ console.log(`${colors.dim}Pull scan for "Already up to date" pulls is off (you turned it off). Turn on: gitsafty pull-scan on${colors.reset}\n`);
38
36
  return;
39
37
  }
40
- const readline = require('readline');
41
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
42
- const answer = await new Promise(r => rl.question(`${colors.bold}${colors.cyan}? Scan after every git pull? (Y/n): ${colors.reset}`, a => { rl.close(); r(a.trim()); }));
43
- if (/^n(o)?$/i.test(answer)) {
44
- console.log(` ${colors.dim}Skipped. Turn it on later: gitsafty pull-scan on${colors.reset}\n`);
45
- return;
38
+ const files = shell.enableByDefault();
39
+ if (files.length) {
40
+ console.log(`${colors.green}✔ Every git pull is scanned${colors.reset} ${colors.dim}(also "Already up to date"; added to ${files.join(', ')}).${colors.reset}`);
41
+ console.log(` ${colors.dim}Load it now:${colors.reset} ${colors.cyan}${shell.reloadHint()}${colors.reset}`);
42
+ console.log(` ${colors.dim}Turn off: gitsafty pull-scan off${colors.reset}\n`);
46
43
  }
47
- const res = shell.installShellIntegration();
48
- console.log(`${colors.green}✔ Pull scan on${colors.reset} ${colors.dim}(${res.files.join(', ')})${colors.reset}. Load it now: ${colors.cyan}${shell.reloadHint()}${colors.reset}\n`);
49
44
  }
50
45
 
51
46
  async function main() {
@@ -56,10 +51,19 @@ async function main() {
56
51
  if (command !== 'credential' && process.env.GITSAFTY_NO_SELF_REFRESH !== '1') {
57
52
  try {
58
53
  const refreshed = require('../src/installer').refreshOutdatedHooks();
59
- const shells = require('../src/shellIntegration').refreshShellIntegration();
54
+ const shell = require('../src/shellIntegration');
55
+ const shells = shell.refreshShellIntegration();
60
56
  if ((refreshed.length || shells.length) && !hookCommands.includes(command)) {
61
57
  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
58
  }
59
+ // Pull scan is on by default: existing global installs get it on update too
60
+ // (only from the real global copy, and never if the user turned it off).
61
+ if (require('../src/installer').isPermanentGlobalInstall()) {
62
+ const added = shell.enableByDefault({ onlyIfGlobalHooks: true });
63
+ if (added.length && !hookCommands.includes(command)) {
64
+ console.log(`${colors.green}✔ Every git pull is now scanned${colors.reset} ${colors.dim}(also "Already up to date"; ${added.map(f => require('path').basename(f)).join(', ')}). Open a new terminal. Turn off: gitsafty pull-scan off${colors.reset}`);
65
+ }
66
+ }
63
67
  } catch (_) {}
64
68
  }
65
69
 
@@ -210,14 +214,14 @@ async function main() {
210
214
  console.log(` Load it now: ${colors.cyan}${shell.reloadHint()}${colors.reset}\n`);
211
215
  }
212
216
  } else if (sub === 'off' || sub === 'remove' || sub === 'disable') {
213
- const res = shell.removeShellIntegration();
214
- console.log(`\n${colors.yellow}${colors.bold}✔ Pull scan shell function removed${res.files.length ? ` from ${res.files.join(', ')}` : ' (was not installed)'}.${colors.reset}`);
215
- console.log(` ${colors.dim}Open a new terminal to finish. Pulls that bring changes are still scanned by the git hooks.${colors.reset}\n`);
217
+ const res = shell.removeShellIntegration({ optOut: true });
218
+ console.log(`\n${colors.yellow}${colors.bold}✔ Pull scan turned off${res.files.length ? ` (removed from ${res.files.join(', ')})` : ''}. GitSafty will not turn it on again by itself.${colors.reset}`);
219
+ console.log(` ${colors.dim}Open a new terminal to finish. Pulls that bring changes are still scanned by the git hooks. Turn on again: gitsafty pull-scan on${colors.reset}\n`);
216
220
  } else {
217
221
  const st = shell.shellIntegrationStatus();
218
222
  console.log(st.installed.length
219
223
  ? `\n${colors.green}✔ Pull scan is ON${colors.reset} ${colors.dim}(${st.installed.join(', ')})${colors.reset}\n`
220
- : `\n${colors.yellow}Pull scan shell function is OFF.${colors.reset} Pulls that bring changes are still scanned by the git hooks.\n Turn on scanning for every pull: ${colors.cyan}gitsafty pull-scan on${colors.reset}\n`);
224
+ : `\n${colors.yellow}Pull scan is OFF${st.optedOut ? ' (you turned it off)' : ''}.${colors.reset} Pulls that bring changes are still scanned by the git hooks.\n Turn on scanning for every pull: ${colors.cyan}gitsafty pull-scan on${colors.reset}\n`);
221
225
  }
222
226
  break;
223
227
  }
@@ -340,6 +344,7 @@ async function main() {
340
344
  console.log(`\n${colors.green}${colors.bold}✔ ${result.message}${colors.reset}\n`);
341
345
  console.log(`${colors.cyan}Your git environment is now protected!${colors.reset}`);
342
346
  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`);
347
+ if (isGlobalFlag) enablePullScan();
343
348
  const { offerPermanentInstall } = require('../src/updateNotifier');
344
349
  // npx copy: re-run the same init globally. Project copy: protect the whole PC.
345
350
  await offerPermanentInstall(['init', '--global']);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arifbiswas/git-pre-push",
3
- "version": "1.0.20",
3
+ "version": "1.0.21",
4
4
  "description": "Zero-dependency pre-push security guard that stops malware, injected scripts, and leaked secrets from reaching GitHub.",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/doctor.js CHANGED
@@ -224,8 +224,10 @@ function runChecks({ cwd = process.cwd(), cliPath } = {}) {
224
224
  try {
225
225
  const shell = require('./shellIntegration');
226
226
  const st = shell.shellIntegrationStatus();
227
- if (!st.installed.length) {
228
- checks.push({ id: 'pull-scan', ok: true, level: 'warn', title: 'Pulls that are "Already up to date" are not scanned (git runs no hook then)', commands: ['gitsafty pull-scan on'] });
227
+ if (!st.installed.length && st.optedOut) {
228
+ checks.push({ id: 'pull-scan', ok: true, level: 'ok', title: 'Pull scan for "Already up to date" pulls is off (you turned it off)', commands: ['gitsafty pull-scan on'] });
229
+ } else if (!st.installed.length) {
230
+ checks.push({ id: 'pull-scan', ok: true, level: 'warn', title: 'Pulls that are "Already up to date" are not scanned (on by default, but not set up here)', commands: ['gitsafty pull-scan on'], fix: () => shell.installShellIntegration() });
229
231
  } else {
230
232
  checks.push({ id: 'pull-scan', ok: true, level: 'ok', title: `Every git pull is scanned (${st.installed.join(', ')})`, fix: () => shell.installShellIntegration() });
231
233
  }
package/src/menu.js CHANGED
@@ -34,7 +34,7 @@ async function runInteractiveMenu() {
34
34
  section('Protect your code');
35
35
  item(1, 'Protect this project', 'Check every push, clone and pull in this project');
36
36
  item(2, 'Protect all projects on this PC', 'Same, for every git project on this computer');
37
- item(3, 'Scan every git pull', 'Also when git says "Already up to date" (on/off)');
37
+ item(3, 'Scan every git pull', 'On by default, also "Already up to date". Turn off/on');
38
38
  item(4, 'Remove protection', 'Uninstall GitSafty from this project or this PC');
39
39
 
40
40
  section('Scan and clean');
@@ -87,6 +87,12 @@ async function runInteractiveMenu() {
87
87
  const res = installHook(process.cwd(), true);
88
88
  if (res.success) {
89
89
  console.log(`\n${colors.green}${colors.bold}✔ ${res.message}${colors.reset}\n`);
90
+ // Pull scan is on by default (unless the user turned it off before).
91
+ const shell = require('./shellIntegration');
92
+ const added = shell.enableByDefault();
93
+ if (added.length) {
94
+ console.log(`${colors.green}✔ Every git pull is scanned${colors.reset} ${colors.dim}(also "Already up to date"). Open a new terminal. Turn off: option 3${colors.reset}\n`);
95
+ }
90
96
  } else {
91
97
  console.error(`\n${colors.red}${colors.bold}✖ ${res.message}${colors.reset}\n`);
92
98
  }
@@ -253,7 +259,7 @@ async function runInteractiveMenu() {
253
259
  const answer = await ask(`\n${colors.bold}${colors.cyan}? Pull scan is ${on ? 'ON' : 'OFF'}. Turn it ${on ? 'off' : 'on'}? (y/N): ${colors.reset}`);
254
260
  if (/^y(es)?$/i.test(answer)) {
255
261
  if (on) {
256
- const r = shell.removeShellIntegration();
262
+ const r = shell.removeShellIntegration({ optOut: true });
257
263
  console.log(`\n${colors.yellow}✔ Turned off (${r.files.join(', ')}). Open a new terminal.${colors.reset}\n`);
258
264
  } else {
259
265
  const r = shell.installShellIntegration();
@@ -6,7 +6,10 @@
6
6
  * git, keeps its exit code, and after `git pull` runs `gitsafty scan-pull`
7
7
  * (which skips the scan when a hook already scanned the same commit).
8
8
  *
9
- * Installed only when the user agrees, between clear markers, in:
9
+ * On by default with the global install (`init --global`, and on update for
10
+ * existing global installs). `gitsafty pull-scan off` removes it and is
11
+ * remembered, so it is never added back by itself. It is written between
12
+ * clear markers, in:
10
13
  * macOS / Linux: the current shell's rc file (~/.zshrc, ~/.bashrc, fish config)
11
14
  * Windows: PowerShell profiles, and ~/.bashrc for Git Bash
12
15
  */
@@ -149,11 +152,21 @@ function stripBlock(text) {
149
152
  return text.replace(re, '\n').replace(/\n{3,}$/, '\n\n');
150
153
  }
151
154
 
155
+ // Written when the user turns pull scan off, so the default never turns it back on.
156
+ function optOutFile() {
157
+ return path.join(home(), '.gitsafty', 'pull-scan-off');
158
+ }
159
+
160
+ function isOptedOut() {
161
+ return fs.existsSync(optOutFile());
162
+ }
163
+
152
164
  /**
153
165
  * Adds (or refreshes) the git wrapper.
154
166
  * @returns {{ files: string[] }}
155
167
  */
156
168
  function installShellIntegration() {
169
+ try { fs.rmSync(optOutFile(), { force: true }); } catch (_) {}
157
170
  const files = [];
158
171
  for (const t of targets()) {
159
172
  const exists = fs.existsSync(t.file);
@@ -167,8 +180,18 @@ function installShellIntegration() {
167
180
  return { files };
168
181
  }
169
182
 
170
- /** Removes the git wrapper everywhere it was added. @returns {{ files: string[] }} */
171
- function removeShellIntegration() {
183
+ /**
184
+ * Removes the git wrapper everywhere it was added.
185
+ * @param {{ optOut?: boolean }} [opts] optOut: the user turned it off; remember that
186
+ * @returns {{ files: string[] }}
187
+ */
188
+ function removeShellIntegration({ optOut = false } = {}) {
189
+ if (optOut) {
190
+ try {
191
+ fs.mkdirSync(path.dirname(optOutFile()), { recursive: true });
192
+ fs.writeFileSync(optOutFile(), `Turned off on ${new Date().toISOString()}. Turn on again: gitsafty pull-scan on\n`);
193
+ } catch (_) {}
194
+ }
172
195
  const files = [];
173
196
  for (const t of targets()) {
174
197
  if (!fs.existsSync(t.file)) continue;
@@ -198,7 +221,29 @@ function refreshShellIntegration() {
198
221
 
199
222
  function shellIntegrationStatus() {
200
223
  const installed = targets().filter(t => fs.existsSync(t.file) && fs.readFileSync(t.file, 'utf8').includes(START)).map(t => t.file);
201
- return { installed, cliExists: fs.existsSync(cliPath()) };
224
+ return { installed, cliExists: fs.existsSync(cliPath()), optedOut: isOptedOut() };
225
+ }
226
+
227
+ /** True when GitSafty's global hooks are active on this PC. */
228
+ function globalHooksActive() {
229
+ const r = spawnSync('git', ['config', '--global', '--get', 'core.hooksPath'], { encoding: 'utf8' });
230
+ const value = (r.stdout || '').trim();
231
+ if (!value) return false;
232
+ const expanded = value.replace(/^~(?=$|[\\/])/, home());
233
+ return path.resolve(expanded) === path.resolve(home(), '.git-hooks-safty');
234
+ }
235
+
236
+ /**
237
+ * Pull scan is on by default for global installs. Turns it on unless the
238
+ * user turned it off before, it is already on, or this is CI.
239
+ * @param {{ onlyIfGlobalHooks?: boolean }} [opts]
240
+ * @returns {string[]} files it was added to (empty when nothing changed)
241
+ */
242
+ function enableByDefault({ onlyIfGlobalHooks = false } = {}) {
243
+ if (process.env.CI || process.env.GITSAFTY_NO_PULL_SCAN === '1' || isOptedOut()) return [];
244
+ if (shellIntegrationStatus().installed.length) return [];
245
+ if (onlyIfGlobalHooks && !globalHooksActive()) return [];
246
+ return installShellIntegration().files;
202
247
  }
203
248
 
204
249
  /** Command that loads the change in the current terminal. */
@@ -222,6 +267,8 @@ module.exports = {
222
267
  removeShellIntegration,
223
268
  refreshShellIntegration,
224
269
  shellIntegrationStatus,
270
+ enableByDefault,
271
+ isOptedOut,
225
272
  reloadHint,
226
273
  posixBlock,
227
274
  powershellBlock,