-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-gh-pager
executable file
·47 lines (41 loc) · 1.5 KB
/
git-gh-pager
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
#!/bin/sh
# Turn references to objects in github into links.
#
# To use, set `git config --global core.pager git-gh-pager` and then
# set `git config gh-pager.project username/repository` in your clone
# of a repo.
#
# This uses OSC 8 - see https://github.com/Alhadis/OSC8-Adoption
# Don't mess with the output if we're piping it into something.
[ -t 1 ] || exec less
# If no repo is configured, just give up.
project="$(git config gh-pager.project)"
[ -n "$project" ] || exec less
# Turn a URL into a link using the OSC 8 escape.
osc8() {
url=$1
text=$2
echo "\e]8;;$url\a$text\e]8;;\a"
}
# Print a URL to an item in github.
gh() {
project="$1"
path="$2"
item="$3"
text="$4"
[ -n "$text" ] || text="$item"
osc8 "https://github.com/$project/$path/$item" "$text"
}
# Use a heuristic to make sure short strings that look like hashes don't do
# something silly - git estimates a reasonable abbreviation length of commits.
min=$(( $(git rev-parse --short HEAD | wc -c) - 1 ))
# Handle commit hashes, issue/PR numbers, and cross-repo issues/PRs. Note PRs
# just use the issue link since git will do a redirect.
#
# Note: for hashes, we treat a leading 'm' as a word boundary, this is a
# heuristic to allow us to handle ANSI colour escapes.
perl -p \
-e 's|((?<=m)\|\b)[a-f0-9]{'$min',40}\b|'$(gh $project commit '$&')'|g;' \
-e 's|(?<!\w)(?:#\|GH-)(\d+)\b|'$(gh $project issues '$1' '$&')'|g;' \
-e 's|\b([\w\d-]+/[\w\d-._]+)#(\d+)|'$(gh '$1' issues '$2' '$&')'|g' \
| less