git-changed 0.3.0 → 0.4.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.
package/README.md CHANGED
@@ -165,7 +165,7 @@ Same options; returns `result.paths`.
165
165
  In order, first hit wins:
166
166
 
167
167
  1. `options.base` / `--base <ref>` — explicit always wins.
168
- 2. `$GITHUB_BASE_REF` as `origin/<ref>`, **if that ref exists locally**. On a pull request GitHub sets this to the target branch. The existence check matters: without it you hand back a ref that every subsequent git call rejects.
168
+ 2. `$GITHUB_BASE_REF` — the PR's target branch — as `origin/<ref>`, else the bare `<ref>`, **whichever exists locally**. `origin/` comes first because Actions fetches only the remote ref and a stale local branch of the same name would diff against the wrong commit; the bare branch covers running the same tool outside CI. Checking existence matters: without it you hand back a ref that every subsequent git call rejects.
169
169
  3. The repository default branch — `origin/HEAD` if set, else the first of `origin/main`, `origin/master`, `main`, `master` that resolves.
170
170
 
171
171
  Then the diff is taken from `git merge-base HEAD <base>`, not from the base tip, so work that landed on the base after you forked is not attributed to you.
@@ -284,7 +284,7 @@ if (process.env.CI && source === 'worktree') {
284
284
  | Files from someone else's merge included | You diffed the base tip somewhere else in your pipeline; this package uses the merge base. Verify with `--json` and look at `mergeBase`. |
285
285
  | A new module's files are missing | You're not using this package, or not `-uall`. Untracked files inside a new directory need it. |
286
286
  | Tool crashes on a missing path | Something passed `existingOnly: false`. The default drops deleted paths. |
287
- | `origin/main` not found | `$GITHUB_BASE_REF` was set but unfetched. This package falls through to the local default branch rather than returning a broken ref. |
287
+ | `origin/main` not found | `$GITHUB_BASE_REF` was set but unfetched. This package falls back to the local branch of that name, then to the default branch, rather than returning a broken ref. |
288
288
 
289
289
  ## License
290
290
 
package/base.d.ts CHANGED
@@ -5,8 +5,8 @@ export declare function defaultBranch(cwd: string): string | undefined;
5
5
  *
6
6
  * 1. an explicit base (`--base`, or `base` in code) — always wins;
7
7
  * 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
8
- * `pull_request` events, as `origin/<branch>` since only the remote ref is
9
- * fetched;
8
+ * `pull_request` events — as `origin/<branch>`, or the bare branch when the
9
+ * remote ref was never fetched;
10
10
  * 3. the repository's default branch.
11
11
  *
12
12
  * `undefined` means "no base is available" — a detached checkout, a fresh repo
package/base.js CHANGED
@@ -25,8 +25,8 @@ function defaultBranch(cwd) {
25
25
  *
26
26
  * 1. an explicit base (`--base`, or `base` in code) — always wins;
27
27
  * 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
28
- * `pull_request` events, as `origin/<branch>` since only the remote ref is
29
- * fetched;
28
+ * `pull_request` events — as `origin/<branch>`, or the bare branch when the
29
+ * remote ref was never fetched;
30
30
  * 3. the repository's default branch.
31
31
  *
32
32
  * `undefined` means "no base is available" — a detached checkout, a fresh repo
@@ -37,13 +37,18 @@ function defaultBranch(cwd) {
37
37
  function resolveBase(base, cwd = process.cwd()) {
38
38
  if (base && base.trim())
39
39
  return base.trim();
40
- const prBase = process.env.GITHUB_BASE_REF;
41
- if (prBase && prBase.trim()) {
42
- const ref = `origin/${prBase.trim()}`;
43
- if ((0, git_1.tryGit)(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined)
44
- return ref;
45
- // The remote ref is missing (a single-branch or shallow fetch). Fall through
46
- // rather than handing back a ref that every later git call will reject.
40
+ const prBase = process.env.GITHUB_BASE_REF?.trim();
41
+ if (prBase) {
42
+ // `origin/<branch>` first: in Actions only the remote ref is fetched, and a
43
+ // stale local branch of the same name would diff against the wrong commit.
44
+ // A bare local branch is the fallback for a developer running the same tool
45
+ // outside CI with the variable exported.
46
+ for (const ref of [`origin/${prBase}`, prBase]) {
47
+ if ((0, git_1.tryGit)(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined)
48
+ return ref;
49
+ }
50
+ // Neither exists (a single-branch or shallow fetch). Fall through rather than
51
+ // handing back a ref that every later git call will reject.
47
52
  }
48
53
  return defaultBranch(cwd);
49
54
  }
package/esm/base.d.ts CHANGED
@@ -5,8 +5,8 @@ export declare function defaultBranch(cwd: string): string | undefined;
5
5
  *
6
6
  * 1. an explicit base (`--base`, or `base` in code) — always wins;
7
7
  * 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
8
- * `pull_request` events, as `origin/<branch>` since only the remote ref is
9
- * fetched;
8
+ * `pull_request` events — as `origin/<branch>`, or the bare branch when the
9
+ * remote ref was never fetched;
10
10
  * 3. the repository's default branch.
11
11
  *
12
12
  * `undefined` means "no base is available" — a detached checkout, a fresh repo
package/esm/base.js CHANGED
@@ -21,8 +21,8 @@ export function defaultBranch(cwd) {
21
21
  *
22
22
  * 1. an explicit base (`--base`, or `base` in code) — always wins;
23
23
  * 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
24
- * `pull_request` events, as `origin/<branch>` since only the remote ref is
25
- * fetched;
24
+ * `pull_request` events — as `origin/<branch>`, or the bare branch when the
25
+ * remote ref was never fetched;
26
26
  * 3. the repository's default branch.
27
27
  *
28
28
  * `undefined` means "no base is available" — a detached checkout, a fresh repo
@@ -33,13 +33,18 @@ export function defaultBranch(cwd) {
33
33
  export function resolveBase(base, cwd = process.cwd()) {
34
34
  if (base && base.trim())
35
35
  return base.trim();
36
- const prBase = process.env.GITHUB_BASE_REF;
37
- if (prBase && prBase.trim()) {
38
- const ref = `origin/${prBase.trim()}`;
39
- if (tryGit(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined)
40
- return ref;
41
- // The remote ref is missing (a single-branch or shallow fetch). Fall through
42
- // rather than handing back a ref that every later git call will reject.
36
+ const prBase = process.env.GITHUB_BASE_REF?.trim();
37
+ if (prBase) {
38
+ // `origin/<branch>` first: in Actions only the remote ref is fetched, and a
39
+ // stale local branch of the same name would diff against the wrong commit.
40
+ // A bare local branch is the fallback for a developer running the same tool
41
+ // outside CI with the variable exported.
42
+ for (const ref of [`origin/${prBase}`, prBase]) {
43
+ if (tryGit(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined)
44
+ return ref;
45
+ }
46
+ // Neither exists (a single-branch or shallow fetch). Fall through rather than
47
+ // handing back a ref that every later git call will reject.
43
48
  }
44
49
  return defaultBranch(cwd);
45
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-changed",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "List the files changed against a merge base, including working-tree and untracked changes",
6
6
  "main": "index.js",
@@ -43,5 +43,5 @@
43
43
  "ci",
44
44
  "incremental"
45
45
  ],
46
- "gitHead": "8285e90d5985f800079f8e1cf36426db392ff565"
46
+ "gitHead": "3a7b927ad734e120b8d1cefe01b56a032eec6613"
47
47
  }