aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-06-12 14:43:24 -0300
committerEuAndreh <eu@euandre.org>2021-06-12 14:43:24 -0300
commita3229f01c26721566df18cf34aa7af4d680214fd (patch)
tree7097dbd964bed0e9e3e637c29d6396389c525c47
parentTODOs.md: Fix bad header of #task-fd654661-fa97-83db-1d49-83a66866ccfa (diff)
downloadgit-permalink-a3229f01c26721566df18cf34aa7af4d680214fd.tar.gz
git-permalink-a3229f01c26721566df18cf34aa7af4d680214fd.tar.xz
src/git-permalink.sh: First working version
-rwxr-xr-xsrc/git-permalink.sh83
1 files changed, 76 insertions, 7 deletions
diff --git a/src/git-permalink.sh b/src/git-permalink.sh
index 13231ec..5202d29 100755
--- a/src/git-permalink.sh
+++ b/src/git-permalink.sh
@@ -1,18 +1,87 @@
#!/bin/sh
set -eu
+FILE="${1:-}"
+MYLINENO="${2:-}"
+COMMIT="$(git rev-parse HEAD)"
+ORIGIN="$(git config --get remote.origin.url)"
+REPOSITORY="$(basename "$PWD")"
+
usage() {
cat <<EOF
- $0 FILE [REF]
+Usage:
+ $0 FILE [LINENO]
EOF
}
-FILE="${1:-}"
if [ -z "$FILE" ]; then
- printf 'Missing FILE argument.\n'
- usage
- exit 1
+ echo "Missing \$FILE argument"
+ usage >&2
+ exit 2
fi
-# cgit, sourcehut, savannah, codeberg, notabug, pagure, bitbucket, gitlab, github
-# Bitbucket Codeberg GitHub GitLab Pagure SourceHut
+euandreh() {
+ printf 'https://git.euandreh.xyz/%s/tree/%s?id=%s%s\n' "$REPOSITORY" "$FILE" "$COMMIT" "${MYLINENO:+#n$MYLINENO}"
+}
+
+sourcehut() {
+ printf '%s/tree/%s/item/%s%s\n' "$ORIGIN" "$COMMIT" "$FILE" "${MYLINENO:+#L$MYLINENO}"
+}
+
+savannah() {
+ printf '%s/tree/%s?id=%s%s\n' "$(echo "$ORIGIN" | sed 's|gnu.org/git|gnu.org/cgit|')" "$FILE" "$COMMIT" "${MYLINENO:+#n$MYLINENO}"
+}
+
+gitlab() {
+ if echo "$ORIGIN" | grep -q '^git@gitlab.com:'; then
+ NAME="$(echo "$ORIGIN" | cut -d: -f2 | cut -d/ -f1)"
+ ORIGIN="https://gitlab.com/$NAME/$REPOSITORY"
+ fi
+ printf '%s/-/blob/%s/%s%s\n' "$ORIGIN" "$COMMIT" "$FILE" "${MYLINENO:+#L$MYLINENO}"
+}
+
+damnyou_github() {
+ if echo "$MYLINENO" | grep -q -- -; then
+ P1="$(echo "$MYLINENO" | cut -d- -f1)"
+ P2="$(echo "$MYLINENO" | cut -d- -f2)"
+ printf '#L%s-L%s' "$P1" "$P2"
+ elif [ -n "$MYLINENO" ]; then
+ printf '#L%s' "${MYLINENO}"
+ else
+ printf ''
+ fi
+}
+
+github() {
+ if echo "$ORIGIN" | grep -q '^git@github.com:'; then
+ NAME="$(echo "$ORIGIN" | cut -d: -f2 | cut -d/ -f1)"
+ ORIGIN="https://github.com/$NAME/$REPOSITORY"
+ fi
+ printf '%s/blob/%s/%s%s\n' "$ORIGIN" "$COMMIT" "$FILE" "$(damnyou_github)"
+}
+
+guess_permalink() {
+ case "$ORIGIN" in
+ *euandreh.xyz*)
+ euandreh
+ ;;
+ *git.sr.ht*)
+ sourcehut
+ ;;
+ *git.savannah.gnu.org*)
+ savannah
+ ;;
+ *gitlab.com*)
+ gitlab
+ ;;
+ *github.com*)
+ github
+ ;;
+ *)
+ echo "Unsupported origin: $ORIGIN"
+ exit 1
+ ;;
+ esac
+}
+
+xdg-open "$(guess_permalink)"