-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitcloc.sh
executable file
·49 lines (43 loc) · 1.63 KB
/
gitcloc.sh
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
#!/usr/bin/env bash
usage() {
echo "
usage: gitcloc branch [--help]
Clear/delete local branches that have been merged into the branch provided as first argument.
The branch must be provided as the first argument even when calling with optional arguments.
optional arguments:
--help Shows this help message.
"
}
set -e
if [ -z "$1" ] || [ "$#" -gt 1 ] || [[ "$@" == *"--help"* ]]; then
usage
else
# Create a file to contain list of all merged branches.
TMP_BRANCHES_FILE=$(mktemp)
trap '{ rm -f $TMP_BRANCHES_FILE; }' EXIT
# Store current branch and switch to target branch.
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" != "$1" ]; then
git checkout "$1"
fi
# Redirect merged branches to tmp file for interactive editing.
git branch --merged | sed 's/^ *//; s/ *$//; /^$/d; /^[*]/d' > "$TMP_BRANCHES_FILE"
# Check that there are merged branches to be delete before continuing.
LINE_COUNT=$(wc -l "$TMP_BRANCHES_FILE" | awk '{print $1}')
if [[ $LINE_COUNT -gt 0 ]]; then
$(git var GIT_EDITOR) "$TMP_BRANCHES_FILE"
# Trim trailing/leading space, and empty lines after edit.
TEMP=$(sed 's/^ *//; s/ *$//; /^$/d' "$TMP_BRANCHES_FILE")
echo "$TEMP" > "$TMP_BRANCHES_FILE"
# Soft delete all branches left in the file.
set +e
xargs git branch -d < "$TMP_BRANCHES_FILE"
else
echo "No branches found for clearing."
fi
# Switch back to original branch if it still exists.
set -e
if [ "$CURRENT_BRANCH" != "$1" ]; then
git checkout "$CURRENT_BRANCH" 2> /dev/null
fi
fi