tg.sh: Add -h, --help aliases for the help command
[topgit.git] / tg.sh
blobd520a230096ebdfbe442af71f333c59c7a9a1c32
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
7 ## Auxiliary functions
9 info()
11 echo "${TG_RECURSIVE}tg: $*"
14 die()
16 info "fatal: $*"
17 exit 1
20 # setup_hook NAME
21 setup_hook()
23 hook_call="\"\$(tg --hooks-path)\"/$1 \"\$@\""
24 if [ -f "$git_dir/hooks/$1" ] &&
25 fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
26 # Another job well done!
27 return
29 # Prepare incanation
30 if [ -x "$git_dir/hooks/$1" ]; then
31 hook_call="$hook_call"' || exit $?'
32 else
33 hook_call="exec $hook_call"
35 # Insert call into the hook
37 echo "#!/bin/sh"
38 echo "$hook_call"
39 [ ! -s "$git_dir/hooks/$1" ] || cat "$git_dir/hooks/$1"
40 } >"$git_dir/hooks/$1+"
41 chmod a+x "$git_dir/hooks/$1+"
42 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
45 # setup_ours (no arguments)
46 setup_ours()
48 if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
50 echo -e ".topmsg\tmerge=ours"
51 echo -e ".topdeps\tmerge=ours"
52 } >>"$git_dir/info/attributes"
54 if ! git config merge.ours.driver >/dev/null; then
55 git config merge.ours.name '"always keep ours" merge driver'
56 git config merge.ours.driver 'touch %A'
60 # measure_branch NAME [BASE]
61 measure_branch()
63 _name="$1"; _base="$2"
64 [ -n "$_base" ] || _base="refs/top-bases/$_name"
65 # The caller should've verified $name is valid
66 _commits="$(git rev-list "$_name" ^"$_base" | wc -l)"
67 _nmcommits="$(git rev-list --no-merges "$_name" ^"$_base" | wc -l)"
68 if [ $_commits -gt 1 ]; then
69 _suffix="commits"
70 else
71 _suffix="commit"
73 echo "$_commits/$_nmcommits $_suffix"
76 # branch_contains B1 B2
77 # Whether B1 is a superset of B2.
78 branch_contains()
80 [ -z "$(git rev-list ^"$1" "$2")" ]
83 # needs_update NAME [BRANCHPATH...]
84 # This function is recursive; it outputs reverse path from NAME
85 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
86 # inner paths first. Innermost name can be ':' if the head is
87 # not in sync with the base.
88 # It will also return non-zero status if NAME needs update.
89 # If needs_update() hits missing dependencies, it will append
90 # them to space-separated $missing_deps list and skip them.
91 needs_update()
93 depsfile="$(mktemp)"
94 git cat-file blob "$1:.topdeps" >"$depsfile"
95 _ret=0
96 while read _dep; do
97 if ! git rev-parse --verify "$_dep" >/dev/null 2>&1; then
98 # All hope is lost
99 missing_deps="$missing_deps $_dep"
100 continue
103 _dep_is_tgish=1
104 git rev-parse --verify "refs/top-bases/$_dep" >/dev/null 2>&1 ||
105 _dep_is_tgish=
107 # Shoo shoo, keep our environment alone!
108 [ -z "$_dep_is_tgish" ] ||
109 (needs_update "$_dep" "$@") ||
110 _ret=$?
112 _dep_base_uptodate=1
113 if [ -n "$_dep_is_tgish" ]; then
114 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
117 if [ -z "$_dep_base_uptodate" ]; then
118 # _dep needs to be synced with its base
119 echo ": $_dep $*"
120 _ret=1
121 elif ! branch_contains "refs/top-bases/$1" "$_dep"; then
122 # Some new commits in _dep
123 echo "$_dep $*"
124 _ret=1
126 done <"$depsfile"
127 missing_deps="${missing_deps# }"
128 rm "$depsfile"
129 return $_ret
132 # branch_empty NAME
133 branch_empty()
135 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v " .top")" ]
138 # switch_to_base NAME [SEED]
139 switch_to_base()
141 _base="refs/top-bases/$1"; _seed="$2"
142 # We have to do all the hard work ourselves :/
143 # This is like git checkout -b "$_base" "$_seed"
144 # (or just git checkout "$_base"),
145 # but does not create a detached HEAD.
146 git read-tree -u -m HEAD "${_seed:-$_base}"
147 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
148 git symbolic-ref HEAD "$_base"
151 # Show the help messages.
152 do_help()
154 if [ -z "$1" ] ; then
155 ## Build available commands list for help output
157 cmds=
158 sep=
159 for cmd in "@cmddir@"/tg-*; do
160 ! [ -r "$cmd" ] && continue
161 # strip directory part and "tg-" prefix
162 cmd="$(basename "$cmd")"
163 cmd="${cmd#tg-}"
164 cmds="$cmds$sep$cmd"
165 sep="|"
166 done
168 echo "TopGit v0.1 - A different patch queue manager"
169 echo "Usage: tg ($cmds|help) ..."
170 elif [ -r "@sharedir@/tg-$1.txt" ] ; then
171 cat "@sharedir@/tg-$1.txt"
172 else
173 echo "`basename $0`: no help for $1" 1>&2
178 ## Initial setup
180 set -e
181 git_dir="$(git rev-parse --git-dir)"
182 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
183 # make sure merging the .top* files will always behave sanely
184 setup_ours
185 setup_hook "pre-commit"
187 [ -d "@cmddir@" ] ||
188 die "No command directory: '@cmddir@'"
190 ## Dispatch
192 # We were sourced from another script for our utility functions;
193 # this is set by hooks.
194 [ -z "$tg__include" ] || return 0
196 cmd="$1"
197 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
198 shift
200 case "$cmd" in
201 help|--help|-h)
202 do_help "$1"
203 exit 1;;
204 --hooks-path)
205 # Internal command
206 echo "@hooksdir@";;
208 [ -r "@cmddir@"/tg-$cmd ] || {
209 echo "Unknown subcommand: $cmd" >&2
210 exit 1
212 . "@cmddir@"/tg-$cmd;;
213 esac