aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/git-permalink.sh48
1 files changed, 43 insertions, 5 deletions
diff --git a/src/git-permalink.sh b/src/git-permalink.sh
index 5202d29..11da86b 100755
--- a/src/git-permalink.sh
+++ b/src/git-permalink.sh
@@ -8,18 +8,50 @@ ORIGIN="$(git config --get remote.origin.url)"
REPOSITORY="$(basename "$PWD")"
usage() {
+ printf 'Usage: %s [-p] FILE [LINENO]\n' "$0"
+}
+
+help() {
cat <<EOF
-Usage:
- $0 FILE [LINENO]
+
+Options:
+ -p only print the link, don't try to open it
+ -h, --help show this help
+ -V, --version print the version number
EOF
}
+version() {
+ echo 'git-permalink-@VERSION@ @DATE@'
+}
+
if [ -z "$FILE" ]; then
- echo "Missing \$FILE argument"
+ printf "Missing \$FILE argument\n\n" >&2
usage >&2
exit 2
fi
+PRINTONLY=false
+for flag in $@; do
+ case "$flag" in
+ -h|--help)
+ usage
+ help
+ exit
+ ;;
+ -V|--version)
+ version
+ exit
+ ;;
+ -p)
+ PRINTONLY=true
+ shift
+ ;;
+ *)
+ ;;
+ esac
+done
+
euandreh() {
printf 'https://git.euandreh.xyz/%s/tree/%s?id=%s%s\n' "$REPOSITORY" "$FILE" "$COMMIT" "${MYLINENO:+#n$MYLINENO}"
}
@@ -78,10 +110,16 @@ guess_permalink() {
github
;;
*)
- echo "Unsupported origin: $ORIGIN"
+ echo "Unsupported origin: $ORIGIN" >&2
exit 1
;;
esac
}
-xdg-open "$(guess_permalink)"
+LINK="$(guess_permalink)"
+if [ "$PRINTONLY" = true ]; then
+ echo "$LINK"
+else
+ echo "Opening $LINK" >&2
+ xdg-open "$LINK"
+fi