-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrappers.sh
executable file
·95 lines (89 loc) · 3.76 KB
/
wrappers.sh
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
#!/bin/sh source-this-script
[ "${BASH_VERSION:-}" -o "${KSH_VERSION:-}" ] || return
: ${GIT_DEFAULT_COMMAND=str}
: ${HUB_DEFAULT_COMMAND=$GIT_DEFAULT_COMMAND}
# Git supports aliases defined in .gitconfig, but you cannot override Git
# builtins (e.g. "git log") by putting an executable "git-log" somewhere in the
# PATH. Also, git aliases are case-insensitive, but case can be useful to create
# a negated command (gf = grep --files-with-matches; gF = grep
# --files-without-match). As a workaround, translate "X" to "-x".
# Add support for the "hub" extension. As this messes with the completion for
# git, anyway, follow their advice and alias git=hub (adapted to my wrapper).
git()
{
typeset -a gitConfigArgs=()
while [ $# -ne 0 ]
do
case "$1" in
-c) gitConfigArgs+=("$1" "$2"); shift; shift;;
*) break;;
esac
done
# If there's no alias we can simply pass gitConfigArgs directly after the "git"
# command. Aliases however need to do this on their own (if their Git command(s)
# need to react to config overrides). We can just pass the arguments along here.
GIT_CONFIG_ARGS=; [ ${#gitConfigArgs[@]} -gt 0 ] && printf -v GIT_CONFIG_ARGS '%q ' "${gitConfigArgs[@]}"; export GIT_CONFIG_ARGS
typeset gitSubAlias="git-$1-$2"
typeset gitAlias="git-$1"
typeset gitCommand="$(which hub 2>/dev/null || which git)"
if [ $# -eq 0 -a -n "$GIT_DEFAULT_COMMAND" ]; then
eval "git \"\${gitConfigArgs[@]}\" $GIT_DEFAULT_COMMAND"
elif type ${BASH_VERSION:+-t} "$gitSubAlias" >/dev/null 2>&1; then
shift; shift
eval $gitSubAlias '"$@"' # Need eval for shell aliases.
elif type ${BASH_VERSION:+-t} "$gitAlias" >/dev/null 2>&1; then
shift
eval $gitAlias '"$@"' # Need eval for shell aliases.
else
case "$1" in
[!-]*[A-Z]*)
# Translate "X" to "-x" to enable aliases with uppercase letters.
typeset translatedAlias="$(echo "$1" | sed -e 's/[[:upper:]]/-\l\0/g')"
shift
"$gitCommand" "${gitConfigArgs[@]}" "$translatedAlias" "$@"
;;
*)
"$gitCommand" "${gitConfigArgs[@]}" "$@";;
esac
fi
}
which hub >/dev/null 2>&1 || return
hub()
{
typeset -a gitConfigArgs=()
while [ $# -ne 0 ]
do
case "$1" in
-c) gitConfigArgs+=("$1" "$2"); shift; shift;;
*) break;;
esac
done
# If there's no alias we can simply pass gitConfigArgs directly after the "git"
# command. Aliases however need to do this on their own (if their Git command(s)
# need to react to config overrides). We can just pass the arguments along here.
GIT_CONFIG_ARGS=; [ ${#gitConfigArgs[@]} -gt 0 ] && printf -v GIT_CONFIG_ARGS '%q ' "${gitConfigArgs[@]}"; export GIT_CONFIG_ARGS
typeset hubSubAlias="hub-$1-$2"
typeset hubAlias="hub-$1"
typeset gitSubAlias="git-$1-$2"
typeset gitAlias="git-$1"
if [ $# -eq 0 ]; then
HUB=t eval "hub \"\${gitConfigArgs[@]}\" $HUB_DEFAULT_COMMAND"
elif type ${BASH_VERSION:+-t} "$hubSubAlias" >/dev/null 2>&1; then
shift; shift
HUB=t eval $hubSubAlias '"$@"' # Need eval for shell aliases.
elif type ${BASH_VERSION:+-t} "$hubAlias" >/dev/null 2>&1; then
shift
HUB=t eval $hubAlias '"$@"' # Need eval for shell aliases.
elif contains "$1" am apply checkout cherry-pick clone fetch init merge push remote submodule alias api browse ci-status compare create delete fork gist issue pr pull-request release sync; then
# Built-in hub commands need to have precedence over git-alias with the same name (e.g. "hub browse" over git-browse).
HUB=t command hub "${gitConfigArgs[@]}" "$@"
elif type ${BASH_VERSION:+-t} "$gitSubAlias" >/dev/null 2>&1; then
shift; shift
HUB=t $gitSubAlias "$@"
elif type ${BASH_VERSION:+-t} "$gitAlias" >/dev/null 2>&1; then
shift
HUB=t eval $gitAlias '"$@"' # Need eval for shell aliases.
else
HUB=t command hub "${gitConfigArgs[@]}" "$@"
fi
}