2 # TopGit - A different patch queue manager
3 # Copyright (C) 2013 Per Cederqvist <ceder@lysator.liu.se>
4 # Copyright (C) 2015,2017 Kyle J. McKay <mackyle@gmail.com>
10 USAGE
="Usage: ${tgname:-tg} [...] checkout [--iow] [-f] [ [ next | prev ] [ -a ] | [goto] [--] <pattern> ]"
17 # Options of "next" and "prev".
20 # Arguments of "goto".
26 _head
="$(git rev-parse --revs-only --abbrev-ref=loose HEAD --)"
27 ref_exists
"refs/$topbases/$_head" && branch_annihilated
"$_head" && _checkout_opts
="-f"
28 git checkout
$iowoptval $forceval ${_checkout_opts} "$1"
32 printf '%s\n' "$USAGE" >&2
36 while [ $# -gt 0 ]; do
42 printf '%s\n' "$USAGE"
46 --ignore-other-worktrees|
--iow)
52 p|prev|previous|pop|parent|..
)
55 if [ -n "$goto" ]; then
56 if [ -n "$pattern" ]; then
64 [ "$arg" = "--" -o "$1" != "--" ] ||
shift
70 if [ -z "$goto" ]; then
75 if [ -z "$all$next$prev$goto" -a -n "$arg" ]; then
84 if [ "$goto$all" = 11 ]; then
85 die
"goto -a does not make sense."
88 if [ -z "$next$prev$goto" ]; then
89 # Default subcommand is "next". This was the most reasonable
90 # opposite of ".." that I could figure out. "goto" would also
91 # make sense as the default command, I suppose.
95 [ "$next$prev$goto" = "1" ] ||
{ err
"incompatible options"; usage
; }
97 [ -n "$tg_tmp_dir" ] || die
"tg-checkout must be run via '$tgdisplay checkout'"
98 _depfile
="$(mktemp "$tg_tmp_dir/tg-co-deps.XXXXXX
")"
99 _altfile
="$(mktemp "$tg_tmp_dir/tg-co-alt.XXXXXX
")"
101 if [ -n "$goto" ]; then
102 tg summary
-t |
grep -e "$pattern" >"$_altfile" ||
:
103 no_branch_found
="No topic branch matches grep pattern '$pattern'"
105 branch
="$(git symbolic-ref -q HEAD)" || die
"Working on a detached head"
106 branch
="$(git rev-parse --revs-only --abbrev-ref "$branch" --)"
108 if [ -n "$prev" ]; then
109 no_branch_found
="$branch does not depend on any topic"
111 no_branch_found
="No topic depends on $branch"
114 if [ -z "$all" ]; then
115 if [ -n "$prev" ]; then
116 tg prev
-w >"$_altfile"
121 tg summary
--deps >$_depfile || die
"$tgdisplay summary failed"
123 if [ -n "$prev" ]; then
128 script=@sharedir@
/leaves.
awk
129 awk -f @sharedir@
/leaves.
awk dir
=$dir start
=$branch <$_depfile |
sort >"$_altfile"
133 _alts
=$
(( $
(wc -l <"$_altfile") ))
134 if [ $_alts = 0 ]; then
135 die
"$no_branch_found"
136 elif [ $_alts = 1 ]; then
137 checkout
"$(cat "$_altfile")"
142 sed = |
paste -d ":" - - |
sed 's/^/ /;s/^ *\(......\):/\1 /'
145 echo Please
select one of the following topic branches
:
147 printf '%s' "Input the number: "
151 sane
="$(sed 's/[^0-9]//g' <<-EOT
155 if [ -z "$n" ] ||
[ "$sane" != "$n" ]; then
158 if [ $n -lt 1 ] ||
[ $n -gt $_alts ]; then
159 die
"Input out of range"
162 new_branch
="$(sed -n ${n}p "$_altfile")"
163 [ -n "$new_branch" ] || die
"Bad input"
165 checkout
"$new_branch"