chiark / gitweb /
6312d0064d9fcb9529cf24b11f0aea1324bb79ae
[topgit.git] / tg.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6
7 ## Auxiliary functions
8
9 info()
10 {
11         echo "${TG_RECURSIVE}tg: $*"
12 }
13
14 die()
15 {
16         info "fatal: $*"
17         exit 1
18 }
19
20 # setup_hook NAME
21 setup_hook()
22 {
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
28         fi
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"
34         fi
35         # Insert call into the hook
36         {
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"
43 }
44
45 # setup_ours (no arguments)
46 setup_ours()
47 {
48         if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
49                 {
50                         echo -e ".topmsg\tmerge=ours"
51                         echo -e ".topdeps\tmerge=ours"
52                 } >>"$git_dir/info/attributes"
53         fi
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'
57         fi
58 }
59
60 # measure_branch NAME [BASE]
61 measure_branch()
62 {
63         _bname="$1"; _base="$2"
64         [ -n "$_base" ] || _base="refs/top-bases/$_bname"
65         # The caller should've verified $name is valid
66         _commits="$(git rev-list "$_bname" ^"$_base" | wc -l)"
67         _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" | wc -l)"
68         if [ $_commits -gt 1 ]; then
69                 _suffix="commits"
70         else
71                 _suffix="commit"
72         fi
73         echo "$_commits/$_nmcommits $_suffix"
74 }
75
76 # branch_contains B1 B2
77 # Whether B1 is a superset of B2.
78 branch_contains()
79 {
80         [ -z "$(git rev-list ^"$1" "$2")" ]
81 }
82
83 # recurse_deps CMD NAME [BRANCHPATH...]
84 # Recursively eval CMD on all dependencies of NAME.
85 # CMD can refer to $_name for queried branch name,
86 # $_dep for dependency name,
87 # $_depchain for space-seperated branch backtrace,
88 # and the $_dep_is_tgish boolean.
89 # It can modify $_ret to affect the return value
90 # of the whole function.
91 # If recurse_deps() hits missing dependencies, it will append
92 # them to space-separated $missing_deps list and skip them.
93 recurse_deps()
94 {
95         _cmd="$1"; shift
96         _name="$1"; # no shift
97         _depchain="$*"
98         _depsfile="$(mktemp)"
99         git cat-file blob "$_name:.topdeps" >"$_depsfile"
100         _ret=0
101         while read _dep; do
102                 if ! git rev-parse --verify "$_dep" >/dev/null 2>&1; then
103                         # All hope is lost
104                         missing_deps="$missing_deps $_dep"
105                         continue
106                 fi
107
108                 _dep_is_tgish=1
109                 git rev-parse --verify "refs/top-bases/$_dep" >/dev/null 2>&1 ||
110                         _dep_is_tgish=
111
112                 # Shoo shoo, keep our environment alone!
113                 [ -z "$_dep_is_tgish" ] ||
114                         (needs_update "$_cmd" "$_dep" "$@") ||
115                         _ret=$?
116
117                 eval "$_cmd"
118         done <"$_depsfile"
119         missing_deps="${missing_deps# }"
120         rm "$_depsfile"
121         return $_ret
122 }
123
124 # branch_needs_update
125 # This is a helper function for determining whether given branch
126 # is up-to-date wrt. its dependencies. It expects input as if it
127 # is called as a recurse_deps() helper.
128 # In case the branch does need update, it will echo it together
129 # with the branch backtrace on the output (see needs_update()
130 # description for details) and set $_ret to non-zero.
131 branch_needs_update()
132 {
133         _dep_base_uptodate=1
134         if [ -n "$_dep_is_tgish" ]; then
135                 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
136         fi
137
138         if [ -z "$_dep_base_uptodate" ]; then
139                 # _dep needs to be synced with its base
140                 echo ": $_dep $_depchain"
141                 _ret=1
142         elif ! branch_contains "refs/top-bases/$_name" "$_dep"; then
143                 # Some new commits in _dep
144                 echo "$_dep $_depchain"
145                 _ret=1
146         fi
147 }
148
149 # needs_update NAME
150 # This function is recursive; it outputs reverse path from NAME
151 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
152 # inner paths first. Innermost name can be ':' if the head is
153 # not in sync with the base.
154 # It will also return non-zero status if NAME needs update.
155 # If needs_update() hits missing dependencies, it will append
156 # them to space-separated $missing_deps list and skip them.
157 needs_update()
158 {
159         recurse_deps branch_needs_update "$@"
160 }
161
162 # branch_empty NAME
163 branch_empty()
164 {
165         [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v "     .top")" ]
166 }
167
168 # switch_to_base NAME [SEED]
169 switch_to_base()
170 {
171         _base="refs/top-bases/$1"; _seed="$2"
172         # We have to do all the hard work ourselves :/
173         # This is like git checkout -b "$_base" "$_seed"
174         # (or just git checkout "$_base"),
175         # but does not create a detached HEAD.
176         git read-tree -u -m HEAD "${_seed:-$_base}"
177         [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
178         git symbolic-ref HEAD "$_base"
179 }
180
181 # Show the help messages.
182 do_help()
183 {
184         if [ -z "$1" ] ; then
185                 ## Build available commands list for help output
186
187                 cmds=
188                 sep=
189                 for cmd in "@cmddir@"/tg-*; do
190                         ! [ -r "$cmd" ] && continue
191                         # strip directory part and "tg-" prefix
192                         cmd="$(basename "$cmd")"
193                         cmd="${cmd#tg-}"
194                         cmds="$cmds$sep$cmd"
195                         sep="|"
196                 done
197
198                 echo "TopGit v0.1 - A different patch queue manager"
199                 echo "Usage: tg ($cmds|help) ..."
200         elif [ -r "@sharedir@/tg-$1.txt" ] ; then
201                 cat "@sharedir@/tg-$1.txt"
202         else
203                 echo "`basename $0`: no help for $1" 1>&2
204         fi
205 }
206
207
208 ## Initial setup
209
210 set -e
211 git_dir="$(git rev-parse --git-dir)"
212 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
213 # make sure merging the .top* files will always behave sanely
214 setup_ours
215 setup_hook "pre-commit"
216
217 [ -d "@cmddir@" ] ||
218         die "No command directory: '@cmddir@'"
219
220 ## Dispatch
221
222 # We were sourced from another script for our utility functions;
223 # this is set by hooks.
224 [ -z "$tg__include" ] || return 0
225
226 cmd="$1"
227 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
228 shift
229
230 case "$cmd" in
231 help|--help|-h)
232         do_help "$1"
233         exit 1;;
234 --hooks-path)
235         # Internal command
236         echo "@hooksdir@";;
237 *)
238         [ -r "@cmddir@"/tg-$cmd ] || {
239                 echo "Unknown subcommand: $cmd" >&2
240                 exit 1
241         }
242         . "@cmddir@"/tg-$cmd;;
243 esac