tg.sh: use --no-deref when returning to a detached HEAD
[topgit/pro.git] / tg-delete.sh
blob24618b801f1d4b4cc062361cea2a7ab1d915a2bc
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 force= # Whether to delete non-empty branch, or branch where only the base is left.
7 name=
10 ## Parse options
12 while [ -n "$1" ]; do
13 arg="$1"; shift
14 case "$arg" in
15 -f|--force)
16 force=$(( $force +1 ));;
17 -*)
18 echo "Usage: ${tgname:-tg} [...] delete [-f] <name>" >&2
19 exit 1;;
21 [ -z "$name" ] || die "name already specified ($name)"
22 name="$arg";;
23 esac
24 done
27 ## Sanity checks
29 [ -n "$name" ] || die "no branch name specified"
30 branchrev="$(git rev-parse --verify "refs/heads/$name" -- 2>/dev/null)" ||
31 if [ -n "$force" ]; then
32 info "invalid branch name: $name; assuming it has been deleted already"
33 else
34 die "invalid branch name: $name"
36 baserev="$(git rev-parse --verify "refs/$topbases/$name" -- 2>/dev/null)" ||
37 die "not a TopGit topic branch: $name"
38 ! headsym="$(git symbolic-ref -q HEAD)" || [ "$headsym" != "refs/heads/$name" ] || {
39 [ -n "$force" ] && [ "$force" -ge 2 ] || die "cannot delete your current branch"
40 warn "detaching HEAD to delete current branch"
41 git update-ref -m "tgdelete: detach HEAD to delete $name" --no-deref HEAD "$branchrev"
42 git --no-pager log -n 1 --format=format:'HEAD is now at %h... %s' HEAD
45 [ -z "$force" ] && { branch_empty "$name" || die "branch is non-empty: $name"; }
47 # Quick'n'dirty check whether branch is required
48 [ -z "$force" ] && { tg summary --deps | cut -d' ' -f2- | tr ' ' '\n' | grep -Fxq -- "$name" && die "some branch depends on $name"; }
50 ## Wipe out
52 git update-ref -d "refs/$topbases/$name" "$baserev"
53 [ -z "$branchrev" ] || git update-ref -d "refs/heads/$name" "$branchrev"
55 # vim:noet