forked from daler/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
123 lines (102 loc) · 4.16 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
#!/usr/bin/env bash
# Start the ssh-agent and add keys to the agent.
# Detects if on Mac; if so detects the macOS version and provides the
# appropriate pargs to add keys to the keychain
function start_agent {
echo "Initializing new SSH agent..."
eval "$(ssh-agent)"
additional_arg=""
if [[ $OSTYPE == darwin* ]]; then
if [[ $(sw_vers -productVersion | cut -f1 -d '.') -lt 12 ]]; then
additional_arg="-K"
else
additional_arg="--apple-use-keychain"
fi
ssh-add "$additional_arg"
else
ssh-add
fi
}
# `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|env|.snakemake|node_modules|bower_components' --dirsfirst "$@" | less -FRNX;
}
# Set up fzf, if it's installed
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
# Select a conda env to activate
sa() {
local name=$(conda env list | grep -v "#" | fzf)
local env=$(echo $name | awk '{print $1}');
eval "source activate $env";
}
function prsetup {
if [ -z $1 ]; then
echo "Run this function in a clone of a repo with a pull request to set up"
echo "to push back to the contributor's fork. When creating the PR, the "
echo "contributor must have checked the box, 'Allow edits from maintainers'."
echo "The first argument can be determined by looking at the top of the "
echo "PR and copying the <contributor:branch>."
echo
echo "Usage: prsetup <contributor:branch>"
echo
echo "(assumes same name of repo as current origin)"
echo
return 1
fi
current_remote=$(git remote -v | grep "^origin" | grep "(fetch)" | cut -f2 | cut -f1 -d " ")
current_repo_name=$(basename $current_remote)
contributor=$(echo $1 | cut -f1 -d ":")
contributor_branch=$(echo $1 | cut -f2 -d ":")
[email protected]:$contributor/$current_repo_name.git
echo "The following are the detected variables:"
echo "current_remote: " $current_remote
echo "current_repo_name: " $current_repo_name
echo "contributor_branch: " $contributor_branch
echo "contributor: " $contributor
echo "contributor_fork: " $contributor_fork
git remote add $contributor $contributor_fork
git fetch $contributor
git checkout -b $contributor-$contributor_branch $contributor/$contributor_branch
echo "Now make changes, and run:"
echo " git push $contributor HEAD:$contributor_branch"
}
function ca() {
# This function allows you to activate the base env only when you're ready
# to (and don't activate it on EVERY new shell).
#
# You can also provide an env name or path to activate it.
eval "$(conda shell.bash hook 2> /dev/null)"
conda activate "$@"
}
function conda_deactivate_all() {
# Keep deactivating until we no longer have a CONDA_PREFIX env var (which
# might be a few times, if we had activated the base env and then another
# env)
eval "$(conda shell.bash hook 2> /dev/null)"
while [ -n "$CONDA_PREFIX" ]; do
conda deactivate;
done
}
function hostlist() {
# Parse ~/.ssh/config and print out a nicely-formatted table of configured
# hosts. Useful for when you're trying to remember how to log in to an
# infrequently-accessed host.
paste \
<(grep -i "^Host" ~/.ssh/config | grep -v "*" | awk '{print $2}') \
<(grep -i "Hostname" ~/.ssh/config | grep -v "*" | awk '{print $2}') \
| column -t \
| sort
}
function git-clean-branches () {
# Print local branches that have been merged into main (or master).
#
# Auto-detect whether to use main/master.
local DEFAULT_BRANCH=$(git branch | grep -Ew "main|master" | sed "s/\* //")
# Just print the names. If you're on a branch that has been merged (has
# a "*"), don't print it
git branch --merged $DEFAULT_BRANCH | grep -v "\*" | grep -Evw $DEFAULT_BRANCH
echo "Use 'git-clean-branches | xargs git branch -d' to actually delete these." >&2
}