a dotfile but it's really big
1#!/usr/bin/env nix-shell
2#!nix-shell -i nu -p nushell jujutsu wl-clipboard
3
4# Generates a remote git URL for the given file and line, and copies it to the clipboard.
5def main [
6 file: string # Absolute or relative path to the file
7 --line-start (-s): string # The starting line number (e.g., cursor line)
8 --line-end (-e): string # The ending line number (for selections)
9] {
10 let root = (jj workspace root | str trim)
11 let rel_path = ($file | path expand | path relative-to $root)
12
13 # Intersect current ancestry with the ancestry of all remote bookmarks
14 let ref = (jj log -r "heads(::@ & ::remote_bookmarks())" -n 1 --no-graph -T "commit_id" | str trim)
15
16 if ($ref | is-empty) {
17 print -e "Error: No pushed commits found in the current ancestry."
18 exit 1
19 }
20
21 let remote_url = (
22 jj git remote list
23 | parse "{remote} {url}"
24 | where remote == "origin"
25 | get url.0
26 | if ($in | str contains "://") { $in } else { $"https://($in | str replace ':' '/')" }
27 | url parse
28 )
29
30 # Construct the line number suffix (GitHub format)
31 let start = if ($line_start | is-empty) { "" } else { $line_start }
32 let end = if ($line_end | is-empty) { "" } else { $"-L($line_end)" }
33 let line_suffix = if ($start | is-empty) { "" } else { $"#L($start)($end)" }
34
35 # Construct the final URL
36 let url = $"https://($remote_url.host)($remote_url.path | str replace '.git' '')/blob/($ref)/($rel_path)($line_suffix)"
37
38 $url | wl-copy
39 print -e $"Copied to clipboard: ($url)"
40}