Skip to content

Commit f6c7482

Browse files
Create Git_Functions.sh
1 parent 8777fae commit f6c7482

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Git_Functions.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function delete_gone_branches() {
2+
if [[ "$#" -ne 1 ]]; then
3+
echo "Exactly one argument needed: path to the directory."
4+
return 1
5+
fi
6+
dir=${1}
7+
make_heading ${dir}
8+
if [[ ! -d ${dir} ]]; then
9+
echo "${dir} is not a valid path."
10+
return 1
11+
fi
12+
if [[ ${dir} != '.' ]]; then
13+
pushd ${dir}
14+
fi
15+
if [[ -d .git ]]; then
16+
git fetch -p
17+
git branch -v | grep gone | awk -F' ' '{print $1}' | xargs -I{} -P 4 -- git branch -D {}
18+
else
19+
echo "It is not a git repository."
20+
fi
21+
if [[ ${dir} != '.' ]]; then
22+
popd
23+
fi
24+
}
25+
function removeBranchesFromRemote() {
26+
padding=" "
27+
date=$(gum input --value $(date '+%Y-%m-%d') --placeholder "Older than?")
28+
if [[ -z "$date" ]]; then
29+
echo "Date cannot be empty."
30+
return
31+
fi
32+
old_branches=""
33+
function populateOldBranches() {
34+
for remote_branch in $(git branch -r | grep -v "HEAD\|develop\|master\|main\|release*" | sed /\*/d); do
35+
if [[ -z "$(git log -1 --since=\"${date}\" -s ${remote_branch})" ]]; then
36+
author=$(git show --format="%an" ${remote_branch} | head -n 1)
37+
age=$(git show --format="%cr" ${remote_branch} | head -n 1)
38+
branch=$(echo ${remote_branch} | sed 's#origin/##')
39+
old_branches+='\n'$(printf "%s|%s|%s\n" \
40+
"${author}${padding:${#author}}" \
41+
"${age}${padding:${#age}}" \
42+
"${branch}")
43+
fi
44+
done
45+
}
46+
populateOldBranches
47+
if [[ -n $old_branches ]]; then
48+
branchesToBeDeleted=$(echo $old_branches | gum choose --no-limit | awk -F'|' '{print $3}')
49+
if [[ -n $branchesToBeDeleted ]]; then
50+
echo $branchesToBeDeleted
51+
gum confirm "Delete aforementioned branches from remote?" &&
52+
echo $branchesToBeDeleted | xargs -P 8 -I{} -- git push origin --delete {}
53+
fi
54+
else
55+
echo "No branch older than ${date} found."
56+
fi
57+
}

0 commit comments

Comments
 (0)