@pgpmjs/core 7.24.0 → 7.25.0

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.
@@ -1,5 +1,7 @@
1
1
  import { verifyBundle } from '@pgpmjs/bundle';
2
- import { execSync } from 'child_process';
2
+ import { execFileSync } from 'child_process';
3
+ import { existsSync } from 'fs';
4
+ import { changedFiles as gitChangedFiles, resolveBase as resolveChangedBase } from 'git-changed';
3
5
  import { isAbsolute, relative, resolve as resolvePath } from 'path';
4
6
  import { bundleMatchesModule, readBundleArtifact, resolveBundleArtifactPath, } from '../bundle/artifact';
5
7
  import { PgpmPackage } from '../core/class/pgpm';
@@ -48,63 +50,98 @@ export function checkModuleArtifact(moduleDir, name) {
48
50
  }
49
51
  return null;
50
52
  }
51
- function git(args, cwd) {
52
- return execSync(`git ${args}`, {
53
- cwd,
54
- encoding: 'utf-8',
55
- stdio: ['ignore', 'pipe', 'ignore'],
56
- maxBuffer: 64 * 1024 * 1024,
57
- });
58
- }
59
53
  /**
60
- * Resolve the git ref to diff against. Explicit `--since` wins; otherwise use
61
- * the PR base branch when running in GitHub Actions; otherwise `undefined`
62
- * (working-tree changes only).
54
+ * Resolve the git ref to diff against. Explicit `--since` wins; otherwise the
55
+ * PR base branch when running in GitHub Actions; otherwise the repository's
56
+ * default branch. `undefined` means there is nothing to diff against — a
57
+ * shallow or detached checkout — and only the working tree is inspected.
63
58
  */
64
- export function resolveBase(since) {
65
- if (since)
66
- return since;
67
- const prBase = process.env.GITHUB_BASE_REF;
68
- if (prBase && prBase.trim())
69
- return `origin/${prBase.trim()}`;
70
- return undefined;
59
+ export function resolveBase(since, cwd = process.cwd()) {
60
+ return resolveChangedBase(since, cwd);
71
61
  }
72
62
  /**
73
- * Collect the set of changed files, combining committed changes since `base`
74
- * (three-dot diff, so it works with branches, refs, and tags) with any
75
- * uncommitted/untracked changes in the working tree. Returns absolute paths.
63
+ * Collect the set of changed files: committed changes since the **merge base**
64
+ * with `base`, unioned with uncommitted and untracked working-tree changes.
65
+ *
66
+ * Returns absolute paths, including paths that no longer exist — deleting a
67
+ * `deploy/` file makes a module's bundle stale exactly as much as editing one,
68
+ * so the deletion has to reach {@link mapFilesToModules}.
76
69
  */
77
70
  export function changedFiles(cwd, base) {
78
- const files = new Set();
79
- const status = git('status --porcelain', cwd);
80
- for (const rawLine of status.split('\n')) {
81
- const line = rawLine.trimEnd();
82
- if (!line)
71
+ // `base: false` when there is no base: the ref is resolved by the caller, and
72
+ // git-changed would otherwise resolve one of its own.
73
+ return gitChangedFiles({ cwd, base: base ?? false, existingOnly: false }).paths.map(decodeGitPath);
74
+ }
75
+ /**
76
+ * Undo git's C-escaping of a path it decided to quote.
77
+ *
78
+ * With `core.quotepath` on (the default) git renders a path holding a non-ASCII
79
+ * byte as `"caf\303\251 dir/x.sql"`: octal escapes, one per UTF-8 byte. A caller
80
+ * that only strips the surrounding quotes is left with a filename that exists
81
+ * nowhere on disk, so the module owning it silently drops out of the check —
82
+ * `pgpm package --check` passes while the module's bundle is stale.
83
+ *
84
+ * Only applied when it actually resolves something: if the escaped form exists on
85
+ * disk and the decoded one does not, the backslashes were part of the filename.
86
+ */
87
+ function decodeGitPath(path) {
88
+ if (!path.includes('\\'))
89
+ return path;
90
+ const decoded = cUnescape(path);
91
+ if (decoded === path)
92
+ return path;
93
+ if (existsSync(decoded))
94
+ return decoded;
95
+ // Neither form exists for a deleted file; the decoded name is the real one.
96
+ return existsSync(path) ? path : decoded;
97
+ }
98
+ const C_ESCAPES = {
99
+ a: 0x07,
100
+ b: 0x08,
101
+ f: 0x0c,
102
+ n: 0x0a,
103
+ r: 0x0d,
104
+ t: 0x09,
105
+ v: 0x0b,
106
+ '\\': 0x5c,
107
+ '"': 0x22,
108
+ };
109
+ /** Decode `\NNN` octal byte escapes and the single-character C escapes git emits. */
110
+ function cUnescape(input) {
111
+ const bytes = [];
112
+ for (let i = 0; i < input.length; i += 1) {
113
+ const ch = input[i];
114
+ if (ch !== '\\' || i + 1 >= input.length) {
115
+ bytes.push(...Buffer.from(ch, 'utf8'));
83
116
  continue;
84
- let p = line.slice(3);
85
- const arrow = p.indexOf(' -> ');
86
- if (arrow !== -1)
87
- p = p.slice(arrow + 4);
88
- p = p.replace(/^"|"$/g, '');
89
- if (p)
90
- files.add(resolvePath(cwd, p));
91
- }
92
- if (base) {
93
- let diff;
94
- try {
95
- diff = git(`diff --name-only ${base}...HEAD`, cwd);
96
117
  }
97
- catch (err) {
98
- throw new Error(`Could not diff against '${base}'. Ensure the ref exists locally ` +
99
- `(e.g. \`git fetch origin\`). Original error: ${err instanceof Error ? err.message : String(err)}`);
118
+ const next = input[i + 1];
119
+ const octal = input.slice(i + 1, i + 4);
120
+ if (/^[0-7]{3}$/.test(octal)) {
121
+ bytes.push(parseInt(octal, 8));
122
+ i += 3;
123
+ continue;
100
124
  }
101
- for (const rawLine of diff.split('\n')) {
102
- const p = rawLine.trim();
103
- if (p)
104
- files.add(resolvePath(cwd, p));
125
+ if (next in C_ESCAPES) {
126
+ bytes.push(C_ESCAPES[next]);
127
+ i += 1;
128
+ continue;
105
129
  }
130
+ bytes.push(...Buffer.from(ch, 'utf8'));
131
+ }
132
+ return Buffer.from(bytes).toString('utf8');
133
+ }
134
+ function refExists(ref, cwd) {
135
+ try {
136
+ execFileSync('git', ['rev-parse', '--verify', '--quiet', ref], {
137
+ cwd,
138
+ stdio: 'ignore',
139
+ });
140
+ return true;
141
+ }
142
+ catch {
143
+ return false;
106
144
  }
107
- return [...files];
108
145
  }
109
146
  /** Enumerate the modules to consider — the whole workspace, or the single module. */
110
147
  export function enumerateModules(pkg) {
@@ -181,9 +218,12 @@ export async function checkPackages(options = {}) {
181
218
  targetNames = modules.map((m) => m.name);
182
219
  }
183
220
  else {
184
- base = resolveBase(options.since);
185
- const files = changedFiles(cwd, base);
186
- changedModules = mapFilesToModules(files, modules);
221
+ base = resolveBase(options.since, cwd);
222
+ if (options.since && !refExists(options.since, cwd)) {
223
+ throw new Error(`Could not diff against '${options.since}'. Ensure the ref exists locally ` +
224
+ '(e.g. `git fetch origin`).');
225
+ }
226
+ changedModules = mapFilesToModules(changedFiles(cwd, base), modules);
187
227
  const set = new Set(changedModules);
188
228
  if (options.dependents && pkg.getWorkspacePath())
189
229
  addDependents(set, pkg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgpmjs/core",
3
- "version": "7.24.0",
3
+ "version": "7.25.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "PGPM Package and Migration Tools",
6
6
  "main": "index.js",
@@ -44,29 +44,30 @@
44
44
  "@pgsql/types": "^18.0.0",
45
45
  "@types/pg": "^8.20.0",
46
46
  "copyfiles": "^2.4.1",
47
- "inquirerer": "^4.9.1",
47
+ "inquirerer": "^4.9.3",
48
48
  "makage": "^0.3.0"
49
49
  },
50
50
  "dependencies": {
51
- "@pgpmjs/ast": "^0.11.1",
52
- "@pgpmjs/bundle": "^0.14.1",
53
- "@pgpmjs/diff": "^0.3.1",
54
- "@pgpmjs/env": "^2.41.1",
51
+ "@pgpmjs/ast": "^0.11.2",
52
+ "@pgpmjs/bundle": "^0.14.2",
53
+ "@pgpmjs/diff": "^0.3.2",
54
+ "@pgpmjs/env": "^2.41.2",
55
55
  "@pgpmjs/logger": "^2.24.0",
56
- "@pgpmjs/server-utils": "^3.25.1",
57
- "@pgpmjs/slice": "^0.14.2",
58
- "@pgpmjs/transform": "^0.23.0",
59
- "@pgpmjs/types": "^2.50.1",
60
- "csv-to-pg": "^4.10.1",
61
- "genomic": "^5.6.2",
56
+ "@pgpmjs/server-utils": "^3.25.2",
57
+ "@pgpmjs/slice": "^0.14.3",
58
+ "@pgpmjs/transform": "^0.23.1",
59
+ "@pgpmjs/types": "^2.50.2",
60
+ "csv-to-pg": "^4.10.2",
61
+ "genomic": "^5.6.5",
62
+ "git-changed": "^0.3.0",
62
63
  "glob": "^13.0.6",
63
64
  "parse-package-name": "^1.0.0",
64
65
  "pg": "^8.21.0",
65
- "pg-cache": "^3.25.1",
66
+ "pg-cache": "^3.25.2",
66
67
  "pg-env": "^1.29.0",
67
68
  "pgsql-deparser": "^18.3.3",
68
69
  "pgsql-parser": "^18.2.3",
69
70
  "yanse": "^0.2.1"
70
71
  },
71
- "gitHead": "c9dcb0c96174dfb0bc6fd63e46cc7776ea7af173"
72
+ "gitHead": "43121004a8bedd2c5fcf7bf33208079d55bad01f"
72
73
  }
@@ -23,8 +23,9 @@ export interface PackageCheckOptions {
23
23
  cwd?: string;
24
24
  /**
25
25
  * Git ref/branch/tag to diff `HEAD` against for change detection. When
26
- * omitted, the check auto-detects the base (`origin/$GITHUB_BASE_REF` in a
27
- * PR, otherwise it only inspects uncommitted/untracked working-tree changes).
26
+ * omitted, the base is auto-detected: `origin/$GITHUB_BASE_REF` in a PR,
27
+ * otherwise the repository's default branch, falling back to working-tree
28
+ * changes only when neither is available.
28
29
  */
29
30
  since?: string;
30
31
  /** Check every module in the workspace, skipping git change detection. */
@@ -63,15 +64,19 @@ export interface PackageCheckResult {
63
64
  */
64
65
  export declare function checkModuleArtifact(moduleDir: string, name: string): ModuleDrift | null;
65
66
  /**
66
- * Resolve the git ref to diff against. Explicit `--since` wins; otherwise use
67
- * the PR base branch when running in GitHub Actions; otherwise `undefined`
68
- * (working-tree changes only).
67
+ * Resolve the git ref to diff against. Explicit `--since` wins; otherwise the
68
+ * PR base branch when running in GitHub Actions; otherwise the repository's
69
+ * default branch. `undefined` means there is nothing to diff against — a
70
+ * shallow or detached checkout — and only the working tree is inspected.
69
71
  */
70
- export declare function resolveBase(since?: string): string | undefined;
72
+ export declare function resolveBase(since?: string, cwd?: string): string | undefined;
71
73
  /**
72
- * Collect the set of changed files, combining committed changes since `base`
73
- * (three-dot diff, so it works with branches, refs, and tags) with any
74
- * uncommitted/untracked changes in the working tree. Returns absolute paths.
74
+ * Collect the set of changed files: committed changes since the **merge base**
75
+ * with `base`, unioned with uncommitted and untracked working-tree changes.
76
+ *
77
+ * Returns absolute paths, including paths that no longer exist — deleting a
78
+ * `deploy/` file makes a module's bundle stale exactly as much as editing one,
79
+ * so the deletion has to reach {@link mapFilesToModules}.
75
80
  */
76
81
  export declare function changedFiles(cwd: string, base?: string): string[];
77
82
  /** Enumerate the modules to consider — the whole workspace, or the single module. */
@@ -9,6 +9,8 @@ exports.addDependents = addDependents;
9
9
  exports.checkPackages = checkPackages;
10
10
  const bundle_1 = require("@pgpmjs/bundle");
11
11
  const child_process_1 = require("child_process");
12
+ const fs_1 = require("fs");
13
+ const git_changed_1 = require("git-changed");
12
14
  const path_1 = require("path");
13
15
  const artifact_1 = require("../bundle/artifact");
14
16
  const pgpm_1 = require("../core/class/pgpm");
@@ -57,63 +59,98 @@ function checkModuleArtifact(moduleDir, name) {
57
59
  }
58
60
  return null;
59
61
  }
60
- function git(args, cwd) {
61
- return (0, child_process_1.execSync)(`git ${args}`, {
62
- cwd,
63
- encoding: 'utf-8',
64
- stdio: ['ignore', 'pipe', 'ignore'],
65
- maxBuffer: 64 * 1024 * 1024,
66
- });
67
- }
68
62
  /**
69
- * Resolve the git ref to diff against. Explicit `--since` wins; otherwise use
70
- * the PR base branch when running in GitHub Actions; otherwise `undefined`
71
- * (working-tree changes only).
63
+ * Resolve the git ref to diff against. Explicit `--since` wins; otherwise the
64
+ * PR base branch when running in GitHub Actions; otherwise the repository's
65
+ * default branch. `undefined` means there is nothing to diff against — a
66
+ * shallow or detached checkout — and only the working tree is inspected.
72
67
  */
73
- function resolveBase(since) {
74
- if (since)
75
- return since;
76
- const prBase = process.env.GITHUB_BASE_REF;
77
- if (prBase && prBase.trim())
78
- return `origin/${prBase.trim()}`;
79
- return undefined;
68
+ function resolveBase(since, cwd = process.cwd()) {
69
+ return (0, git_changed_1.resolveBase)(since, cwd);
80
70
  }
81
71
  /**
82
- * Collect the set of changed files, combining committed changes since `base`
83
- * (three-dot diff, so it works with branches, refs, and tags) with any
84
- * uncommitted/untracked changes in the working tree. Returns absolute paths.
72
+ * Collect the set of changed files: committed changes since the **merge base**
73
+ * with `base`, unioned with uncommitted and untracked working-tree changes.
74
+ *
75
+ * Returns absolute paths, including paths that no longer exist — deleting a
76
+ * `deploy/` file makes a module's bundle stale exactly as much as editing one,
77
+ * so the deletion has to reach {@link mapFilesToModules}.
85
78
  */
86
79
  function changedFiles(cwd, base) {
87
- const files = new Set();
88
- const status = git('status --porcelain', cwd);
89
- for (const rawLine of status.split('\n')) {
90
- const line = rawLine.trimEnd();
91
- if (!line)
80
+ // `base: false` when there is no base: the ref is resolved by the caller, and
81
+ // git-changed would otherwise resolve one of its own.
82
+ return (0, git_changed_1.changedFiles)({ cwd, base: base ?? false, existingOnly: false }).paths.map(decodeGitPath);
83
+ }
84
+ /**
85
+ * Undo git's C-escaping of a path it decided to quote.
86
+ *
87
+ * With `core.quotepath` on (the default) git renders a path holding a non-ASCII
88
+ * byte as `"caf\303\251 dir/x.sql"`: octal escapes, one per UTF-8 byte. A caller
89
+ * that only strips the surrounding quotes is left with a filename that exists
90
+ * nowhere on disk, so the module owning it silently drops out of the check —
91
+ * `pgpm package --check` passes while the module's bundle is stale.
92
+ *
93
+ * Only applied when it actually resolves something: if the escaped form exists on
94
+ * disk and the decoded one does not, the backslashes were part of the filename.
95
+ */
96
+ function decodeGitPath(path) {
97
+ if (!path.includes('\\'))
98
+ return path;
99
+ const decoded = cUnescape(path);
100
+ if (decoded === path)
101
+ return path;
102
+ if ((0, fs_1.existsSync)(decoded))
103
+ return decoded;
104
+ // Neither form exists for a deleted file; the decoded name is the real one.
105
+ return (0, fs_1.existsSync)(path) ? path : decoded;
106
+ }
107
+ const C_ESCAPES = {
108
+ a: 0x07,
109
+ b: 0x08,
110
+ f: 0x0c,
111
+ n: 0x0a,
112
+ r: 0x0d,
113
+ t: 0x09,
114
+ v: 0x0b,
115
+ '\\': 0x5c,
116
+ '"': 0x22,
117
+ };
118
+ /** Decode `\NNN` octal byte escapes and the single-character C escapes git emits. */
119
+ function cUnescape(input) {
120
+ const bytes = [];
121
+ for (let i = 0; i < input.length; i += 1) {
122
+ const ch = input[i];
123
+ if (ch !== '\\' || i + 1 >= input.length) {
124
+ bytes.push(...Buffer.from(ch, 'utf8'));
92
125
  continue;
93
- let p = line.slice(3);
94
- const arrow = p.indexOf(' -> ');
95
- if (arrow !== -1)
96
- p = p.slice(arrow + 4);
97
- p = p.replace(/^"|"$/g, '');
98
- if (p)
99
- files.add((0, path_1.resolve)(cwd, p));
100
- }
101
- if (base) {
102
- let diff;
103
- try {
104
- diff = git(`diff --name-only ${base}...HEAD`, cwd);
105
126
  }
106
- catch (err) {
107
- throw new Error(`Could not diff against '${base}'. Ensure the ref exists locally ` +
108
- `(e.g. \`git fetch origin\`). Original error: ${err instanceof Error ? err.message : String(err)}`);
127
+ const next = input[i + 1];
128
+ const octal = input.slice(i + 1, i + 4);
129
+ if (/^[0-7]{3}$/.test(octal)) {
130
+ bytes.push(parseInt(octal, 8));
131
+ i += 3;
132
+ continue;
109
133
  }
110
- for (const rawLine of diff.split('\n')) {
111
- const p = rawLine.trim();
112
- if (p)
113
- files.add((0, path_1.resolve)(cwd, p));
134
+ if (next in C_ESCAPES) {
135
+ bytes.push(C_ESCAPES[next]);
136
+ i += 1;
137
+ continue;
114
138
  }
139
+ bytes.push(...Buffer.from(ch, 'utf8'));
140
+ }
141
+ return Buffer.from(bytes).toString('utf8');
142
+ }
143
+ function refExists(ref, cwd) {
144
+ try {
145
+ (0, child_process_1.execFileSync)('git', ['rev-parse', '--verify', '--quiet', ref], {
146
+ cwd,
147
+ stdio: 'ignore',
148
+ });
149
+ return true;
150
+ }
151
+ catch {
152
+ return false;
115
153
  }
116
- return [...files];
117
154
  }
118
155
  /** Enumerate the modules to consider — the whole workspace, or the single module. */
119
156
  function enumerateModules(pkg) {
@@ -190,9 +227,12 @@ async function checkPackages(options = {}) {
190
227
  targetNames = modules.map((m) => m.name);
191
228
  }
192
229
  else {
193
- base = resolveBase(options.since);
194
- const files = changedFiles(cwd, base);
195
- changedModules = mapFilesToModules(files, modules);
230
+ base = resolveBase(options.since, cwd);
231
+ if (options.since && !refExists(options.since, cwd)) {
232
+ throw new Error(`Could not diff against '${options.since}'. Ensure the ref exists locally ` +
233
+ '(e.g. `git fetch origin`).');
234
+ }
235
+ changedModules = mapFilesToModules(changedFiles(cwd, base), modules);
196
236
  const set = new Set(changedModules);
197
237
  if (options.dependents && pkg.getWorkspacePath())
198
238
  addDependents(set, pkg);