-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
128 lines (103 loc) · 3.54 KB
/
.functions
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
#!/usr/bin/env bash
# Create a new directory and enter it
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
nd() {
if [ -z "$1" ]; then
echo "Usage: $0 <script_file> [argument]"
exit
fi
script=$1
arg=$2
if [ -z "$arg" ]; then
node "$script.js"
else
node "$script.js" "$arg"
fi
}
function server() {
python3 -m http.server "$@"
}
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
function tre() {
tree -aC -I '.git|node_modules|bower_components' --dirsfirst "$@" | less -FRNX
}
function compare() {
hub compare "${1:=develop}"..."$(git rev-parse --abbrev-ref HEAD)"
}
function compare_branch() {
hub compare ${1:="$@"}..."$(git rev-parse --abbrev-ref HEAD)"
}
# Call from a local repo to open the repository on github/bitbucket in browser
# Modified version of https://github.com/zeke/ghwd
repo() {
# Figure out github repo base URL
local base_url
base_url=$(git config --get remote.origin.url)
base_url=${base_url%\.git} # remove .git from end of string
# Fix [email protected]: URLs
base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
# Fix git://github.com URLS
base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
# Fix [email protected]: URLs
base_url=${base_url//[email protected]:/https:\/\/bitbucket\.org\/}
# Fix [email protected]: URLs
base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
echo $base_url
# Validate that this folder is a git folder
if ! git branch 2>/dev/null 1>&2; then
echo "Not a git repo!"
exit $?
fi
# Find current directory relative to .git parent
full_path=$(pwd)
git_base_path=$(
cd "./$(git rev-parse --show-cdup)" || exit 1
pwd
)
relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
# If filename argument is present, append it
if [ "$1" ]; then
relative_path="$relative_path/$1"
fi
echo $relative_path
# Figure out current git branch
# git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
git_where=$(command git name-rev --name-only --no- undefined --always HEAD) 2>/dev/null
# Remove cruft from branchname
branch="${git_where#refs\/heads\/}"
branch="${git_where#remotes\/origin\/}"
branch="${branch#tags\/}"
branch="${branch%^0}"
[[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
url="$base_url/$tree/$branch$relative_path"
echo "Calling $(type open) for $url"
open "$url" &>/dev/null || (echo "Using $(type open) to open URL failed." && exit 1)
}
todo() {
ag --color-line-number '1;36' --color-path '1;36' --ignore-case --print-long-lines --silent '(?:<!-- *)?(?:#|//|/\*+|<!--|--) *(TODO|FIXME|FIX|BUG|UGLY|HACK|NOTE|IDEA|REVIEW|DEBUG|OPTIMIZE)(?:\([^(]+\))?:?(?!\w)(?: *-->| *\*/|(?= *(?:[^:]//|/\*+|<!--|@|--))|((?: +[^\n@]*?)(?= *(?:[^:]//|/\*+|<!--|@|--))|(?: +[^@\n]+)?))'
}
# Normalize `open` across Linux, macOS, and Windows.
# This is needed to make the `o` function (see below) cross-platform.
if [ ! "$(uname -s)" = 'Darwin' ]; then
if grep -q Microsoft /proc/version; then
# Ubuntu on Windows using the Linux subsystem
alias open='explorer.exe'
else
alias open='xdg-open'
fi
fi
# `o` with no arguments opens the current directory, otherwise opens the given
# location
o() {
if [ $# -eq 0 ]; then
open .
else
open "$@"
fi
}