aboutsummaryrefslogtreecommitdiff
path: root/src/git-permalink.in
blob: 0334ecb10256c9dc94a8dd9c90c4a01c7a3d5a16 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/sh
set -eu

MSGS=''
add_msg() {
	MSGS="$MSGS $1"
}


MSG_USAGE="$(cat <<-'EOF'
	Usage:  git permalink [-phV] FILE [LINENO]
EOF
)"
add_msg USAGE

MSG_HELP="$(cat <<-'EOF'
	Options:
	  -p             only print the link, don't try to open it
	  -h, --help     show this help message
	  -V, --version  print the version number

	See "man git-permalink" for more information.
EOF
)"
add_msg HELP

MSG_MISSING_FILE="$(cat <<-'EOF'
	Missing FILE argument.
EOF
)"
add_msg MISSING_FILE

MSG_UNSUPPORTED_ORIGIN="$(cat <<-'EOF'
	Unsupported origin: %s.

	Add a template override to use git-permalink (see
	"man git-permalink" for instructions).
EOF
)"
add_msg UNSUPPORTED_ORIGIN

MSG_OPEN="$(cat <<-'EOF'
	Opening %s
EOF
)"
add_msg OPEN

#
# End translatable strings
#



dump_translatable_strings() {
	for msg in $MSGS; do
		eval "echo \"\$MSG_$msg\"" > src/locale/"$msg".en.txt
	done

	cat <<-'EOF'
		#!/bin/sh
		# shellcheck disable=2034
		set -eu

	EOF
	for msg in $MSGS; do
		# shellcheck disable=2016
		printf "MSG_%s=\"\$(cat '@LOCALEDIR@/@LANG@/LC_MESSAGES/@NAME@/%s.@LANG@.txt')\"\n" \
			"$msg" "$msg"
	done
}

if [ -n "${GIT_PERMALINK_DUMP_TRANSLATABLE_STRINGS:-}" ]; then
	dump_translatable_strings
	exit
fi


if [ -r '@LIBEXECDIR@/@NAME@/load-messages.sh' ]; then
	# shellcheck disable=1091
	. '@LIBEXECDIR@/@NAME@/load-messages.sh'
fi



#
# Documentation functions
#

usage() {
	printf '%s\n' "$MSG_USAGE"
}

help() {
	printf '\n%s\n' "$MSG_HELP"
}

version() {
	printf 'git-permalink-@VERSION@ @DATE@\n'
}



#
# Core functions
#

normalize_origin() {
	if echo "$ORIGIN" | grep -q '^git@'; then
		NAME="$(echo "$ORIGIN" | cut -d: -f2 | cut -d/ -f1)"
		URL="$(echo "$ORIGIN" | cut -d@ -f2 | cut -d: -f1)"
		ORIGIN="https://$URL/$NAME/$REPOSITORY"
	fi
}

lineno_with_l() {
	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
}

euandreh() {
	printf 'https://euandreh.xyz/%s.git/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}"
}

kernel() {
	printf '%s/tree/%s?id=%s%s\n' \
		"$ORIGIN" "$FILE" "$COMMIT" "${MYLINENO:+#n$MYLINENO}"
}

savannah() {
	ORIGIN="$(echo "$ORIGIN" | sed 's|gnu.org/git|gnu.org/cgit|')"
	printf '%s/tree/%s?id=%s%s\n' \
		"$ORIGIN" "$FILE" "$COMMIT" "${MYLINENO:+#n$MYLINENO}"
}

notabug() {
	normalize_origin
	printf '%s/src/%s/%s%s\n' "$ORIGIN" "$COMMIT" "$FILE" "$(lineno_with_l)"
}

codeberg() {
	normalize_origin
	printf '%s/src/commit/%s/%s%s\n' \
		"$ORIGIN" "$COMMIT" "$FILE" "$(lineno_with_l)"
}

bitbucket() {
	normalize_origin
        MYLINENO="${MYLINENO:+#lines-$(echo "$MYLINENO" | tr '-' ':')}"
	printf '%s/src/%s/%s%s\n' \
		"$ORIGIN" "$COMMIT" "$FILE" "$MYLINENO"
}

pagure() {
	printf '%s/blob/%s/f/%s%s\n' \
		"$ORIGIN" "$COMMIT" "$FILE" "${MYLINENO:+#_$MYLINENO}"
}

gitlab() {
	normalize_origin
	printf '%s/-/blob/%s/%s%s\n' \
		"$ORIGIN" "$COMMIT" "$FILE" "${MYLINENO:+#L$MYLINENO}"
}

github() {
	normalize_origin
	printf '%s/blob/%s/%s%s\n' \
		"$ORIGIN" "$COMMIT" "$FILE" "$(lineno_with_l)"
}

guess_permalink() {
	if [ -n "$OVERRIDE_CF" ]; then
		# shellcheck disable=2059
		printf "$OVERRIDE_CF\n" "$COMMIT" "$FILE"
	elif [ -n "$OVERRIDE_FC" ]; then
		# shellcheck disable=2059
		printf "$OVERRIDE_FC\n" "$FILE" "$COMMIT"
	else
		case "$ORIGIN" in
			*euandreh.xyz*)
				euandreh
				;;
			*git.sr.ht*)
				sourcehut
				;;
			*git.kernel.org*)
				kernel
				;;
			*git.savannah.gnu.org*)
				savannah
				;;
			*notabug.org*)
				notabug
				;;
			*codeberg.org*)
				codeberg
				;;
			*bitbucket.org*)
				bitbucket
				;;
			*pagure.io*)
				pagure
				;;
			*gitlab.com*)
				gitlab
				;;
			*github.com*)
				github
				;;
			*)
				# shellcheck disable=2059
				printf "$MSG_UNSUPPORTED_ORIGIN\n" "$ORIGIN" >&2
				exit 1
				;;
		esac
	fi
}



#
# Main
#

for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		--version)
			version
			exit
			;;
		*)
			;;
	esac
done

PRINTONLY=false
while getopts 'phV' flag; do
	case "$flag" in
		p)
			PRINTONLY=true
			;;
		h)
			usage
			help
			exit
			;;
		V)
			version
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

if [ -z "${1:-}" ]; then
	printf '%s\n\n' "$MSG_MISSING_FILE" >&2
	usage >&2
	exit 2
fi

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

LINK="$(guess_permalink)"
if [ "$PRINTONLY" = true ]; then
	echo "$LINK"
else
	# shellcheck disable=2059
	printf "$MSG_OPEN\n" "$LINK" >&2
	xdg-open "$LINK"
fi