help: use command-list.h for common command list
[git/git.git] / generate-cmdlist.sh
1 #!/bin/sh
2
3 die () {
4 echo "$@" >&2
5 exit 1
6 }
7
8 command_list () {
9 grep -v '^#' "$1"
10 }
11
12 get_categories () {
13 tr ' ' '\n'|
14 grep -v '^$' |
15 sort |
16 uniq
17 }
18
19 category_list () {
20 command_list "$1" |
21 cut -c 40- |
22 get_categories
23 }
24
25 get_synopsis () {
26 sed -n '
27 /^NAME/,/'"$1"'/H
28 ${
29 x
30 s/.*'"$1"' - \(.*\)/N_("\1")/
31 p
32 }' "Documentation/$1.txt"
33 }
34
35 define_categories () {
36 echo
37 echo "/* Command categories */"
38 bit=0
39 category_list "$1" |
40 while read cat
41 do
42 echo "#define CAT_$cat (1UL << $bit)"
43 bit=$(($bit+1))
44 done
45 test "$bit" -gt 32 && die "Urgh.. too many categories?"
46 }
47
48 print_command_list () {
49 echo "static struct cmdname_help command_list[] = {"
50
51 command_list "$1" |
52 while read cmd rest
53 do
54 printf " { \"$cmd\", $(get_synopsis $cmd), 0"
55 for cat in $(echo "$rest" | get_categories)
56 do
57 printf " | CAT_$cat"
58 done
59 echo " },"
60 done
61 echo "};"
62 }
63
64 echo "/* Automatically generated by generate-cmdlist.sh */
65 struct cmdname_help {
66 const char *name;
67 const char *help;
68 uint32_t category;
69 };
70 "
71 if test -z "$2"
72 then
73 define_categories "$1"
74 echo
75 print_command_list "$1"
76 exit 0
77 fi
78
79 echo "static const char *common_cmd_groups[] = {"
80
81 grps=grps$$.tmp
82 match=match$$.tmp
83 trap "rm -f '$grps' '$match'" 0 1 2 3 15
84
85 sed -n '
86 1,/^### common groups/b
87 /^### command list/q
88 /^#/b
89 /^[ ]*$/b
90 h;s/^[^ ][^ ]*[ ][ ]*\(.*\)/ N_("\1"),/p
91 g;s/^\([^ ][^ ]*\)[ ].*/\1/w '$grps'
92 ' "$1"
93 printf '};\n\n'
94
95 n=0
96 substnum=
97 while read grp
98 do
99 echo "^git-..*[ ]$grp"
100 substnum="$substnum${substnum:+;}s/[ ]$grp/$n/"
101 n=$(($n+1))
102 done <"$grps" >"$match"
103
104 printf 'static struct cmdname_help common_cmds[] = {\n'
105 grep -f "$match" "$1" |
106 sed 's/^git-//' |
107 sort |
108 while read cmd tags
109 do
110 tag=$(echo "$tags" | sed "$substnum; s/[^0-9]//g")
111 echo " {\"$cmd\", $(get_synopsis git-$cmd), $tag},"
112 done
113 echo "};"