feat: add copy remote URL to line number context menu (#253)
* feat: add copy remote URL to line number context menu Add 'Copy Remote URL' option to the line number gutter right-click menu, allowing users to quickly copy a shareable link to a file and line on GitHub, GitLab, Bitbucket, or other hosted git providers. - Install hosted-git-info to parse remote URLs and generate browse URLs - Add getRemoteFileUrl() to resolve file URLs using default branch - Wire up IPC handler, preload bridge, and renderer UI - Use ExternalLink icon to distinguish from path copy options * refactor: use @types/hosted-git-info instead of custom type declarations Replace the custom hosted-git-info.d.ts with the @types package from DefinitelyTyped. Use a targeted cast for browseFile() which the @types package doesn't include yet (stuck on v3 API). * refactor: revert to local type declarations for hosted-git-info The @types/hosted-git-info package is stuck on the v3 API and lacks browseFile(). A local .d.ts is cleaner than casting around stale types.
This commit is contained in:
@@ -2,6 +2,7 @@ import { execFile, execSync } from 'child_process'
|
||||
import { existsSync, statSync } from 'fs'
|
||||
import { join, basename } from 'path'
|
||||
import { promisify } from 'util'
|
||||
import hostedGitInfo from 'hosted-git-info'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
@@ -321,3 +322,36 @@ export async function getBranchConflictKind(
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hosted URL (e.g. GitHub, GitLab, Bitbucket) for a specific file
|
||||
* and line in the repo. Returns null when the remote isn't a recognized host.
|
||||
*
|
||||
* Why hosted-git-info: it handles SSH, HTTPS, and shorthand remote URLs
|
||||
* across multiple providers, so we don't have to maintain our own URL parser.
|
||||
*/
|
||||
export function getRemoteFileUrl(
|
||||
repoPath: string,
|
||||
relativePath: string,
|
||||
line: number
|
||||
): string | null {
|
||||
const remoteUrl = getRemoteUrl(repoPath)
|
||||
if (!remoteUrl) {
|
||||
return null
|
||||
}
|
||||
|
||||
const info = hostedGitInfo.fromUrl(remoteUrl)
|
||||
if (!info) {
|
||||
return null
|
||||
}
|
||||
|
||||
const defaultBranch = getDefaultBaseRef(repoPath).replace(/^origin\//, '')
|
||||
const browseUrl = info.browseFile(relativePath, { committish: defaultBranch })
|
||||
if (!browseUrl) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Why: hosted-git-info lowercases the fragment, but GitHub convention
|
||||
// uses uppercase L for line links (e.g. #L42). Append manually.
|
||||
return `${browseUrl}#L${line}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user