tg: alternatively not so much leaking
[topgit/pro.git] / tg-annihilate.sh
blobbd120f026dc60a4b3694541d869dacb64ee9b290
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 v_verify_topgit_branch name HEAD
34 ! branch_annihilated "$name" || die "TopGit branch $name is already annihilated."
36 [ -z "$force" ] && { branch_empty "$name" || die "branch is non-empty: $name"; }
38 ## Annihilate
39 ensure_clean_tree
40 ensure_clean_topfiles
41 ensure_ident_available
42 alldeps="$(get_temp alldeps)"
43 tg --no-pager summary --deps >"$alldeps" || die "tg summary --deps failed"
44 mb="$(git merge-base "refs/$topbases/$name" "refs/heads/$name")"
45 git read-tree "$mb^{tree}"
46 # Need to pass --no-verify in order to inhibit TopGit's pre-commit hook to run,
47 # which would bark upon missing .top* files.
48 git commit --no-verify -m"TopGit branch $name annihilated."
50 # Propagate the dependencies through to dependents (if any), if they don't already have them
51 dependencies="$(awk -v annb="$name" 'NF == 2 && $2 != "" && $1 == annb { print $2 }' <"$alldeps")"
52 updatelist=
53 while read dependent && [ -n "$dependent" ]; do
54 # to avoid ambiguity with checkout -f we must use symbolic-ref + reset
55 git symbolic-ref HEAD "refs/heads/$dependent"
56 git reset -q --hard
57 needupdate=
58 while read dependency && [ -n "$dependency" ]; do
59 ! tg depend add --no-update "$dependency" >/dev/null 2>&1 || needupdate=1
60 done <<-EOT
61 $dependencies
62 EOT
63 [ -z "$needupdate" ] || updatelist="${updatelist:+$updatelist }$dependent"
64 done <<EOT
65 $(awk -v annb="$name" 'NF == 2 && $1 != "" && $2 == annb { print $1 }' <"$alldeps")
66 EOT
68 info "branch successfully annihilated: $name"
69 now="now"
70 if [ -n "$updatelist" ]; then
71 if [ -n "$update" ]; then
72 now="after the update completes"
73 else
74 info "skipping update because --no-update given"
75 info "be sure to update affected branches: $updatelist"
76 now="after updating"
79 info "If you have shared your work, you might want to run ${tgname:-tg} push $name $now."
80 if [ -n "$updatelist" ] && [ -n "$update" ]; then
81 info "now updating affected branches: $updatelist"
82 set -- $updatelist
83 . "$TG_INST_CMDDIR"/tg-update