forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-open-commit-history.sh
executable file
·85 lines (70 loc) · 2.3 KB
/
github-open-commit-history.sh
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
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Open Commit History
# @raycast.mode silent
# Optional parameters:
# @raycast.icon ./images/github-logo.png
# @raycast.iconDark ./images/github-logo-iconDark.png
# @raycast.packageName GitHub
# Documentation:
# @raycast.author Thomas Paul Mann
# @raycast.authorURL https://github.com/thomaspaulmann
# @raycast.description Opens the commit history of the filepath in the clipboard or the frontmost window. Alternatively can open the file view on GitHub.
# Configuration:
# Set the branch to open on GitHub.
BRANCH="master"
# Set the page to open for the file. Available values: commits, blob, blame
PAGE="commits"
# Enable to get verbose output.
DEBUG=false
# Utils:
get_current_filepath_of_frontmost_application() {
osascript -e '
tell application "System Events" to set frontmostApplication to name of first process whose frontmost is true
if (frontmostApplication is "Finder") then
tell application "Finder"
get POSIX path of (selection as alias)
end tell
else if (frontmostApplication is "iTerm2") then
tell application "iTerm"
tell current session of current window
get variable named "session.path"
end tell
end tell
else
tell application "System Events"
tell (first process where frontmost is true)
get value of attribute "AXDocument" of first window
end tell
end tell
end if
'
}
# Main:
CLIPBOARD_CONTENTS=$(pbpaste)
if [[ -d $CLIPBOARD_CONTENTS || -f $CLIPBOARD_CONTENTS ]]; then
FILEPATH=$CLIPBOARD_CONTENTS
else
FRONTMOST_FILEPATH=$(get_current_filepath_of_frontmost_application)
FILEPATH=${FRONTMOST_FILEPATH#"file://"}
fi
if [ "$DEBUG" = true ] ; then
echo "Extracted filepath: $FILEPATH"
fi
if [[ -d $FILEPATH || -f $FILEPATH ]]; then
cd `dirname $FILEPATH`
if git rev-parse > /dev/null 2>&1; then
GIT_REPOSITORY_ROOT_PATH=$(git rev-parse --show-toplevel)
RELATIVE_FILEPATH=${FILEPATH#"$GIT_REPOSITORY_ROOT_PATH/"}
BASE=$(git config --get remote.origin.url | sed s/\\.git// | sed 's/:/\//' | sed 's/.*github.com/https:\/\/github.com/')
URL="$BASE/$PAGE/$BRANCH/$RELATIVE_FILEPATH"
open $URL
else
echo "Not a Git repostiory"
exit 1
fi
else
echo "Not a valid filepath"
exit 1
fi