tg: accomodate multiple git worktrees
[topgit/pro.git] / tg-annihilate.sh
blob288f4a2d407bb8aebc16fd426b9a834c0be4fa82
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (C) Petr Baudis <pasky@suse.cz> 2008
4 # (C) Per Cederqvist <ceder@lysator.liu.se> 2010
5 # (C) Kyle J. McKay <mackyle@gmail.com> 2017
6 # All rights reserved.
7 # GPLv2
9 force= # Whether to annihilate non-empty branch, or branch where only the base is left.
10 update=1 # Whether to run tg update on affected branches
13 ## Parse options
15 while [ -n "$1" ]; do
16 arg="$1"; shift
17 case "$arg" in
18 -f|--force)
19 force=1;;
20 --no-update)
21 update=;;
22 --update)
23 update=1;;
25 echo "Usage: ${tgname:-tg} [...] annihilate [-f] [--no-update]" >&2
26 exit 1;;
27 esac
28 done
31 ## Sanity checks
33 name="$(verify_topgit_branch HEAD)"
35 [ -z "$force" ] && { branch_empty "$name" || die "branch is non-empty: $name"; }
37 ## Annihilate
38 ensure_ident_available
39 mb="$(git merge-base "refs/$topbases/$name" "refs/heads/$name")"
40 git read-tree "$mb^{tree}"
41 # Need to pass --no-verify in order to inhibit TopGit's pre-commit hook to run,
42 # which would bark upon missing .top* files.
43 git commit --no-verify -m"TopGit branch $name annihilated."
45 # Propagate the dependencies through to dependents (if any), if they don't already have them
46 dependencies="$(tg prev -w)"
47 updatelist=
48 while read dependent && [ -n "$dependent" ]; do
49 git checkout -f "refs/heads/$dependent"
50 needupdate=
51 while read dependency && [ -n "$dependency" ]; do
52 ! $tg depend add --no-update "$dependency" >/dev/null 2>&1 || needupdate=1
53 done <<-EOT
54 $dependencies
55 EOT
56 [ -z "$needupdate" ] || updatelist="${updatelist:+$updatelist }$dependent"
57 done <<EOT
58 $($tg next)
59 EOT
61 info "branch successfully annihilated: $name"
62 now="now"
63 if [ -n "$updatelist" ]; then
64 if [ -n "$update" ]; then
65 info "now updating affected branches: $updatelist"
66 while read dependent && [ -n "$dependent" ]; do
67 $tg update "$dependent"
68 done <<-EOT
69 $(sed 'y/ /\n/' <<-LIST
70 $updatelist
71 LIST
73 EOT
74 else
75 info "skipping update because --no-update given"
76 info "be sure to update affected branches: $updatelist"
77 now="after updating"
81 info "If you have shared your work, you might want to run ${tgname:-tg} push $name $now."
82 git status
84 # vim:noet