aboutsummaryrefslogtreecommitdiff
path: root/src/git-permalink.sh
blob: 12f9fc84a66027e7fa72401313192074f4c22dce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh
set -eu

FILE="${1:-}"
MYLINENO="${2:-}"
COMMIT="$(git rev-parse HEAD)"
ORIGIN="$(git config remote.origin.url)"
OVERRIDE_CF="$(git config git-permalink.template-commit-file ||:)"
OVERRIDE_FC="$(git config git-permalink.template-file-commit ||:)"
REPOSITORY="$(basename "$PWD")"

usage() {
	printf 'Usage:  %s [-p] FILE [LINENO]\n' "$0"
}

help() {
	cat <<EOF

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
	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}"
}

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() {
	if [ -n "$OVERRIDE_CF" ]; then
		printf "$OVERRIDE_CF\n" "$COMMIT" "$FILE"
	elif [ -n "$OVERRIDE_FC" ]; then
		printf "$OVERRIDE_FC\n" "$FILE" "$COMMIT"
	else
		case "$ORIGIN" in
			*euandreh.xyz*)
				euandreh
				;;
			*git.sr.ht*)
				sourcehut
				;;
			*git.savannah.gnu.org*)
				savannah
				;;
			*gitlab.com*)
				gitlab
				;;
			*github.com*)
				github
				;;
			*)
				printf "Unsupported origin: %s.\n\n" $ORIGIN >&2
				printf 'Add an template override to use git-permalink (see "man git-permalink.1" for instructions).\n' >&2
				exit 1
				;;
		esac
	fi
}

LINK="$(guess_permalink)"
if [ "$PRINTONLY" = true ]; then
	echo "$LINK"
else
	echo "Opening $LINK" >&2
	xdg-open "$LINK"
fi