-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-bunch.bash
172 lines (131 loc) · 2.56 KB
/
git-bunch.bash
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# =========================================================================
ME="$( basename "$0" )"
GITCONFIG=~/.gitconfig
# =========================================================================
print_usage() {
cat - <<USAGE
<usage>
USAGE
exit
}
print_help() {
cat - <<HELP
<help>
HELP
exit
}
# =========================================================================
main() {
case "$1" in
'' ) print_usage ;;
-h | --help ) print_help ;;
-l | --list ) bunch_list ;;
* ) bunch_exec "$@" ;;
esac
}
# =========================================================================
bunch_list() {
sed -n 's/^\[bunch "\([^"]*\)"\].*/\1/p' "$GITCONFIG"
}
bunch_configure() {
case "$1" in
-l | --list )
sed -n "/./ s/^/bunch.$BUNCH_NAME./p" <<<"$BUNCH_RAW"
;;
--unset )
git config --global --unset "bunch.$BUNCH_NAME.$2"
;;
path | stoponerror )
git config --global "bunch.$BUNCH_NAME.$1" "$2"
;;
* )
print_usage
;;
esac
}
bunch_raw_config() {
sed -n '
/^\[bunch "'"$1"'"\]/,/^\[/ {
/^\[/d;
s/^\s*//;
s/\s*=\s*/=/;
s/\s*$//;
p;
}
' "$GITCONFIG"
}
bunch_exec() {
BUNCH_NAME="$1"
BUNCH_ERREXIT="+o"
unset BUNCH_PATH BUNCH_STOP
# Read raw bunch config
BUNCH_RAW="$( bunch_raw_config "$BUNCH_NAME" )"
# Parse the bunch
while IFS="=" read -r k v
do
case "$k" in
path )
BUNCH_PATH="$v"
;;
stoponerror )
BUNCH_STOP="$v"
case "$v" in
0 | no | off | false | '' ) BUNCH_ERREXIT="+o" ;;
1 | yes | on | true ) BUNCH_ERREXIT="-o" ;;
esac
;;
esac
done <<<"$BUNCH_RAW"
# Print the bunch path
# git-bunch NAME
shift
[ $# -gt 0 ] || {
echo "$BUNCH_PATH"
return
}
# Configure the bunch
# git-bunch NAME config ...
if [ "$1" = "config" ]
then
shift
bunch_configure "$@"
return
fi
[ -d "$BUNCH_PATH" ] || die "Not bunch directory: $BUNCH_PATH"
# List the bunch
# git-bunch NAME ls
if [ "$1" = "ls" ]
then
ls "$BUNCH_PATH" --color=tty
return
fi
# Work through the bunch and execute the git command
# git-bunch NAME ...
[ "$1" = "--" ] && shift
set "$BUNCH_ERREXIT" errexit
for f in "$BUNCH_PATH"/*
do
[ -d "$f" ] || continue
(
set "$BUNCH_ERREXIT" errexit
builtin cd "$f"
git "$@"
)
done
}
# =========================================================================
catecho() {
[ $# -eq 0 ] && cat - || echo "$@"
}
warn() {
catecho "$@" >&2
}
die() {
warn "$@"
exit 1
}
# =========================================================================
main "$@"
# =========================================================================
# EOF