-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitTrim
executable file
·75 lines (68 loc) · 1.95 KB
/
gitTrim
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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
usage(){
echo "usage : $(basename "$0") [ -d or -D ] [ install-alias ]"
doc=(
"lists branches that were deleted from remote"
"" "List of flags :"
"-h | --help displays help"
"-d deletes branches that were deleted from remote (with no unmerged commits)"
"-D deletes branches that were deleted from remote (even with unmerged commits, when squashing for instance)"
""
"install-alias installs gitTrim as a git global alias"
)
printf "%s\n" "${doc[@]}"
}
flag=''
while (( $# ))
do
case "$1" in
"-h"|"--help")
usage
exit
;;
-d)
flag=d
;;
-D)
flag=D
;;
install-alias)
echo "# Installing globally alias trim as !gitTrim"
echo "# Make sure gitTrim is accessible as an executable"
echo "# For instance, you can put it in /usr/local/bin"
echo
echo "# Executing command :"
echo "git config --global alias.trim '!gitTrim'"
git config --global alias.trim '!gitTrim'
exit 0
;;
esac
shift
done
echo "Running git fetch -p"
git fetch -p
deletedBranches=''
while read -r branch
do
if [ "$branch" = "*" ];
then
echo "Warning: Your current local branch is deleted on remote." >&2
continue
fi
deletedBranches="$deletedBranches, $branch"
if [ -n "$flag" ]; then
git branch -$flag "$branch" > /dev/null
fi
done < <(git for-each-ref --format '%(upstream:track) %(refname)' refs/heads | awk ' $1 == "[gone]" { sub("refs/heads/", "", $2) ; print $2 }')
if [ -n "$deletedBranches" ]; then
deletedBranches=${deletedBranches/, }
if [ -z "$flag" ]; then
echo "Branches to be trimmed : $deletedBranches"
else
echo "Trimed branches : $deletedBranches"
fi
else
echo "No branches to trim"
fi