forked from sukima/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools
110 lines (100 loc) · 2.8 KB
/
tools
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
#!/bin/bash
function randpass {
passlength=6
[ ! -z "$1" ] && passlength=$1
printf "%s\n" `< /dev/urandom tr -dc A-Za-z0-9 | head -c$passlength`
}
function ghblame {
local fullpath=$1
local path_no_lineno=${fullpath%:*}
if [[ ! -e $path_no_lineno ]]; then
printf "%s %s\n" "$path_no_lineno" "does not exist" >&2
return 1
fi
local lineno=${fullpath#*:}
if [[ $lineno =~ [0-9]+ ]]; then
lineno="#L$lineno"
else
lineno=""
fi
local abs_path=$(cd "${path_no_lineno%/*}" &>/dev/null; printf "%s/%s" "$(pwd)" "${path_no_lineno##*/}")
local file_dir=${abs_path%/*}
local in_git_repo
[ -d $file_dir/.git ] || (cd $file_dir; git rev-parse --git-dir) > /dev/null 2>&1
in_git_repo=$?
if [[ $in_git_repo != 0 ]]; then
printf "%s\n" "Not in a git repo" >&2
return in_git_repo
fi
local root=$(cd $file_dir >/dev/null; git rev-parse --show-toplevel)
local ref
ref=$(cd $root >/dev/null; git symbolic-ref HEAD 2>/dev/null)
if [[ $? != 0 ]]; then
printf "%s\n" "Not on any branch" >&2
return 1
fi
local branch=${ref#refs/heads/}
local remote_name=$(cd $root >/dev/null; git config branch.$branch.remote 2>/dev/null)
local remote_url=$(cd $root >/dev/null; git config remote.$remote_name.url 2>/dev/null)
if [[ ! $remote_url =~ 'github.com' ]]; then
printf "%s\n" "Upstream is not a github repo" >&2
return 1
fi
local remote_dir
remote_dir=${remote_url#[email protected]:}
remote_dir=${remote_dir#https://github.com/}
local rel_path=${abs_path#$root}
open "https://github.com/$remote_dir/blame/$branch$rel_path$lineno"
}
# A util function to setup missing file paths that some programs and scrips
# expect.
function setupTempPaths {
mkdir -p ~/tmp/sessions
touch ~/.tmux.conf.local
touch ~/.vim/local.vim
touch ~/.vim/local_gui.vim
touch ~/.bash_aliases
if [[ ! -e ~/.bash_modules ]]; then
cat > ~/.bash_modules <<-"EOF"
# bash_modules - defines what modules to load into the bash environment
# Repository at http://github.com/sukima/dotfiles
# Generated for the first time by repository install.sh.
#
# Uncomment any of the following modules to load on next shell invocation.
EOF
for p in ~/.homesick/repos/dotfiles/bash/modules.d/*; do
echo
egrep "^#[^!][ ]*[^ ]" $p | head -n1
echo "# $(basename $p)"
done >> ~/.bash_modules
fi
}
# Convinience method to check if a command exists or not.
function command_exists {
hash "$1" &> /dev/null
}
# Utility functions to manipulate the PATH.
# Used to prevent duplicates on multiple calls.
function inPath {
echo "${PATH}" | grep "$1" > /dev/null
}
function notInPath {
if inPath "$1"; then
return 1
else
return 0
fi
}
function addToPath {
if notInPath "$1"; then
PATH="${PATH}:$1"
fi
export PATH
}
function prependToPath {
if notInPath "$1"; then
PATH="$1:${PATH}"
fi
export PATH
}
# vim: ft=sh sw=2 ts=2 noet fdm=marker