-
Notifications
You must be signed in to change notification settings - Fork 0
/
cg-help
executable file
·176 lines (156 loc) · 5.2 KB
/
cg-help
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
#!/usr/bin/env bash
#
# Show help for Cogito commands
# Copyright (c) Petr Baudis, 2005
#
# Takes an optional argument describing the command to show the help for.
# The command can be specified either as 'COMMAND' or 'cg-COMMAND'.
# If the argument is left out an overview of all the Cogito commands will
# be shown.
#
# Note, short help for a command is also available by passing `--help` or
# `-h` to the command. The complete command manual is shown when passing
# `--long-help` (and is the same as doing "`cg-help command`").
#
# OPTIONS
# -------
# -c:: Colorize
# Colorize the output. You can customize the colors using the
# $CG_COLORS environment variable (see below).
#
# ENVIRONMENT VARIABLES
# ---------------------
# PAGER::
# The pager to display log information in, defaults to `less`.
#
# PAGER_FLAGS::
# Flags to pass to the pager.
#
# CG_COLORS::
# Colon-separated list of 'name=color' pairs, where name is
# one of helpcopy, helpusage, helpsection, helplitem, helpcgcmd,
# helpcode, helpempth, default, and value is an ECMA-48 SGR
# sequence (see e.g. console_codes(4)).
#
# CG_COLORS_AUTO::
# Even if -c was passed or specified in ~/.cgrc, if this option
# is set, use colors only when the output is a terminal and it
# supports colors.
#
# CG_LESS::
# This is what the $LESS environment variable value will be set
# to before invoking $PAGER. It defaults to $LESS concatenated
# with the `R` flag to allow displaying of colorized output.
#
# CONFIGURATION VARIABLES
# -----------------------
# The following GIT configuration file variables are recognized:
#
# help.usecolor::
# If enabled, colorify the output like with -c if the output
# is a terminal.
# Testsuite: TODO
USAGE="cg-help [-c] [cg-COMMAND | COMMAND]"
_git_repo_unneeded=1
. "${COGITO_LIB}"cg-Xlib || exit 1
setup_colors()
{
local C="helpcopy=34:helpusage=33"
C="$C:helpsection=35:helplitem=33"
C="$C:helpcgcmd=32:helpcode=36"
C="$C:helpempth=31"
C="$C:default=0"
colorify_setup "$C"
apply_colors="
s/^\(-.*\)::.*/$colhelplitem\1$coldefault:/
s/^\(.*\)::/$colhelplitem\1$coldefault:/
s/\`\(cg-[a-z-]*\)\`/$colhelpcgcmd\1$coldefault/g
s/\`\([^\`]*\)\`/$colhelpcode&$coldefault/g
s/[^A-Z0-9a-z_-]\$ .*/$colhelpcode&$coldefault/g
s/'\([^ ]*\)'/$colhelpemph&$coldefault/g
s/'\(-[A-Z0-9a-z_-]* [^']*\)'/$colhelpemph&$coldefault/g
s/^Usage: .*/$colhelpusage&$coldefault/
/^[A-Z -_]*/,/^---*$/s/^[A-Z -_]*\$/$colhelpsection&$coldefault/
s/^Copyright .*/$colhelpcopy&$coldefault/
"
}
print_command_listing()
{
width="$(IFS=$'\n'; echo "$*" | column_width "$bin_path/" 25)"
for command in "$@"; do
[ -f "$command" ] || continue
if [ -L "$command" ]; then
tg="$(readlink "$command")"
# Skip symlinks within the same directory, those are
# aliases; symlinks to elsewhere are permitted since
# they may be created by some weirder packaging sys.
[ z"${tg#*/}" != z"$tg" ] || continue
fi
cmdname="${command#$bin_path/}"
shortdesc="$(sed -n 'n;n;p;q' <"$command")"
# Some minimal sanity check that we didn't pick up some
# random binary named cg-*
[ "${shortdesc:0:1}" = "#" ] || continue
columns_print '' t- "$cmdname" $width " ${shortdesc:2}"
done
}
colorize() {
sed -e "$apply_colors" | pager
}
colors=
apply_colors=
while optparse; do
if optparse -c; then
colors=1
else
optfail
fi
done
colorify_detect "$colors" help && setup_colors
bin_path="$(dirname "$0")"
if [ "$ARGS" = "admin" ]; then
echo "The advanced (low-level, dangerous, or administrative) commands:"
IFS=$'\n' print_command_listing $(ls "$bin_path"/cg-admin*)
exit
elif [ "$ARGS" = "branch" ]; then
echo "The branch commands family:"
IFS=$'\n' print_command_listing $(ls "$bin_path"/cg-branch*)
exit
elif [ "$ARGS" = "tag" ]; then
echo "The tag commands family:"
IFS=$'\n' print_command_listing $(ls "$bin_path"/cg-tag*)
exit
elif [ "$ARGS" ]; then
cmd="$(echo "${ARGS[0]}" | sed 's/^cg-//')"
print_help long "$cmd" | colorize
[ "${PIPESTATUS[0]}" -eq 0 ] && exit
echo "cg-help: no help available for command \"${ARGS[0]}\""
echo "Call cg-help without any arguments for the list of available commands"
exit 1
fi
REGULAR_COMMANDS="$(ls "$bin_path"/cg-* | grep -v /cg-X | grep -v /cg-admin | grep -v /cg-branch | grep -v /cg-tag)"
# TODO: Some uberevil `column` tricks...
BRANCH_COMMANDS="$(ls "$bin_path"/cg-branch* | sed 's#.*/##' | tr '\n' ' ')"
TAG_COMMANDS="$(ls "$bin_path"/cg-tag* | sed 's#.*/##' | tr '\n' ' ')"
ADVANCED_COMMANDS="$(ls "$bin_path"/cg-admin-* | sed 's#.*/##' | tr '\n' ' ')"
IFS=$'\n' colorize <<__END__
The Cogito version control system $(cg-version)
Available regular commands:
$(print_command_listing $REGULAR_COMMANDS)
Special command families:
cg-help admin ($ADVANCED_COMMANDS)
cg-help branch ($BRANCH_COMMANDS)
cg-help tag ($TAG_COMMANDS)
These expressions can be used interchangeably as "ID"s:
empty string, "this" or "HEAD" (current HEAD)
branch name (as registered with cg-branch-add)
tag name (as registered with cg-tag)
date string (as recognized by the date tool)
shortcut hash (shorted unambiguous hash lead)
commit object hash (as returned by cg-object-id -c)
tree object hash (accepted only by some commands)
For details on individual commands, do e.g.:
cg-help cg-log
cg-log --long-help
(both variants are equivalent)
__END__