-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-branches
executable file
·129 lines (113 loc) · 2.12 KB
/
git-branches
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# git branches
#
# Lists all branches together with the author of each branch.
# Separates remote and local branches by blank line.
#
# for script debugging
# set -x
# settings
debug=false
authorNamesWidth=25
sort=sortByBranch
# vars
app="$(basename $0)"
read -d '' help <<EOF
usage: $app [-abdh] [-w width]
-a sort by author
-b sort by branch name
-d turn on debugging
-h print this help and exit
-w change author width
EOF
# command line processing
args=`getopt abdho:w: $*`
if [ $? != 0 ]; then
echo "$help"
exit 2
fi
set -- $args
for i; do
case "$i" in
-a)
# sort by author
sort=sortByAuthor
shift;;
-b)
# sort by branch name
sort=sortByBranch
shift;;
-d)
# turn on debugging
debug=true
shift;;
-h)
# print help
echo "$help"
exit 0
shift;;
-o)
# change author offset
authorNamesOffset="$2"
shift; shift;;
-o)
# change author width
authorNamesWidth="$2"
shift; shift;;
--)
shift
break;;
esac
done
sortByBranch() {
cat
}
sortByAuthor() {
if $debug; then
echo "## sorting by author"
echo sort -k 4
fi >&2
sort -k 4 \
| colourByAuthor
}
colourByAuthor() {
perl -pe '
BEGIN {
@colours=(7, 41, 42, 43, 45, 46, 47, 31, 33, 34, 35);
$colourIndex=-1;
$author="";
$esc="\033";
$off="${esc}[m";
$width='${authorNamesWidth}'-2;
}
if(/by (\w[. \w]{2,$width}\w)/ && $1 ne $author ) {
$colourIndex++;
$author = $1;
$colourIndex = 0 if $colourIndex >= scalar(@colours);
}
$colour= "${esc}[" . $colours[$colourIndex];
s/(.*)/${esc}[${colour}m${1}${off}/;
'
}
localBranches() {
$debug && echo "## Local branches:" >&2
$debug && git branch | sed 's/^..//' >&2
git branch | sed -e 's/^..//' -e 's/.* rebasing \(.*\))/\1/'
}
remoteBranches() {
$debug && echo "## Local branches:" >&2
$debug && git branch -r | grep -v HEAD >&2
git branch -r | egrep -v HEAD
}
displayBranch() {
git show --format="%ai by %<(${authorNamesWidth})%an" "$1" \
| head -1 \
| expand -t 8 2>/dev/null
}
for branch in $(remoteBranches) "" $(localBranches); do
if [[ $branch == "" ]]; then
echo ""
else
echo "$(displayBranch $branch) ${branch}"
fi
done | $sort