+/config

my dotfiles and nix configuration

git clone https://git.julia.blue/+/config

Julia RyanFix helix permalinking69ec940

main
2.2 KiB104 linesraw
1#!/usr/bin/env bash
2
3# set -x
4set -euo pipefail
5
6if [[ $# -lt 1 || $# -gt 3 ]]; then
7  echo "usage: $0 <file> [line] [line_end]" >&2
8  exit 1
9fi
10
11file="$1"
12line="${2:-}"
13line_end="${3:-}"
14
15root="$(git rev-parse --show-toplevel 2>/dev/null)" || {
16  echo "not in a git repository" >&2
17  exit 1
18}
19
20file="$(realpath --relative-to="$root" "$file")"
21git ls-files --error-unmatch "$file" >/dev/null 2>&1 || {
22  echo "file is not tracked by git: $file" >&2
23  exit 1
24}
25
26commit="$(git rev-parse HEAD)"
27
28# TODO: some heuristic for still using links on upstream if the given file hasn't been modified in a local branch/by this commit
29choose_remote() {
30  if git remote get-url upstream >/dev/null 2>&1; then
31    if git branch -r --contains "$commit" | grep -q '^ *upstream/'; then
32      echo upstream
33      return
34    fi
35  fi
36  echo origin
37}
38
39remote_name="$(choose_remote)"
40remote_url="$(git remote get-url "$remote_name")"
41remote="$(echo "$remote_url" | sd "([^:]+):" '$1/' | sd "^git@" https://)"
42
43base_url=""
44blob_path="blob"
45
46case "$remote_url" in
47  git@github.com:*)
48    repo="${remote_url#git@github.com:}"
49    repo="${repo%.git}"
50    base_url="https://github.com/$repo"
51    blob_path="blob"
52    ;;
53  https://github.com/*)
54    repo="${remote_url#https://github.com/}"
55    repo="${repo%.git}"
56    base_url="https://github.com/$repo"
57    blob_path="blob"
58    ;;
59
60  git@git.sr.ht:*)
61    repo="${remote_url#git@git.sr.ht:}"
62    base_url="https://git.sr.ht/$repo"
63    blob_path="tree"
64    ;;
65  https://git.sr.ht/*)
66    repo="${remote_url#https://git.sr.ht/}"
67    base_url="https://git.sr.ht/$repo"
68    blob_path="tree"
69    ;;
70
71  ssh://forgejo@git.donsz.nl/*)
72    repo="${remote_url#ssh://forgejo@git.donsz.nl/}"
73    base_url="https://git.donsz.nl/$repo"
74    blob_path="src/commit"
75    ;;
76
77  *)
78    echo "unsupported remote: $remote_url" >&2
79    exit 1
80    ;;
81esac
82# echo $remote_url
83# echo $repo
84# echo $base_url
85
86url="$base_url/$blob_path/$commit/$file"
87
88if [[ -n "$line" ]]; then
89  if [[ -n "$line_end" && "$line_end" != "$line" ]]; then
90    case "$base_url" in
91      *git.sr.ht*)
92        url="$url#L$line-$line_end"
93        ;;
94      *)
95        url="$url#L$line-L$line_end"
96        ;;
97    esac
98  else
99    url="$url#L$line"
100  fi
101fi
102
103echo "$url"
104