gemini-cli
gemini-cli copied to clipboard
fix(cli): handle PAT tokens and credentials in git remote URL parsing
Summary
The /setup-github command fails when the git remote URL contains a personal access token (PAT) or other credentials.
This PR replaces the fragile regex-based URL parser with the standard URL constructor, which properly handles credentials and is already used elsewhere in the codebase.
Details
The previous regex /(?:https?:\/\/|git@)github\.com(?::|\/)([^/]+)\/([^/]+?)(?:\.git)?$/ couldn't parse URLs like https://[email protected]/owner/repo.git because it didn't account for credentials between the protocol and hostname.
The new implementation:
- Uses JavaScript's native URL constructor (following the pattern in tryParseGithubUrl() from packages/cli/src/config/extensions/github.ts)
- Handles all credential formats: classic PAT (ghp_), fine-grained PAT (github_pat_), username:password, OAuth tokens, GitHub Actions tokens
- Fixes case sensitivity bug (uppercase GITHUB.COM now works)
- Properly handles SSH URLs ([email protected]:owner/repo.git)
- Rejects non-GitHub providers (GitLab, Bitbucket) with error
Related Issues
Fixes #14556
How to Validate
-
Run the new tests: npx vitest run packages/cli/src/utils/gitUtils.test.ts
-
Manual test with a PAT URL:
git remote set-url origin https://[email protected]/owner/repo.git
gemini
# Run /setup-github - should no longer fail with "Owner & repo could not be extracted"
git remote set-url origin https://github.com/owner/repo.git # restore
Pre-Merge Checklist
- [ ] Updated relevant documentation and README (if needed)
- [x] Added/updated tests (if needed)
- [ ] Noted breaking changes (if any)
- [x] Validated on required platforms/methods:
- [ ] MacOS
- [ ] npm run
- [ ] npx
- [ ] Docker
- [ ] Podman
- [ ] Seatbelt
- [ ] Windows
- [ ] npm run
- [ ] npx
- [ ] Docker
- [x] Linux
- [x] npm run
- [ ] npx
- [ ] Docker
- [ ] MacOS