Skip to content

Commit 2ded660

Browse files
committedFeb 26, 2025
improved git init functions
1 parent e81e8ec commit 2ded660

File tree

1 file changed

+88
-14
lines changed

1 file changed

+88
-14
lines changed
 

‎script-snippets/.bash_rc.sh

+88-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,73 @@ alias k='kubectl'
33
alias pods='kubectl get pods -w'
44
alias pod='kubectl get pods -w'
55

6+
function git_default_branch(){
7+
git for-each-ref --format='%(refname:short)' refs/heads/ | grep -xq "main" && echo -n "main" || echo -n "master"
8+
}
9+
10+
function git_init_global(){
11+
GIT_USER_NAME=$1
12+
GIT_USER_EMAIL=$2
13+
14+
if [[ -z "$GIT_USER_NAME" ]] || [[ -z "$GIT_USER_EMAIL" ]]; then
15+
echo "enter your git user.name and user.email with parameter 1 and 2"
16+
return
17+
fi
18+
19+
if [[ ! -f ~/.ssh/id_rsa.pub ]]; then
20+
echo "SSH public key not found"
21+
return
22+
fi
23+
24+
git config --global alias.co checkout
25+
git config --global alias.chekcout checkout
26+
git config --global alias.st status
27+
git config --global alias.br branch
28+
git config --global alias.cm 'commit -s'
29+
30+
# setup name & email
31+
git config --global user.name "$GIT_USER_NAME"
32+
git config --global user.email "$GIT_USER_EMAIL"
33+
34+
# by default, sign commit using SSH public key
35+
git config --global user.signingkey "$HOME/.ssh/id_rsa.pub"
36+
git config --global gpg.format ssh
37+
git config --global commit.gpgsign true
38+
}
39+
40+
function git_init_repo(){
41+
if [[ ! -d ./.git/hooks ]]; then
42+
git init .
43+
# echo "Current working directory is not a git repository"
44+
# return
45+
fi
46+
47+
# add auto sign off commit template
48+
cat <<EOF > .git/hooks/prepare-commit-msg
49+
#!/bin/sh
50+
51+
NAME=$(git config user.name)
52+
EMAIL=$(git config user.email)
53+
54+
if [ -z "\$NAME" ]; then
55+
echo "empty git config user.name"
56+
exit 1
57+
fi
58+
59+
if [ -z "\$EMAIL" ]; then
60+
echo "empty git config user.email"
61+
exit 1
62+
fi
63+
64+
git interpret-trailers --if-exists doNothing --trailer \
65+
"Signed-off-by: \$NAME <\$EMAIL>" \
66+
--in-place "\$1"
67+
EOF
68+
69+
}
70+
671
function gsync(){
7-
DEFAULT_BRANCH=$(git for-each-ref --format='%(refname:short)' refs/heads/ | grep -xq "main" && echo -n "main" || echo -n "master")
72+
DEFAULT_BRANCH=$(git_default_branch)
873
UPSREAM_BRANCH=$1
974
if [[ -z "$UPSREAM_BRANCH" ]]; then
1075
UPSREAM_BRANCH=$DEFAULT_BRANCH
@@ -17,6 +82,27 @@ function gsync(){
1782
fi
1883
}
1984

85+
function br(){
86+
BR=$(git --no-pager branch | gum filter --placeholder 'select a branch')
87+
if [[ "${BR:0:2}" != '* ' ]]; then
88+
git checkout $(echo $BR | tr -d ' ')
89+
fi
90+
}
91+
92+
function br_del(){
93+
BRANCH=$1
94+
if [[ ! -f "./.git/refs/heads/$BRANCH" ]]; then
95+
echo "branch '$BRANCH' does not exist"
96+
return
97+
fi
98+
99+
UPSTREAM=$(git for-each-ref --format='%(upstream:short)' "refs/heads/$BRANCH")
100+
101+
git checkout $(git_default_branch)
102+
git branch -D "$BRANCH"
103+
git push $(echo $UPSTREAM | cut -d '/' -f 1) "$BRANCH" --delete
104+
}
105+
20106
function kuse(){
21107
CLS=$1
22108
if [[ ! -z "$CLS" ]]; then
@@ -163,7 +249,7 @@ alias preview-release="$HOME/go/src/github.com/jijiechen/kong-workspace/kong-mes
163249
alias clera='clear'
164250
alias pdos='pods'
165251

166-
export PATH=$HOME/go/bin:$HOME/kong-mesh-2.8.1/bin:$HOME/.kuma-dev/kuma-master/bin:$HOME/.local/bin:$PATH
252+
export PATH=$HOME/go/bin:$HOME/.cargo/bin:$HOME/kong-mesh-2.8.1/bin:$HOME/.kuma-dev/kuma-master/bin:$HOME/.local/bin:$PATH
167253
if type crcd >/dev/null 2>&1 ; then
168254
eval $(crc oc-env)
169255
fi
@@ -176,15 +262,3 @@ export GIT_PERSONAL_ORG=jijiechen
176262
if [[ -z "$ZSH" ]]; then
177263
export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
178264
fi
179-
180-
git config --global alias.co checkout
181-
git config --global alias.chekcout checkout
182-
git config --global alias.st status
183-
git config --global alias.br branch
184-
git config --global alias.cm 'commit -s'
185-
function br(){
186-
BR=$(git --no-pager branch | gum filter --placeholder 'select a branch')
187-
if [[ "${BR:0:2}" != '* ' ]]; then
188-
git checkout $(echo $BR | tr -d ' ')
189-
fi
190-
}

0 commit comments

Comments
 (0)
Please sign in to comment.