-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequencer-status
executable file
·320 lines (283 loc) · 7.1 KB
/
sequencer-status
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
export GIT_DIR=$(git rev-parse --git-dir)
export SEQUENCER_VERSION=1.3
USE_COLOR=0
if [ -t 1 ]; then
USE_COLOR=1
fi
usage()
{
local errCode=$1
echo -e "Usage: $(basename $0) [OPTS]"
echo -e "\t--[no-]color Force color to be enabled/disabled"
echo -e "\t-v,--version Diplay version information"
echo -e "\t-h,--help Diplay usage"
exit $errCode
}
while [ $# -gt 0 ]; do
ARG=$1
shift
case "$ARG" in
--color)
USE_COLOR=1
;;
--no-color)
USE_COLOR=0
;;
-v|--version)
echo "git-sequencer-status $SEQUENCER_VERSION"
exit 0
;;
-h|--help)
usage 0
;;
*)
echo "ERROR: Invalid option $ARG" >&2
usage 1
;;
esac
done
if [ $USE_COLOR -eq 1 ]; then
export YELLOW_NODIM='\033[00;33m'
export RED_NODIM='\033[00;31m'
export RED_DIM='\033[02;31m'
export CYAN_BOLD='\033[01;36m'
export WHITE='\033[00m'
export COLOR_OPT="--color"
else
export COLOR_OPT="--no-color"
fi
__git_log()
{
git --no-pager log $COLOR_OPT "$@"
}
___colorize()
{
local C0=$1
local C1=$2
local C2=$3
awk -vC0=$C0 -vC1=$C1 -vC2=$C2 \
'
{
if ($1 == "exec") {
printf( C0 "%-7s " C2, $1);
for (i = 2; i < NF; i++) {
printf("%s ", $i);
};
printf("%s\n", $NF);
} else {
printf( C0 "%-7s " C1 $2 " " C2, $1);
for (i = 3; i < NF; i++) {
printf("%s ", $i);
};
printf("%s\n", $NF);
}
}'
}
__colorize_todo()
{
___colorize $YELLOW_NODIM $RED_NODIM $WHITE
}
__git_log_todo()
{
__git_log --format='%C(auto,yellow nodim)pick %C(auto,red nodim)%h %C(auto,white)%s' $*
}
__colorize_current()
{
___colorize $CYAN_BOLD $RED_NODIM $WHITE
}
__git_log_current_unmerged()
{
__git_log --format='%C(auto,cyan bold)*pick %C(auto,red nodim)%h %C(auto,white)%s' $*
}
__git_log_current()
{
__git_log --format='%C(auto,cyan bold)pick %C(auto,red nodim)%h %C(auto,white)%s' $*
}
__colorize_done()
{
___colorize $RED_DIM $RED_NODIM $WHITE
}
__git_log_done()
{
__git_log --format='%C(auto,red dim)done %C(auto,red nodim)%h %C(auto,white)%s' $*
}
__git_log_onto()
{
__git_log -n 1 --format='%C(auto,red dim)onto %C(auto,red nodim)%h %C(auto,white)%s' $*
}
__has_unmerged()
{
git ls-files --unmerged | wc -l
}
__sha1_to_refname()
{
local ONTO=$1
if [ $# -gt 1 ]; then
git name-rev --name-only --no-undefined ${ONTO} --refs $2 2> /dev/null
if [ $? -eq 0 ]; then
return
fi
fi
git name-rev --name-only --no-undefined ${ONTO} --refs refs/heads/*
}
__sanitize()
{
while read line; do
ACTION=$(echo "$line" | awk '{ print $1}')
if [ "$ACTION" != "exec" ]; then
SHA1=$(echo "$line" | awk '{ print $2}')
SHORT_SHA1=$(git log -n 1 ${SHA1} --format='%h')
SUBJECT=$(echo "$line" | awk '{ for (i = 3; i <= NF; i++) { printf("%s ", $i);}; printf("\n");}')
echo "${ACTION} ${SHORT_SHA1} ${SUBJECT}"
else
echo $line
fi
done
}
#
# Dump status for cherry-pick and revert series
#
__simple_sequence()
{
local BASE=$(cat ${GIT_DIR}/sequencer/head)
local BASE_NICE=$(__sha1_to_refname ${BASE})
local N_LINES=$(wc -l ${GIT_DIR}/sequencer/todo | awk '{ print $1}')
echo "# " $*
tac ${GIT_DIR}/sequencer/todo | head -n $(( $N_LINES - 1 )) | __sanitize | __colorize_todo
if [ $(__has_unmerged) != "0" ]; then
(echo -n '*'; head -n 1 ${GIT_DIR}/sequencer/todo) | __sanitize | __colorize_current
else
head -n 1 ${GIT_DIR}/sequencer/todo | __sanitize | __colorize_current
fi
__git_log_done HEAD ^${BASE}
__git_log_onto ${BASE}
}
#
# Status for a single cherry-pick or revert
#
__simple_action()
{
local HEADFILE=$1
local ACTION=$2
local BASE=$(git rev-parse HEAD)
local COMMIT=$(cat ${GIT_DIR}/$HEADFILE)
shift 2
echo "# " $* " a single commit"
echo "*$ACTION $(__git_log --format='%h %s' -n 1 $COMMIT)" | __colorize_current
__git_log_onto ${BASE}
}
__rebase_merge_sequence()
{
local BASE=$(cat ${GIT_DIR}/rebase-merge/head-name)
local ONTO=$(cat ${GIT_DIR}/rebase-merge/onto)
local BASE_NICE=$(echo ${BASE} | sed -e 'sL^refs/heads/LL')
local ONTO_NICE=$(__sha1_to_refname ${ONTO} ${BASE})
local CURRENT=$(tail -n 1 ${GIT_DIR}/rebase-merge/done)
echo "# Rebase: ${BASE_NICE} onto ${ONTO_NICE}"
tac ${GIT_DIR}/rebase-merge/git-rebase-todo | egrep -v '^#' | \
egrep -v '^$' | __sanitize |__colorize_todo
if [ $(__has_unmerged) != "0" ]; then
echo "*$CURRENT" | __sanitize | __colorize_current
else
echo "$CURRENT" | __sanitize | __colorize_current
fi
tac ${GIT_DIR}/rebase-merge/done | tail -n +2 | __sanitize | __colorize_done
__git_log_onto ${ONTO}
}
__rebase_apply_sequence()
{
local BASE=$(cat ${GIT_DIR}/rebase-apply/head-name)
local ONTO=$(cat ${GIT_DIR}/rebase-apply/onto)
local BASE_NICE=$(echo ${BASE} | sed -e 'sL^refs/heads/LL')
local ONTO_NICE=$(__sha1_to_refname ${ONTO} ${BASE})
local UNAPPLIED=1
local NEXT=$(cat ${GIT_DIR}/rebase-apply/next)
echo "# Rebase: ${BASE_NICE} onto ${ONTO_NICE}"
for FILE in $(ls -r ${GIT_DIR}/rebase-apply/[0-9]*); do
local NUM=$(basename $FILE | sed -e 's/^0*//')
local SHA1=$(head -n 1 $FILE | awk '{ print $2 }')
if [ "$NUM" == "$NEXT" ]; then
UNAPPLIED=0
if [ $(__has_unmerged) != "0" ]; then
__git_log_current_unmerged -n 1 ${SHA1}
else
__git_log_current -n 1 ${SHA1}
fi
elif [ $UNAPPLIED -eq 0 ]; then
__git_log_done -n 1 ${SHA1}
else
__git_log_todo -n 1 ${SHA1}
fi
done
__git_log_onto $(cat ${GIT_DIR}/rebase-apply/abort-safety)
}
__am_apply_sequence()
{
local BASE=$(cat ${GIT_DIR}/rebase-apply/abort-safety)
local UNAPPLIED=1
local NEXT=$(cat ${GIT_DIR}/rebase-apply/next)
echo "# Applying patches"
for FILE in $(ls -r ${GIT_DIR}/rebase-apply/[0-9]*); do
local PATCH_ID=$(basename $FILE)
local NUM=$(echo $PATCH_ID | sed -e 's/^0*//')
local SUBJECT=$(egrep '^Subject:' $FILE | head -n 1 |\
sed -e 's/Subject:[[:space:]]*\(\[[^]]*\][[:space:]]*\)*//')
if [ "$NUM" == "$NEXT" ]; then
UNAPPLIED=0
if [ $(__has_unmerged) != "0" ]; then
echo "pick ${PATCH_ID} ${SUBJECT}" | __colorize_current
else
echo "done ${PATCH_ID} ${SUBJECT}" | __colorize_current
fi
elif [ $UNAPPLIED -eq 0 ]; then
echo "done ${PATCH_ID} ${SUBJECT}" | __colorize_done
else
echo "pick ${PATCH_ID} ${SUBJECT}" | __colorize_todo
fi
done
__git_log_onto ${BASE}
}
if [ "$LESS" == "" ]; then
export LESS="FRX"
fi
if [ "$LV" == "" ]; then
export LV="-c"
fi
if [ "$GIT_PAGER" != "" ]; then
PAGER=$GIT_PAGER
else
GIT_CONFIG_PAGER=$(git config core.pager)
if [ $? -eq 0 ]; then
PAGER=$GIT_CONFIG_PAGER
else
if [ "$PAGER" == "" ]; then
PAGER="less"
else
# Keep pager at its current value
true
fi
fi
fi
(
if [ -d ${GIT_DIR}/rebase-merge ]; then
__rebase_merge_sequence
elif [ -f ${GIT_DIR}/CHERRY_PICK_HEAD ]; then
if [ -d ${GIT_DIR}/sequencer ]; then
__simple_sequence "Cherry picking"
else
__simple_action CHERRY_PICK_HEAD pick "Cherry picking"
fi
elif [ -d ${GIT_DIR}/rebase-apply ]; then
if [ -f ${GIT_DIR}/rebase-apply/applying ]; then
__am_apply_sequence
else
__rebase_apply_sequence
fi
elif [ -f ${GIT_DIR}/REVERT_HEAD ]; then
if [ -d ${GIT_DIR}/sequencer ]; then
__simple_sequence "Reverting"
else
__simple_action REVERT_HEAD revert "Reverting"
fi
fi) | ${PAGER}