Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Local development and CI share these entrypoints. The `npm run` aliases remain a
- `script/all` runs lint, unit tests, build, and acceptance.
- `script/verify-bundle` rebuilds and rejects tracked or untracked changes in `dist/`. Run it after committing the generated files.

Keep inputs, outputs, defaults, errors, and diff parsing compatible. The parser currently truncates some `--binary` diffs and retains a trailing tab for some paths containing spaces. Tests preserve these existing behaviors. Parser fixes should be separate, deliberate changes.
Keep inputs, outputs, defaults, errors, and diff parsing compatible. The parser currently truncates some `--binary` diffs. Tests preserve this existing behavior. Parser fixes should be separate, deliberate changes.

The only runtime package is `parse-git-diff`. TypeScript provides type checking, `@types/node` describes the supported runtime, Prettier provides formatting, and `ncc` produces the self-contained Action. The local Actions adapter implements only the string input/output and logging functions this repository uses. Its toolkit attribution is in `LICENSES/actions-toolkit.txt` and the generated distribution notices.

Expand Down
3 changes: 1 addition & 2 deletions __tests__/acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ test('standalone bundle runs real Git with spaces in path and file-only outputs'
const parsed: {files: Array<{path: string}>} = JSON.parse(
readFileSync(join(action.workspace, 'diff.json'), 'utf8')
)
// Preserve parse-git-diff's existing trailing-tab behavior for spaced paths.
assert.equal(parsed.files[0]?.path, 'space name.txt\t')
assert.equal(parsed.files[0]?.path, 'space name.txt')
assert.match(
readFileSync(join(action.workspace, 'raw.txt'), 'utf8'),
/after 雪/
Expand Down
34 changes: 34 additions & 0 deletions __tests__/fixtures/paths-with-spaces.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
diff --git a/added file.txt b/added file.txt
new file mode 100644
index 0000000000000000000000000000000000000000..45b983be36b73c0788dc9cbcb76cbb80fc7bb057
--- /dev/null
+++ b/added file.txt
@@ -0,0 +1 @@
+hi
diff --git a/changed file.txt b/changed file.txt
index 814f4a422927b82f5f8a43f8fab6d3839e3983f2..4cb29ea38f70d7c61b2a3a25b02e3bdf44905402 100644
--- a/changed file.txt
+++ b/changed file.txt
@@ -1,2 +1,3 @@
one
two
+three
diff --git a/deleted file.txt b/deleted file.txt
deleted file mode 100644
index b023018cabc396e7692c70bbf5784a93d3f738ab..0000000000000000000000000000000000000000
--- a/deleted file.txt
+++ /dev/null
@@ -1 +0,0 @@
-bye
diff --git a/old name.txt b/new name.txt
similarity index 85%
rename from old name.txt
rename to new name.txt
index 0fdf397db08b5cecda1b6394d4fef7395c1933ba..f9d9a0195c5b9c01ef64e2a69d8b9a624f42b8c8 100644
--- a/old name.txt
+++ b/new name.txt
@@ -4,3 +4,4 @@ c
d
e
f
+g
16 changes: 16 additions & 0 deletions __tests__/git-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ for (const [file, count, rawCount] of [
})
}

test('strips the trailing tab git adds to paths with spaces', async () => {
process.env.INPUT_GIT_DIFF_FILE = '__tests__/fixtures/paths-with-spaces.diff'
const result = await gitDiff()
assert.deepEqual(
result?.files.map(file =>
'path' in file ? file.path : [file.pathBefore, file.pathAfter]
),
[
'added file.txt',
'changed file.txt',
'deleted file.txt',
['old name.txt', 'new name.txt']
]
)
})

for (const [value, expected] of [
['', []],
[' ', []],
Expand Down
12 changes: 11 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions src/functions/git-diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '../actions-core.ts'
import parseGitDiff from 'parse-git-diff'
import parseGitDiff, {type GitDiff} from 'parse-git-diff'
import {execFileAsync} from './exec-async.ts'
import fs from 'node:fs'
import path from 'node:path'
Expand Down Expand Up @@ -137,6 +137,17 @@ function resolveWorkspacePath(filePath: string) {
)
}

// git appends a tab to `---` and `+++` paths that contain spaces, and
// parse-git-diff keeps that tab in the parsed path
function stripPathTrailingTabs(diff: GitDiff) {
for (const file of diff.files) {
if ('path' in file && file.path.endsWith('\t')) {
file.path = file.path.slice(0, -1)
}
}
return diff
}

// Helper function to get the diff from the git command
// :returns: The diff object which is parsed git diff
// If an error occurs, setFailed is called and it returns null
Expand Down Expand Up @@ -214,7 +225,7 @@ export async function gitDiff() {
}

// JSON diff
const diff = parseGitDiff(rawGitDiff)
const diff = stripPathTrailingTabs(parseGitDiff(rawGitDiff))
const jsonDiff = JSON.stringify(diff)

// log the total amount of files changed in the json diff
Expand Down