Repository navigation
fix: preserve special characters in Git filenames #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yongthyuan
wants to merge
2
commits into
prettier:master
Choose a base branch
from
Yongthyuan:codex/fix-git-special-filenames
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "pretty-quick": patch | ||
| --- | ||
|
|
||
| Preserve non-ASCII characters, quotes, tabs and newlines in Git filenames when finding changed files. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { execFileSync } from 'child_process' | ||
| import fs from 'fs' | ||
| import os from 'os' | ||
| import path from 'path' | ||
|
|
||
| let directory: string | ||
|
|
||
| // CI builds before testing. Run the built public API in Node so that Git, | ||
| // tinyexec and Prettier execute without Jest's manual module mocks. | ||
| const runPrettyQuick = (lockIndex = false) => | ||
| JSON.parse( | ||
| execFileSync( | ||
| process.execPath, | ||
| [ | ||
| '--input-type=module', | ||
| '--eval', | ||
| `import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { pathToFileURL } from 'node:url'; | ||
| const { default: prettyQuick } = await import(pathToFileURL(process.argv[1])); | ||
| const directory = process.argv[2]; | ||
| const result = await prettyQuick(directory, { | ||
| staged: true, | ||
| onStageFiles: process.argv[3] === 'true' | ||
| ? () => fs.writeFileSync(path.join(directory, '.git', 'index.lock'), '') | ||
| : undefined, | ||
| }); | ||
| process.stdout.write(JSON.stringify(result));`, | ||
| path.resolve(__dirname, '../lib/index.mjs'), | ||
| directory, | ||
| String(lockIndex), | ||
| ], | ||
| { encoding: 'utf8' }, | ||
| ), | ||
| ) | ||
|
|
||
| const git = (...args: string[]) => | ||
| // Resolve the same Git executable from PATH that pretty-quick uses. | ||
| // eslint-disable-next-line sonarjs/no-os-command-from-path | ||
| execFileSync('git', args, { cwd: directory, encoding: 'utf8' }) | ||
|
|
||
| beforeEach(() => { | ||
| directory = fs.mkdtempSync(path.join(os.tmpdir(), 'pretty-quick-git-')) | ||
| git('init', '--quiet') | ||
| fs.writeFileSync(path.join(directory, '.prettierrc.json'), '{"semi":true}') | ||
| git('add', '--', '.prettierrc.json') | ||
| git( | ||
| '-c', | ||
| 'user.name=Test', | ||
| '-c', | ||
| 'user.email=test@example.com', | ||
| '-c', | ||
| 'core.hooksPath=/dev/null', | ||
| 'commit', | ||
| '--quiet', | ||
| '-m', | ||
| 'Initial', | ||
| ) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(directory, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| const testFilename = process.platform === 'win32' ? test.skip : test | ||
|
|
||
| testFilename.each([ | ||
| ['中文.js', 'other.js'], | ||
| ['quote"name.js', 'other.js'], | ||
| ['tab\tname.js', 'other.js'], | ||
| ['line\nname.js', 'other.js'], | ||
| ['*中文.js', 'other中文.js'], | ||
| ['[文].js', '文.js'], | ||
| ['-中文.js', 'other.js'], | ||
| [':(glob)*中文.js', 'other中文.js'], | ||
| ])('restages only the literal file %j', (file, unrelated) => { | ||
| fs.writeFileSync(path.join(directory, file), 'example()') | ||
| fs.writeFileSync(path.join(directory, unrelated), 'unrelated()') | ||
| git('--literal-pathspecs', 'add', '--', file) | ||
|
|
||
| const result = runPrettyQuick() | ||
|
|
||
| expect(result).toEqual({ success: true, errors: [] }) | ||
| expect(git('show', ':0:' + file)).toBe('example();\n') | ||
| expect(git('diff', '--cached', '--name-only', '-z')).toBe(file + '\0') | ||
| expect(git('ls-files', '--others', '--exclude-standard', '-z')).toBe( | ||
| unrelated + '\0', | ||
| ) | ||
| expect(fs.readFileSync(path.join(directory, unrelated), 'utf8')).toBe( | ||
| 'unrelated()', | ||
| ) | ||
| }) | ||
|
|
||
| test('reports a failed restage when Git cannot write the index', () => { | ||
| fs.writeFileSync(path.join(directory, 'example.js'), 'example()') | ||
| git('add', '--', 'example.js') | ||
| const result = runPrettyQuick(true) | ||
| expect(result).toEqual({ success: false, errors: ['STAGE_FAILED'] }) | ||
| expect(git('show', ':0:example.js')).toBe('example()') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.