-
Notifications
You must be signed in to change notification settings - Fork 5
/
git-recent-branches
executable file
·79 lines (68 loc) · 2.23 KB
/
git-recent-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
#!/bin/bash
# Copyright 2013 Mark Lodato <lodato@google.com>
# Released under the MIT license; see LICENSE for details.
OPTIONS_SPEC="\
git recent-branches
Show branches and how old each is, sorted by commit date.
Default values for the below flags can be set in .gitconfig with
'recent-branch.since' or 'recent-branch.ignore'.
--
a,all show all branches, overriding --since and --ignore
since= ignore branches older than this date
ignore= ignore branches whose names match this GNU regexp
"
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH=
. "$(git --exec-path)/git-sh-setup"
since=$(git config --get recent-branches.since)
ignore_re=$(git config --get recent-branches.ignore)
while test $# != 0
do
case "$1" in
-a|--all) since= ignore_re= ;;
--since) since="$2" ; shift ;;
--ignore) ignore_re="$2" ; shift ;;
--) shift ; break ;;
*) break ;;
esac
shift
done
if [[ $# -ne 0 ]]; then
echo "error: no positional arguments are allowed" >&2
usage
fi
parse_date() {
git rev-parse --since="$*" | sed -e 's/^--max-age=//'
}
if [[ -z $since ]]; then
min_timestamp=0
else
min_timestamp=$(parse_date "$since")
fi
current_branch=$(git symbolic-ref --short -q HEAD)
# Make sure that the date format is not completely invalid. `git rev-parse
# --since=...` ignores pieces not understand, replacing those pieces with those
# corresponding to now. This won't catch things like "yesterday non" (instead
# of "noon"), but it's better than nothing.
if [[ min_timestamp -eq $(parse_date now) ]]; then
echo "error: invalid timestamp for --since: $since"
exit 2
fi
git for-each-ref refs/heads --sort=committerdate \
--format='%(refname:short)|%(committerdate:raw)|%(committerdate:relative)' \
| while IFS='|' read branch timestamp relative_date
do
# $timestamp is of the form "12345 -0500"; we only want the first part.
if [[ $branch != $current_branch &&
( ${timestamp% *} -lt $min_timestamp ||
( -n $ignore_re && $branch =~ $ignore_re ) ) ]]; then
continue
fi
start=' '
end=''
if [[ $branch = $current_branch ]]; then
start='* \e[32m'
end='\e[0m'
fi
printf "$start%-30s %s$end\\n" "$branch" "$relative_date"
done