2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2017 Kyle J. McKay <mackyle@gmail.com>
8 force
= # Whether to delete non-empty branch, or branch where only the base is left.
9 stash
= # tgstash refs before changes
12 if [ "$(git config --get --bool topgit.autostash 2>/dev/null)" != "false" ]; then
13 # topgit.autostash is true (or unset)
23 force
=$
(( $force +1 ));;
29 echo "Usage: ${tgname:-tg} [...] delete [-f] <name>" >&2
32 [ -z "$name" ] || die
"name already specified ($name)"
40 [ -n "$name" ] || die
"no branch name specified"
41 branchrev
="$(git rev-parse --verify "refs
/heads
/$name^
0" -- 2>/dev/null)" ||
42 if [ -n "$force" ]; then
43 info
"invalid branch name: $name; assuming it has been deleted already"
45 die
"invalid branch name: $name"
47 baserev
="$(git rev-parse --verify "refs
/$topbases/$name^
0" -- 2>/dev/null)" ||
48 die
"not a TopGit topic branch: $name"
49 ! headsym
="$(git symbolic-ref -q HEAD)" ||
[ "$headsym" != "refs/heads/$name" ] ||
{
50 [ -n "$force" ] && [ "$force" -ge 2 ] || die
"cannot delete your current branch"
51 warn
"detaching HEAD to delete current branch"
52 git update-ref
-m "tgdelete: detach HEAD to delete $name" --no-deref HEAD
"$branchrev"
53 git
--no-pager log
-n 1 --format=format
:'HEAD is now at %h... %s' HEAD
56 [ -z "$force" ] && { branch_empty
"$name" || die
"branch is non-empty: $name"; }
58 # Quick'n'dirty check whether branch is required
59 [ -z "$force" ] && { tg summary
--deps | cut
-d' ' -f2- |
tr ' ' '\n' |
grep -Fxq -- "$name" && die
"some branch depends on $name"; }
61 ensure_ident_available
63 # always auto stash even if it's just to the anonymous stash TG_STASH
65 stashmsg
="tgdelete: autostash before delete branch $name"
66 if [ -n "$stash" ]; then
67 tg tag
-q -q -m "$stashmsg" --stash $name &&
68 stashhash
="$(git rev-parse --quiet --verify refs/tgstash --)" &&
69 [ -n "$stashhash" ] &&
70 [ "$(git cat-file -t "$stashhash" -- 2>/dev/null)" = "tag" ] ||
71 die
"requested --stash failed"
73 tg tag
--anonymous $name &&
74 stashhash
="$(git rev-parse --quiet --verify TG_STASH --)" &&
75 [ -n "$stashhash" ] &&
76 [ "$(git cat-file -t "$stashhash" -- 2>/dev/null)" = "tag" ] ||
77 die
"anonymous --stash failed"
82 git update-ref
-d "refs/$topbases/$name" "$baserev"
83 [ -z "$branchrev" ] || git update-ref
-d "refs/heads/$name" "$branchrev"