@arifbiswas/git-pre-push 1.0.19 → 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.
@@ -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,
package/src/utils/git.js CHANGED
@@ -101,10 +101,10 @@ function parsePrePushInput(stdinText) {
101
101
  */
102
102
  function getChangedFiles(diffRange, cwd = process.cwd()) {
103
103
  // Use git diff-tree or git log to extract changed filenames
104
- const output = runGit(`diff-tree --no-commit-id --name-only -r ${diffRange}`, cwd);
104
+ const output = runGit(`diff-tree --no-commit-id --name-only --diff-filter=d -r ${diffRange}`, cwd);
105
105
  if (!output) {
106
106
  // Fallback to git log
107
- const logOutput = runGit(`log ${diffRange} --name-only --oneline`, cwd);
107
+ const logOutput = runGit(`log ${diffRange} --name-only --oneline --diff-filter=d`, cwd);
108
108
  if (!logOutput) return [];
109
109
  const files = new Set();
110
110
  for (const line of logOutput.split('\n')) {