-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bashrc.functions
192 lines (152 loc) · 5.29 KB
/
.bashrc.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
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
#!/usr/bin/env bash
ssh() {
local parameters=("${@}")
_change_titles && { tmux rename-window " ${parameters[-1]}"; _set_title "${parameters[-1]}"; }
command ssh "${@}"
_change_titles && { tmux set-window-option automatic-rename "on" 1>/dev/null; }
}
git() {
local parameters=("${@}")
local use_defaults=false
if [[ "${parameters[0]}" == "clone" ]]; then
[[ "${parameters[1]}" == "--use-defaults" ]] && { unset "parameters[1]"; use_defaults=true; }
fi
command git "${parameters[@]}"
if [[ "${parameters[0]}" == "clone" ]]; then
_git_configure_repo ${use_defaults} "${parameters[-1]}"
fi
}
_set_title() {
# shellcheck disable=SC1003
echo -e '\033k'"${*}"'\033\\'
}
__append_prompt_command() {
if [[ -n $1 ]] ; then
case $PROMPT_COMMAND in
*$1*) ;;
"") PROMPT_COMMAND="$1";;
*) PROMPT_COMMAND="$PROMPT_COMMAND;$1";;
esac
fi
}
# shellcheck disable=SC2120
_get_git_user() {
local check="${1:-false}"
if ${check}; then
local repository_directory
repository_directory="$(git rev-parse --show-toplevel 2>/dev/null)"
[[ -z "${repository_directory}" ]] && return 0
fi
local url
url="$(git config --get remote.origin.url)"
if [[ "${url}" == *"github.com"* ]]; then
echo "${url}" | sed -r 's|.*[:]?[/]{2}?github.com[:/]?(.*)/.*|\1|g'
elif [[ "${url}" == *"@"* ]]; then
echo "${url}" | awk -F@ '{gsub(/^.*?:\/\//, "", $1); print $1}'
else
echo "${USER}"
fi
}
_change_titles() {
[[ -n "${TMUX}" && -z "${SSH_CLIENT}" ]]
}
_git_configure_repo() {
local use_defaults="${1}"
local repo_dir="${2}"
# if last argument wasn't the directory the repo was cloned to, use the
# latest created directory/file in the current directory
# shellcheck disable=SC2012
! [[ -d "${repo_dir}" ]] && repo_dir="$(ls -t | head -1)"
# make sure that the directory actually is a directory, and that a git
# directory exists
! [[ -d "${repo_dir}" && -e "${repo_dir}/.git" ]] && return 0
export GIT_DIR="${repo_dir}/.git"
export GIT_WORK_TREE="${repo_dir}"
local user_name user_email
if [[ -z "$(git config --get user.name)" ]]; then
if [[ -z "${GIT_USER_NAME}" ]]; then
local default_user_name
default_user_name="$(getent passwd "${USER}" | awk -F: '{gsub(",", " ", $5); gsub(/[ \t]+$/, "", $5); print $5}')"
if ! ${use_defaults}; then
echo -n "Enter your name [${default_user_name}]: "
read -r user_name
fi
[[ -z "${user_name}" ]] && user_name="${default_user_name}"
else
user_name="${GIT_USER_NAME}"
fi
git config user.name "${user_name}"
fi
if [[ -z "$(git config --get user.email)" ]]; then
if [[ -z "${GIT_USER_EMAIL}" ]]; then
local default_user_email
default_user_email="${USER}@$(hostname -f)"
if ! ${use_defaults}; then
echo -n "Enter your e-mail [${default_user_email}]: "
read -r user_email
fi
[[ -z "${user_email}" ]] && user_email="${default_user_email}"
else
user_email="${GIT_USER_EMAIL}"
fi
git config user.email "${user_email}"
fi
unset GIT_DIR GIT_WORK_TREE
}
tmux_git_window_name() {
_change_titles || return 0
local repository_directory
repository_directory="$(git rev-parse --show-toplevel 2>/dev/null)"
if [[ -z "${repository_directory}" ]]; then
tmux set-window-option automatic-rename "on" 1>/dev/null
else
local name repository
repository="$(basename "${repository_directory}")"
name="$(_get_git_user "")"
[[ -n "${name}" ]] && name+=":"
tmux set-window-option automatic-rename "off" 1>/dev/null
#tmux rename-window " #[nobold]${name}#[bold]${repository}/#[fg=colour237,nobold]${SCM_BRANCH}"
tmux rename-window "${name}${repository}/$(_git-friendly-ref)"
#_set_title "git ${name}${repository}/${SCM_BRANCH}"
fi
}
if [[ "${TERM}" == screen* && -z "${TMUX}" ]]; then
preexec() { :; }
screen_window_title() {
[[ -n "${COMP_LINE}" ]] && return
[[ "${BASH_COMMAND}" == "${PROMPT_COMMAND}" ]] && return
local cmd
cmd=$(HISTTIMEFORMAT='' history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
local title="${cmd}"
if [[ "${title}" == "ssh "* ]]; then
title="${title#ssh }"
else
(( "${#title}" > 15 )) && title="${title:0:15}..."
fi
# shellcheck disable=SC1003
printf '\ek%s\e\\' "${title}"
preexec "${cmd}"
}
trap 'screen_window_title' DEBUG
fi
pet-add() {
local cmd
cmd="$(fc -lrn | awk 'NR==2{gsub(/^[ \t]+/, "", $0); print $0; exit}')"
bash -c "pet new $(printf "%q" "${cmd}")"
}
run_in_docker() {
local executable="${FUNCNAME[1]#_}"
local name="${1}"
shift
docker start "${name}" &> /dev/null || docker run -it -d --name "${name}" -h "${name}" -v "${HOME}:${HOME}" "mgor/${name}" /bin/bash &> /dev/null || true
docker exec -i "${name}" "${executable}" "$@"
}
_php() {
run_in_docker phpqa "$@"
}
_phpcs() {
run_in_docker phpqa "$@"
}
_phpmd() {
run_in_docker phpqa "$@"
}