-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-branch-info
More file actions
executable file
·80 lines (72 loc) · 2.61 KB
/
Copy pathgit-branch-info
File metadata and controls
executable file
·80 lines (72 loc) · 2.61 KB
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
#!/usr/bin/bash
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
script_name="$(basename "${BASH_SOURCE[0]}")"
unmerged=
merged=
while getopts "um" o; do
case "$o" in
u) unmerged=1 ;;
m) merged=1 ;;
*) ;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ] ; then
cat 1>&2 <<- EOM
usage: $script_name [flags] <trunk> [<trunk1> ...]
flags:
-u only show branches where ahead > 0
description:
For each branch in the local repo, find the # of commits behind and ahead of <trunk>. It then
outputs a list of branches with at least one commit ahead of <trunk>, sorted by increasing
number of commits behind <trunk>.
If more than one trunk (positional argument) is given, branches are excluded from the output if
they have been merged into any of the trunks.
EOM
exit 1
fi
# dependencies
git_behind="${script_dir}"/git-behind
git_ahead="${script_dir}"/git-ahead
if [ ! -x "$git_behind" ]; then
echo "FATAL: git-behind not available in \$PATH" 1>&2
exit 1
fi
if [ ! -x "$git_ahead" ]; then
echo "FATAL: git-ahead not available in \$PATH" 1>&2
exit 1
fi
readarray -t branches <<< "$(git branch | awk '{ print $1 }' | sed '/origin\|*\|+/d')"
for b in "${branches[@]}" ; do
# echo ">branch: ${b}" 1>&2
min_behind=4294967295
min_ahead=4294967295
for trunk in "$@" ; do
behind="$("$git_behind" "${trunk}" "${b}")"
if [ "${min_behind}" -gt "${behind}" ] ; then
min_behind="${behind}"
fi
ahead="$("$git_ahead" "${trunk}" "${b}")"
if [ "${min_ahead}" -gt "${ahead}" ] ; then
min_ahead="${ahead}"
fi
done
if [ -z "$unmerged" ] && [ -z "$merged" ] ; then
author="$(git cat-file -p "$(git rev-parse "$b")" | sed -nE 's/author (.*?) <.*/\1/p')"
ahead_list+=("$(printf "%6d %6d %-25s %s\n" "$behind" "$ahead" "\"$author\"" "$b")")
elif [ -n "$merged" ] && [ "${min_ahead}" -eq 0 ] ; then
author="$(git cat-file -p "$(git rev-parse "$b")" | sed -nE 's/author (.*?) <.*/\1/p')"
ahead_list+=("$(printf "%6d %6d %-25s %s\n" "$behind" "$ahead" "\"$author\"" "$b")")
elif [ -n "$unmerged" ] && [ "${min_ahead}" -ne 0 ] ; then
author="$(git cat-file -p "$(git rev-parse "$b")" | sed -nE 's/author (.*?) <.*/\1/p')"
ahead_list+=("$(printf "%6d %6d %-25s %s\n" "$behind" "$ahead" "\"$author\"" "$b")")
fi
done
if [ "${#ahead_list[@]}" -gt 0 ]; then
IFS=$'\n'; ahead_list=("$(sort -n -k1 <<< "${ahead_list[*]}")"); IFS=
# readarray -t ahead_list <<< "$(sort -n -k1 <<< "${ahead_list[*]}")"
printf "%6s %6s %-25s %s\n" "behind" " ahead" "author " "branch"
for row in "${ahead_list[@]}"; do
echo "$row"
done
fi