Git - strip host:user prefix when deriving clone folder name#318518
Open
yogeshwaran-c wants to merge 1 commit into
Open
Git - strip host:user prefix when deriving clone folder name#318518yogeshwaran-c wants to merge 1 commit into
yogeshwaran-c wants to merge 1 commit into
Conversation
Clone targets derived from SCP-style SSH URLs (`[user@]host:path`) where the path contains no slash – e.g. `git@host.example:repo` – previously kept the full `user@host.example:repo` string as the folder name. The literal `:` is invalid in a Windows path, so `git clone` fails with `could not create work tree dir`. Strip everything up to the last `:` (after the existing slash-strip) so the folder name becomes `repo`. For all standard URL shapes the behavior is unchanged because the slash-strip already collapses the URL to its final path segment which has no colon. The folder-name derivation is extracted into the exported `getCloneTargetFolderName` helper and covered by unit tests. Fixes microsoft#175062
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: @lszomoruMatched files:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR extracts clone-target folder name derivation into a dedicated helper and adds tests to cover multiple remote URL formats, including SCP-style SSH URLs from issue #175062.
Changes:
- Introduced
getCloneTargetFolderName(url)and used it inGit.clone(...)instead of inline string manipulation. - Added unit tests covering common Git remote URL shapes and empty/unparseable inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| extensions/git/src/git.ts | Adds getCloneTargetFolderName and uses it in clone() to derive the target folder name more robustly (esp. SCP-style URLs). |
| extensions/git/src/test/git.test.ts | Adds test coverage for getCloneTargetFolderName across https/ssh/git/SCP-style/Windows-path inputs and fallback behavior. |
| */ | ||
| export function getCloneTargetFolderName(url: string): string { | ||
| return decodeURI(url) | ||
| .replace(/[\/]+$/, '') |
Comment on lines
+1046
to
+1050
| return decodeURI(url) | ||
| .replace(/[\/]+$/, '') | ||
| .replace(/^.*[\/\\]/, '') | ||
| .replace(/^.*:/, '') | ||
| .replace(/\.git$/, '') || 'repository'; |
| test('Windows file path with backslashes', () => { | ||
| assert.strictEqual(getCloneTargetFolderName('C:\\Users\\me\\repo.git'), 'repo'); | ||
| }); | ||
|
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #175062
Problem
Cloning an SCP-style SSH URL whose path contains no slash – for example
git@versions.somedomain.net:climate-project– fails on Windows (and is awkward on macOS/Linux) because the derived clone folder name keeps theuser@host:prefix verbatim:The literal
:is reserved in Windows paths, and thegit@host.example:prefix is meaningless inside a target directory name on any platform.Cause
Git.cloneinextensions/git/src/git.tsderivesbaseFolderNamefrom the URL with:For
git@host.example:repothere is no/(or\) at all, so the second replace leaves the string unchanged and the SCP[user@]host:prefix survives into the folder name.Fix
Strip everything up to the last
:as an additional step (after the slash-strip):For every URL shape that previously worked, the slash-strip has already collapsed the value to the final path segment (which contains no
:), so the new step is a no-op. Only SCP-style URLs without a slash in the path – the broken case – are affected.The derivation is extracted into an exported
getCloneTargetFolderName(url)helper alongside the other parsers ingit.ts, and 12 unit tests are added ingit.test.tscovering https, ssh://, git://, SCP-style with and without a path slash, Windows-style backslash paths, trailing slashes, and the fallback to'repository'.Out of scope
Full SCP-vs-URL parsing (port handling, scheme-aware logic,
git-url-parse-style dependencies) is intentionally not introduced – the goal is to repair the one well-defined broken shape without changing behavior for any already-working URL.