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:
Matheus Nogueira
2026-04-01 20:02:05 -03:00
committed by GitHub
parent e8e7d2ea8f
commit 2ec433f6df
8 changed files with 143 additions and 2 deletions
+34
View File
@@ -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}`
}
+12
View File
@@ -26,6 +26,7 @@ import {
getBranchCompare,
getBranchDiff
} from '../git/status'
import { getRemoteFileUrl } from '../git/repo'
import {
resolveAuthorizedPath,
resolveRegisteredWorktreePath,
@@ -432,4 +433,15 @@ export function registerFilesystemHandlers(store: Store): void {
await discardChanges(worktreePath, filePath)
}
)
ipcMain.handle(
'git:remoteFileUrl',
async (
_event,
args: { worktreePath: string; relativePath: string; line: number }
): Promise<string | null> => {
const worktreePath = await resolveRegisteredWorktreePath(args.worktreePath, store)
return getRemoteFileUrl(worktreePath, args.relativePath, args.line)
}
)
}
+13
View File
@@ -0,0 +1,13 @@
declare module 'hosted-git-info' {
type HostedGitInfo = {
browseFile(path: string, opts?: { committish?: string; fragment?: string }): string | undefined
browse(path?: string, opts?: { committish?: string; fragment?: string }): string | undefined
}
type HostedGitInfoStatic = {
fromUrl(url: string): HostedGitInfo | undefined
}
const hostedGitInfo: HostedGitInfoStatic
export default hostedGitInfo
}
+5
View File
@@ -172,6 +172,11 @@ type GitApi = {
stage: (args: { worktreePath: string; filePath: string }) => Promise<void>
unstage: (args: { worktreePath: string; filePath: string }) => Promise<void>
discard: (args: { worktreePath: string; filePath: string }) => Promise<void>
remoteFileUrl: (args: {
worktreePath: string
relativePath: string
line: number
}) => Promise<string | null>
}
type Api = {
+6 -1
View File
@@ -283,7 +283,12 @@ const api = {
unstage: (args: { worktreePath: string; filePath: string }): Promise<void> =>
ipcRenderer.invoke('git:unstage', args),
discard: (args: { worktreePath: string; filePath: string }): Promise<void> =>
ipcRenderer.invoke('git:discard', args)
ipcRenderer.invoke('git:discard', args),
remoteFileUrl: (args: {
worktreePath: string
relativePath: string
line: number
}): Promise<string | null> => ipcRenderer.invoke('git:remoteFileUrl', args)
},
ui: {
@@ -1,7 +1,7 @@
import React, { useRef, useCallback, useEffect, useState } from 'react'
import Editor, { type OnMount } from '@monaco-editor/react'
import type { editor } from 'monaco-editor'
import { Copy } from 'lucide-react'
import { Copy, ExternalLink } from 'lucide-react'
import {
DropdownMenu,
DropdownMenuContent,
@@ -206,6 +206,23 @@ export default function MonacoEditor({
<Copy className="w-3.5 h-3.5 mr-1.5" />
Copy Rel. Path to Line
</DropdownMenuItem>
<DropdownMenuItem
onSelect={async () => {
// Derive worktree root from the absolute and relative paths
const worktreePath = filePath.slice(0, -(relativePath.length + 1))
const url = await window.api.git.remoteFileUrl({
worktreePath,
relativePath,
line: gutterMenuLine
})
if (url) {
window.api.ui.writeClipboardText(url)
}
}}
>
<ExternalLink className="w-3.5 h-3.5 mr-1.5" />
Copy Remote URL
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</>