-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_utils
140 lines (114 loc) · 3.36 KB
/
.bash_utils
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
#!/bin/bash
# ----------------------------------------------------------------------
# Enables "cd --" "cd -[0-9]" syntax {{{
# ----------------------------------------------------------------------
# do ". acd_func.sh"
# acd_func 1.0.5, 10-nov-2004
# petar marinov, http:/geocities.com/h2428, this is public domain
cd_func ()
{
local x2 the_new_dir adir index
local -i cnt
if [[ $1 == "--" ]]; then
dirs -v
return 0
fi
the_new_dir=$1
[[ -z $1 ]] && the_new_dir=$HOME
if [[ ${the_new_dir:0:1} == '-' ]]; then
#
# Extract dir N from dirs
index=${the_new_dir:1}
[[ -z $index ]] && index=1
adir=$(dirs +$index)
[[ -z $adir ]] && return 1
the_new_dir=$adir
fi
#
# '~' has to be substituted by ${HOME}
[[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"
#
# Now change to the new dir and add to the top of the stack
pushd "${the_new_dir}" > /dev/null
[[ $? -ne 0 ]] && return 1
the_new_dir=$(pwd)
#
# Trim down everything beyond 11th entry
popd -n +11 2>/dev/null 1>/dev/null
#
# Remove any other occurence of this dir, skipping the top of the stack
for ((cnt=1; cnt <= 10; cnt++)); do
x2=$(dirs +${cnt} 2>/dev/null)
[[ $? -ne 0 ]] && return 0
[[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
if [[ "${x2}" == "${the_new_dir}" ]]; then
popd -n +$cnt 2>/dev/null 1>/dev/null
cnt=cnt-1
fi
done
return 0
}
alias cd=cd_func
if [[ $BASH_VERSION > "2.05a" ]]; then
# ctrl+w shows the menu
bind -x "\"\C-w\":cd_func -- ;"
fi
# ----------------------------------------------------------------------
# }}} Run a command for a given commit and repo {{{
# ----------------------------------------------------------------------
with () {
# the repo directory
REPO=$1
# the commit hash
COMMIT=$2
# the command you want to run
CMD=${@:3:500}
WD=$(pwd)
cd $REPO
CURRENT_STATE=$(git branch | awk '/\*/ { print $2; }')
# try checking out. If fatal, git stash, then checkout
if git checkout $COMMIT; then
NO_STASH=1
else
git stash
NO_STASH=0
git checkout $COMMIT
fi
cd $WD
echo -e "=======================================================================\n"
eval $CMD
echo -e "\n======================================================================="
cd $REPO
git checkout $CURRENT_STATE
if [ "$NO_STASH" -eq "0" ]; then
git stash pop
fi
cd $WD
}
# ----------------------------------------------------------------------
# }}} Git search and replace repo {{{
# ----------------------------------------------------------------------
gitsed () {
ORIG=$1
REPLACE=$2
SUBDIR=$3
git grep -l "${ORIG}" ${SUBDIR} | xargs sed -i "" -e "s/${ORIG}/${REPLACE}/g"
}
# ----------------------------------------------------------------------
# }}} Make a conda environment. Remove existing if it exists {{{
# ----------------------------------------------------------------------
cm () {
NAME=$1
PYTHON_VERSION=${2:-3.10} # Set PYTHON_VERSION to the 2nd argument or default to "3.10" if not provided
conda deactivate
conda deactivate
conda env remove --name $NAME
conda create -y -n $NAME python=$PYTHON_VERSION
conda activate $NAME
}
cx () {
NAME=$1
conda deactivate
conda deactivate
conda env remove --name $NAME
}