-
Notifications
You must be signed in to change notification settings - Fork 2
fix: fixed issue where git may not be setup on users computer. #23
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
Merged
Merged
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
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,82 @@ | ||
| jest.mock('inquirer', () => ({ | ||
| __esModule: true, | ||
| default: { prompt: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.mock('execa', () => ({ | ||
| __esModule: true, | ||
| execa: jest.fn(), | ||
| })); | ||
|
|
||
| import inquirer from 'inquirer'; | ||
| import { execa } from 'execa'; | ||
| import { promptAndSetLocalGitUser } from '../git-user-config.js'; | ||
|
|
||
| const mockPrompt = inquirer.prompt as jest.MockedFunction<typeof inquirer.prompt>; | ||
| const mockExeca = execa as jest.MockedFunction<typeof execa>; | ||
|
|
||
| describe('promptAndSetLocalGitUser', () => { | ||
| const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
| const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| consoleErrorSpy.mockRestore(); | ||
| consoleLogSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('reads global defaults then sets local user.name and user.email', async () => { | ||
| mockExeca | ||
| .mockResolvedValueOnce({ stdout: 'Jane Doe\n', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>) | ||
| .mockResolvedValueOnce({ stdout: 'jane@example.com\n', stderr: '', exitCode: 0 } as Awaited< | ||
| ReturnType<typeof execa> | ||
| >) | ||
| .mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>); | ||
|
|
||
| mockPrompt.mockResolvedValue({ | ||
| userName: ' Local Name ', | ||
| userEmail: ' local@example.com ', | ||
| }); | ||
|
|
||
| await promptAndSetLocalGitUser('/tmp/proj'); | ||
|
|
||
| expect(mockExeca).toHaveBeenNthCalledWith(1, 'git', ['config', '--global', 'user.name'], { | ||
| reject: false, | ||
| }); | ||
| expect(mockExeca).toHaveBeenNthCalledWith(2, 'git', ['config', '--global', 'user.email'], { | ||
| reject: false, | ||
| }); | ||
| expect(mockExeca).toHaveBeenNthCalledWith(3, 'git', ['config', '--local', 'user.name', 'Local Name'], { | ||
| cwd: '/tmp/proj', | ||
| stdio: 'inherit', | ||
| }); | ||
| expect(mockExeca).toHaveBeenNthCalledWith( | ||
| 4, | ||
| 'git', | ||
| ['config', '--local', 'user.email', 'local@example.com'], | ||
| { cwd: '/tmp/proj', stdio: 'inherit' }, | ||
| ); | ||
| expect(consoleLogSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Set local git user.name and user.email'), | ||
| ); | ||
| }); | ||
|
|
||
| it('skips git config when name or email is empty after trim', async () => { | ||
| mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>); | ||
| mockPrompt.mockResolvedValue({ userName: '', userEmail: 'a@b.com' }); | ||
|
|
||
| await promptAndSetLocalGitUser('/tmp/proj'); | ||
|
|
||
| const localConfigCalls = mockExeca.mock.calls.filter( | ||
| ([cmd, args]) => | ||
| cmd === 'git' && Array.isArray(args) && (args as string[]).includes('--local'), | ||
| ); | ||
| expect(localConfigCalls).toHaveLength(0); | ||
| expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Both user.name and user.email are required'), | ||
| ); | ||
| }); | ||
| }); |
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,48 @@ | ||
| import { execa } from 'execa'; | ||
| import inquirer from 'inquirer'; | ||
|
|
||
| async function getGlobalGitValue(key: 'user.name' | 'user.email'): Promise<string | undefined> { | ||
| const result = await execa('git', ['config', '--global', key], { reject: false }); | ||
| const value = result.stdout?.trim(); | ||
| return value || undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Prompts for user.name and user.email and sets them locally for the repository at cwd. | ||
| * Defaults are taken from global git config when present. | ||
| */ | ||
| export async function promptAndSetLocalGitUser(cwd: string): Promise<void> { | ||
| const defaultName = await getGlobalGitValue('user.name'); | ||
| const defaultEmail = await getGlobalGitValue('user.email'); | ||
|
|
||
| const answers = await inquirer.prompt([ | ||
| { | ||
| type: 'input', | ||
| name: 'userName', | ||
| message: 'Git user.name for this repository:', | ||
| default: defaultName ?? '', | ||
| }, | ||
| { | ||
| type: 'input', | ||
| name: 'userEmail', | ||
| message: 'Git user.email for this repository:', | ||
| default: defaultEmail ?? '', | ||
| }, | ||
| ]); | ||
|
|
||
| const name = typeof answers.userName === 'string' ? answers.userName.trim() : ''; | ||
| const email = typeof answers.userEmail === 'string' ? answers.userEmail.trim() : ''; | ||
|
|
||
| if (!name || !email) { | ||
| console.error('\n⚠️ Both user.name and user.email are required. Git user was not configured.\n'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await execa('git', ['config', '--local', 'user.name', name], { cwd, stdio: 'inherit' }); | ||
| await execa('git', ['config', '--local', 'user.email', email], { cwd, stdio: 'inherit' }); | ||
| console.log('\n✅ Set local git user.name and user.email for this repository.\n'); | ||
| } catch { | ||
| console.error('\n⚠️ Could not set git config. Ensure git is installed and this directory is a repository.\n'); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.