Skip to content

Commit dd970ab

Browse files
committed
supporting scripts
1 parent 453623d commit dd970ab

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

export_nomerged_list

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
#!/bin/bash
4+
5+
# Synopsis
6+
# Tool to list all not merged remote branches of a Git repository which are not whitelisted
7+
# Output is a comma separated value file (VCSV) containing the branch name, commit and author
8+
# information. The CSV file is sorted and contains a header.
9+
10+
# Parameter
11+
# 1st parameter: The whitelist must contain 1 line per branch to be whitelisted.
12+
# 2nd parameter: Name of the file with the not merged branches.
13+
whitelist=$1
14+
nomergedlist=$2
15+
16+
rm -f "$nomergedlist"
17+
18+
echo "Branchname,Commit Date,Committer,Commiter Email,Author data,Author,Author Email" >> "$nomergedlist"
19+
20+
while read remote; do
21+
22+
found=false
23+
while read white; do
24+
if [[ "$remote" =~ "$white" ]]
25+
then
26+
found=true;
27+
fi
28+
done < "$whitelist"
29+
30+
if ! $found;
31+
then
32+
remote=${remote#origin/}
33+
if [ "$remote" != "master" ]; # ignore master and release
34+
then
35+
echo checkout $remote
36+
git checkout $remote
37+
echo $remote,$(git log --date=short --pretty="%cd,%cn,%ce,%ad,%an,%ae" -1) >> "$nomergedlist"
38+
fi
39+
else
40+
echo "Found '${remote#origin/}' in whitelist. Ignoring."
41+
fi
42+
43+
done < <(git branch -r --no-merged | command grep -vE "^(\*|\s*master\s*$)")
44+
45+
# Sort by dates
46+
tmpfile=$(mktemp)
47+
sort --field-separator=',' --key=2 --key=5 --key=1 "$nomergedlist" > "$tmpfile"
48+
mv "$tmpfile" "$nomergedlist"
49+
50+
# Clean up
51+
rm -rf "$tmpfile"

gitext

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/bin/bash
2+
3+
function source_dir {
4+
local SOURCE="${BASH_SOURCE[0]}"
5+
DIR="$( dirname "$SOURCE" )"
6+
while [ -h "$SOURCE" ]
7+
do
8+
SOURCE="$(readlink "$SOURCE")"
9+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
10+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
11+
done
12+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
13+
}
14+
15+
set -e
16+
set -o pipefail
17+
18+
source_dir
19+
20+
21+
COMMAND=$1
22+
23+
case "$COMMAND" in
24+
branch)
25+
git rev-parse --symbolic-full-name --abbrev-ref HEAD
26+
;;
27+
28+
revision)
29+
git rev-parse $(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
30+
;;
31+
32+
rev)
33+
git rev-parse $(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
34+
;;
35+
36+
checkout)
37+
if [ $(git branch | grep -cw $2) == 0 ]; then
38+
git checkout --track -b $2 remotes/origin/$2
39+
else
40+
git checkout $2
41+
fi
42+
;;
43+
44+
remoteexists)
45+
if [ $(git branch -r | grep -cw origin/$2) == 0 ]; then
46+
# echo remote branch $2 does not exists
47+
exit 2
48+
else
49+
# echo remote branch $2 exists
50+
exit 0
51+
fi
52+
;;
53+
54+
localexists)
55+
if [ $(git branch | grep -cw $2) == 0 ]; then
56+
# echo local branch $2 does not exists
57+
exit 2
58+
else
59+
# echo local branch $2 exists
60+
exit 0
61+
fi
62+
;;
63+
64+
remoteurl)
65+
echo "$(git config --get remote.origin.url)"
66+
;;
67+
68+
meta)
69+
SUB_COMMAND=$2
70+
FILES="${PWD}/*"
71+
for file in ${FILES}; do
72+
if [ -d $file ]; then
73+
case "$SUB_COMMAND" in
74+
list)
75+
cd $file
76+
echo $(gitext remoteurl)
77+
cd ..
78+
;;
79+
80+
listbranch)
81+
cd $file
82+
echo -n "$(${DIR}/gitext remoteurl) "
83+
${DIR}/gitext branch
84+
cd ..
85+
;;
86+
87+
branchOf)
88+
cd $file
89+
branch=$(${DIR}/gitext branch)
90+
if [[ $branch == $3 ]]; then
91+
echo "$(${DIR}/gitext remoteurl)"
92+
fi
93+
cd ..
94+
;;
95+
96+
hasBranch)
97+
if [ -z $3 ]; then
98+
exit 0
99+
fi
100+
cd $file
101+
if [ $(git branch | grep -cw $3) != 0 ]; then
102+
gitext remoteurl
103+
else
104+
if [ $(git branch -r | grep -cw origin/$3) != 0 ]; then
105+
gitext remoteurl
106+
fi
107+
fi
108+
cd ..
109+
;;
110+
111+
checkout)
112+
cd $file
113+
gitext checkout $3
114+
cd ..
115+
;;
116+
117+
reset)
118+
cd $file
119+
echo -n "$(${DIR}/gitext remoteurl) "
120+
git reset $3 $4
121+
cd ..
122+
;;
123+
124+
*)
125+
echo "Unknown meta command: $SUB_COMMAND"
126+
echo "Use {list|listbranch|branchOf BRANCH|hasBranch BRANCH|checkout BRANCH|reset TYPE BRANCH}"
127+
exit 1
128+
esac
129+
fi
130+
done
131+
;;
132+
133+
*)
134+
echo "Usage: $0 {branch|rev|checkout BRANCH|remoteexists BRANCH|localexists BRANCH|meta SUB-COMMAND{list|listbranch|checkoutIf BRANCH}}"
135+
exit 1
136+
137+
esac

remove_remote_branches

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
# Synopsis
4+
# Tool to delete all merged remote branches of a Git repository which are not whitelisted
5+
6+
# Parameter
7+
# 1st parameter: Path of the whitelist file
8+
# The whitelist must contain 1 line per branch to be whitelisted.
9+
whitelist=$1
10+
11+
while read remote; do
12+
13+
found=false
14+
while read white; do
15+
if [[ "$remote" =~ "$white" ]]
16+
then
17+
found=true;
18+
fi
19+
done < "$whitelist"
20+
21+
if ! $found;
22+
then
23+
remote=${remote#origin/}
24+
if [ "$remote" != "master" ]; # protect master and release
25+
then
26+
git push origin --delete "$remote"
27+
fi
28+
fi
29+
done < <(git branch -r --merged | command grep -vE "^(\*|\s*master\s*$)")

whitelist

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
master
2+
release

0 commit comments

Comments
 (0)