-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.git_functions
164 lines (137 loc) · 4.5 KB
/
.git_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
#!/usr/bin/env zsh
alias lg='git log --graph --pretty=format:'"'"'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"'"' --abbrev-commit'
git_main_branch () {
git branch | cut -c 3- | grep -E '^master$|^main$'
}
function gri () {
main_branch=$(git_main_branch)
echo "main branch is $main_branch"
merge_base=$(git merge-base $main_branch HEAD)
echo "merge base is $merge_base"
git rebase -i $merge_base
}
alias gpob='command git push origin HEAD'
alias gcob='command git checkout -b'
alias gcom='command git checkout $(git_main_branch)'
alias gp='command git pull'
alias ga='command git add'
alias gap='command git add --patch'
git () {
if [ "$1" = "push" ] && [ "$2" = "origin" ] && [ "$3" = "HEAD" ]; then
echo "Error: Please use the gpob alias for the git push origin HEAD command."
elif [ "$1" = "checkout" ] && [ "$2" = "-b" ]; then
echo "Error: Please use the gcob alias for the git checkout -b command."
elif [ "$1" = "checkout" ] && ([ "$2" = "master" ] || [ "$2" = "main" ]); then
echo "Error: Please use the gcom alias for the git checkout main/master command."
elif [ "$1" = "pull" ]; then
echo "Error: Please use the gp alias for the git pull command."
elif [ "$1" = "cherry-pick" ]; then
echo "Error: Please use the git cp alias for the git cherry-pick command."
elif [ "$1" = "add" ]; then
echo "Error: Please use the ga alias for the git add command."
else
command git "$@"
fi
}
function git_remove_missing_files() {
git ls-files -d -z | xargs -0 git update-index --remove
}
function git_info() {
if [ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]; then
# print informations
echo "git repo overview"
echo "-----------------"
echo
# print all remotes and thier details
for remote in $(git remote show); do
echo $remote:
git remote show $remote
echo
done
# print status of working repo
echo "status:"
if [ -n "$(git status -s 2> /dev/null)" ]; then
git status -s
else
echo "working directory is clean"
fi
# print at least 5 last log entries
echo
echo "log:"
git log -5 --oneline
echo
else
echo "you're currently not in a git repository"
fi
}
function git_stats {
if [ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]; then
echo "Number of commits per author:"
git --no-pager shortlog -sn --all
AUTHORS=$( git shortlog -sn --all | cut -f2 | cut -f1 -d' ')
LOGOPTS=""
if [ "$1" == '-w' ]; then
LOGOPTS="$LOGOPTS -w"
shift
fi
if [ "$1" == '-M' ]; then
LOGOPTS="$LOGOPTS -M"
shift
fi
if [ "$1" == '-C' ]; then
LOGOPTS="$LOGOPTS -C --find-copies-harder"
shift
fi
for a in $AUTHORS
do
echo '-------------------'
echo "Statistics for: $a"
echo -n "Number of files changed: "
git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f3 | sort -iu | wc -l
echo -n "Number of lines added: "
git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f1 | awk '{s+=$1} END {print s}'
echo -n "Number of lines deleted: "
git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f2 | awk '{s+=$1} END {print s}'
echo -n "Number of merges: "
git log $LOGOPTS --all --merges --author=$a | grep -c '^commit'
done
else
echo "you're currently not in a git repository"
fi
}
function git_edit_all_modified() {
$EDITOR $(git status --short | awk '$1 ~ /^M$/ {print $2}')
}
function cleanup-branches() {
file="/tmp/git-cleanup-branches-$(uuidgen)"
function removeCurrentBranch {
sed -E '/\*/d'
}
function leftTrim {
sed -E 's/\*?[[:space:]]+//'
}
all_branches=$(git branch | removeCurrentBranch | leftTrim)
# write branches to file
for branch in $all_branches; do
echo "keep $branch" >> $file
done
# write instructions to file
echo "
# All of your branches are listed above
# (except for the current branch, which you can't delete)
# change keep to d to delete the branch
# all other lines are ignored" >> $file
# prompt user to edit file
$EDITOR "$file"
# check each line of the file
cat $file | while read -r line; do
# if the line starts with "d "
if echo $line | grep --extended-regexp "^d " > /dev/null; then
# delete the branch
branch=$(echo $line | sed -E 's/^d //')
git branch -D $branch
fi
done
# clean up
rm $file
}