-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-last-branch
executable file
·69 lines (54 loc) · 1.2 KB
/
git-last-branch
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
#!/usr/bin/env bash
set -eo pipefail
error() {
echo "$@" >&2
exit 1
}
debug() {
if [[ -n $DEBUG ]]; then
echo "$@" >&2
fi
}
usage() {
echo "Usage: git last-branch [OPTIONS] [TARGET_REF]"
echo "OPTIONS:"
echo " -d output extra debug info"
echo " -h display this help message and exit"
echo
echo " TARGET_REF branch to start from (default: current branch)"
}
parent() {
local ref=$1
local parent=$(git describe --all --abbrev=0 "$ref"^ | sed -nE 's+^(heads|remotes|tags)/++p')
echo "$parent"
}
while getopts dh opt; do
case $opt in
d)
DEBUG=y
shift
;;
h)
usage
exit 0
;;
esac
done
target=${1:-HEAD}
debug -n Targeting $target
rev=$(git rev-parse "$target")
debug " (revision ${rev::7})"
branch=$(git describe --exact-match --all "$rev" | sed -n 's|^heads/||p')
if [[ -n $branch ]]; then
debug Target branch is $branch
fi
parent=$(parent "$rev")
if [[ $parent == $origin/$branch && master != $parent ]]; then
debug Skipping over remote branch
parent=$(parent "$origin/$branch")
fi
if [[ -n $parent ]]; then
echo "$parent"
else
error "Couldn't determine parent ref for $target ($rev)"
fi