aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/git-branch-cleanup
blob: 5e867148595237c05c103f048cd761f16ddb76d8 (plain)
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
#! /usr/bin/env sh

main()
{
	# Handle opts
	opts "$@"
	shift "$OPTS"
	unset OPTS

	# Show help
	[ "$OPT_HELP_ONLY" ] && usage && exit 0

	# Get a list of branches
	buffer=$(mktemp)

	git branch --merged \
		| awk '{ print $NF }' \
		| grep -Ev "^master$" \
		> "$buffer"

	count=$(wc -l < "$buffer")

	if [ $count -lt 1 ]
	then
		printf "No branches to remove\n"
		exit 0
	fi

	printf "Going to remove the following %s branches:\n" "$count"
	cat -- "$buffer"

	printf "\n^C to cancel...\n"
	read

	while read -r branch
	do
		git branch -D -- "$branch"
	done < "$buffer"
}

opts()
{
	OPTS=0

	while getopts ":h" opt
	do
		case "$opt" in
			h) OPT_HELP_ONLY=1 ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	unset opt
}

usage()
{
	cat <<EOF
Usage:
	$(basename "$0") -h

Clean up merged branches.

Options:
	-h  Show this help text and exit.
EOF
}

main "$@"