tg.sh: next version is 0.16.1
[topgit/pro.git] / tg-checkout.sh
blob1aa019100316389ba017b9309e05d451c109e35f
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # GPLv2
5 ## Parse options
7 # Subcommands.
8 push=
9 pop=
10 goto=
12 # Options of "push" and "pop".
13 all=
15 # Arguments of "goto".
16 pattern=
18 checkout() {
19 _head="$(git rev-parse --abbrev-ref=loose HEAD)"
20 ref_exists "refs/top-bases/$_head" && branch_annihilated "$_head" && _checkout_opts="-f"
21 git checkout ${_checkout_opts} "$1"
24 while [ $# -gt 0 ]; do
25 arg="$1"
26 shift
28 case "$arg" in
29 -a)
30 all=1;;
31 child|next|push)
32 push=1;;
33 parent|prev|pop|..)
34 pop=1;;
35 goto)
36 goto=1
37 if [ $# -gt 0 ]; then
38 pattern="$1"
39 shift
40 fi;;
42 echo "Usage: ${tgname:-tg} [...] checkout [ [ push | pop ] [ -a ] | goto <pattern> ]" >&2
43 exit 1;;
44 esac
45 done
47 if [ "$goto$all" = 11 ]; then
48 die "goto -a does not make sense."
51 if [ -z "$push$pop$goto" ]; then
52 # Default subcommand is "push". This was the most reasonable
53 # opposite of ".." that I could figure out. "goto" would also
54 # make sense as the default command, I suppose.
55 push=1
58 _depfile="$(mktemp -t tg-co-deps.XXXXXX)"
59 _altfile="$(mktemp -t tg-co-alt.XXXXXX)"
60 trap "rm -f \"$_depfile\" \"$_altfile\"" 0
62 if [ -n "$goto" ]; then
63 $tg summary -t | grep -e "$pattern" >$_altfile || :
64 no_branch_found="No topic branch matches grep pattern '$pattern'"
65 else
66 branch=`git symbolic-ref -q HEAD` || die "Working on a detached head"
67 branch=`git rev-parse --abbrev-ref $branch`
69 if [ -n "$pop" ]; then
70 no_branch_found="$branch does not depend on any topic"
71 else
72 no_branch_found="No topic depends on $branch"
75 if [ -z "$all" ]; then
76 if [ -n "$pop" ]; then
77 $tg prev -w >$_altfile
78 else
79 $tg next >$_altfile
81 else
82 $tg summary --deps >$_depfile || die "${tgname:-tg} summary failed"
84 if [ -n "$pop" ]; then
85 dir=pop
86 else
87 dir=push
89 script=@sharedir@/leaves.awk
90 awk -f @sharedir@/leaves.awk dir=$dir start=$branch <$_depfile | sort >$_altfile
94 _alts=`wc_l < $_altfile`
95 if [ $_alts = 0 ]; then
96 die "$no_branch_found"
97 elif [ $_alts = 1 ]; then
98 checkout `cat $_altfile`
99 exit $?
102 echo Please select one of the following topic branches:
103 cat -n $_altfile
104 printf '%s' "Input the number: "
105 read n
107 # Check the input
108 sane=`echo $n|sed 's/[^0-9]//g'`
109 if [ -z "$n" ] || [ "$sane" != "$n" ]; then
110 die "Bad input"
112 if [ $n -lt 1 ] || [ $n -gt $_alts ]; then
113 die "Input out of range"
116 new_branch=`sed -n ${n}p $_altfile`
117 [ -n "$new_branch" ] || die "Bad input"
119 checkout $new_branch