Skip to content

Commit 1a09ed9

Browse files
committed
Hardens working tree comparison logic
- Adds an early return when the target reference is null - Combines checks for revision ranges and staged changes into a single statement - Updates the fallback logic to only consider comparisons against 'HEAD' without a source as a working tree comparison - Prevents untracked files from being incorrectly included when diffing specific commits, as those comparisons do not actually target the working tree
1 parent a5b0077 commit 1a09ed9

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

  • packages/git-cli/src/providers

packages/git-cli/src/providers/diff.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,15 +1058,20 @@ function isNonExactPathspec(path: string | undefined): boolean {
10581058
// (i.e. `git diff`, `git diff HEAD`, or `git diff <ref>` with no `from`), and
10591059
// therefore untracked files are a meaningful addition to the result.
10601060
function isWorkingTreeComparison(to: string | undefined, from: string | undefined): boolean {
1061-
if (to != null && isRevisionRange(to)) return false;
1062-
if (isUncommittedStaged(to)) return false;
1061+
if (to == null) return true;
1062+
if (isRevisionRange(to) || isUncommittedStaged(to)) return false;
1063+
10631064
// `to === uncommitted` always targets the working tree (`prepareToFromDiffArgs` sets
10641065
// `from = 'HEAD'` when omitted, or diffs against the caller-supplied `from`).
10651066
if (isUncommitted(to)) return true;
1067+
10661068
// `git diff <from>` — `to === ''` means the caller explicitly asked for working tree vs `from`
10671069
if (to === '') return from == null || !isUncommittedStaged(from);
1068-
// `git diff <to>` with no `from` — working tree vs `to`
1069-
return from == null;
1070+
1071+
// `getChangedFilesCount` uses `prepareToFromDiffArgs`, which translates `to` (with no `from`)
1072+
// to `${to}^ ${to}` UNLESS `to` is HEAD or empty.
1073+
if (to.toUpperCase() === 'HEAD' && from == null) return true;
1074+
return false;
10701075
}
10711076

10721077
function prepareToFromDiffArgs(

0 commit comments

Comments
 (0)