-
Notifications
You must be signed in to change notification settings - Fork 9
/
git-is-branch-remote
executable file
·70 lines (60 loc) · 1.19 KB
/
git-is-branch-remote
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
#!/usr/bin/env bash
app_name="$(basename "${BASH_SOURCE[0]}")"
app_dir="$(dirname "${BASH_SOURCE[0]}")"
print_help() {
expand -t 4 << EOF
Usage:
$app_name <BRANCH...>
Returns 0 if the branch(es) refer to a remote branch.
Options:
-q, --quiet
Suppress output.
-?, --help
Show this help information and exit.
EOF
}
quiet=0
args=()
for ((i = 1; i <= $#; i++)); do
opt="${!i}"
case "$opt" in
-q|--quiet)
quiet=1
;;
-\?|--help)
print_help
exit 0
;;
--)
args+=("${@:i+1}")
break
;;
-*)
echo "$app_name: [Error] Unknown option: $1" >&2
exit 1
;;
*)
args+=("$opt")
;;
esac
done
if [[ ${#args[@]} -eq 0 ]]; then
echo "$app_name: [Error] No branch is given." >&2
echo "Try '$app_name -?' for more information." >&2
exit 2
fi
git rev-parse --git-dir > /dev/null &&
result=0
for arg in "${args[@]}"; do
full_name="$(git rev-parse --quiet --verify --symbolic-full-name "$arg")"
if [[ "$full_name" == refs/remotes/* ]]; then
[[ quiet -eq 0 ]] && echo "$arg: yes"
elif [[ "$full_name" == refs/heads/* ]]; then
[[ quiet -eq 0 ]] && echo "$arg: no"
result=1
else
echo "$app_name: [Warning] Not a branch: $arg" >&2
result=2
fi
done
exit "$result"