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>
11 Usage: ${tgname:-tg} [...] checkout [--iow] [-f] [-b <branch>] (next | prev) [<steps>]
12 Or: ${tgname:-tg} [...] checkout [--iow] [-f] (next | prev) -a
13 Or: ${tgname:-tg} [...] checkout [--iow] [-f] [goto] [--] <pattern> | --series[=<head>]"
17 [ -z "$2" ] ||
printf '%s\n' "$2" >&2
18 if [ "${1:-0}" != 0 ]; then
19 printf '%s\n' "$USAGE" >&2
21 printf '%s\n' "$USAGE"
29 iowoptval
= # "" or "$iowopt"
30 forceval
= # "" or "-f"
31 dashdash
= # "" or "1" if [goto] "--" [--series[=<head>]] seen
33 while [ $# -gt 0 ]; do case "$1" in
40 --ignore-other-worktrees|
--iow)
44 branch
="${1#--branch=}"
45 [ -n "$branch" ] || usage
1 "--branch= requires a branch name"
48 [ $# -ge 2 ] || usage
1 "$1 requires an argument"
50 [ -n "$branch" ] || usage
1 "$1 requires a branch name"
58 [ "$1" != "goto" ] ||
[ "$2" != "--" ] ||
shift
64 usage
1 "Unknown option: $1"
70 [ $# -ne 0 ] ||
[ -n "$dashdash" ] ||
set -- "next" # deprecated "next" alias
71 [ $# -gt 0 ] || usage
1
74 n|next|push|child
) shift;;
75 p|prev|previous|pop|parent|..
) reverse
=1; shift;;
80 choices
="$(get_temp choices)"
83 if [ -n "$dashdash" ]; then
84 [ $# -eq 1 ] || usage
1 "goto mode requires exactly one pattern"
86 [ -n "$pattern" ] || usage
1 "goto mode requires a non-empty pattern"
89 tg
--no-pager info
"$pattern" "${branch:-HEAD}" >"$choices" ||
exit
93 [ -z "$branch" ] || usage
1 "--branch not allowed in goto <pattern> mode"
94 tg
--no-pager summary
--list |
grep -e "$pattern" >"$choices" ||
:
95 no_branch_found
="No topic branch matches grep pattern '$pattern'"
99 [ $# -gt 0 ] ||
set -- "1"
100 [ $# -eq 1 ] || usage
1 "next/previos permits no more than one argument"
103 [ -z "$branch" ] || usage
1 "--branch not allowed in --all mode"
104 navigate_deps
-t -s=-1 ${reverse:+-r} >"$choices"
105 no_branch_found
="No TopGit branches found at all!"
108 [ "$1" = "${1%%[!0-9]*}" ] || usage
1 "invalid next/previous step count"
109 v_verify_topgit_branch branch
"${branch:-HEAD}"
110 navigate_deps
-t -s="$1" ${reverse:+-r} "$branch" >"$choices" ||
exit
112 [ "$steps" != 1 ] || pl
=
113 [ -z "$reverse" ] || dir
="previous"
114 no_branch_found
="No $dir TopGit branch(es) found $steps$pl away"
117 usage
1 "invalid next/previous movement; must be --all or positive number"
122 cnt
=$
(( $
(wc -l <"$choices") ))
123 [ $cnt -gt 0 ] || die
"$no_branch_found"
125 if [ $cnt -eq 1 ]; then
126 read -r choice
<"$choices" ||
:
127 choice
="${choice%%[ $tab]*}"
128 [ -n "$choice" ] || die
"$no_branch_found"
130 echo "Please select one of the following topic branches:"
131 awk -v "desc=$desc" <"$choices" '
132 BEGIN { colcount = 0 }
134 if (colcount) return colcount
135 sizer = "exec stty size 0>&2 2>/dev/null"
140 if (split(info, nums, " ") >= 2 && nums[2] ~ /^[1-9][0-9]*$/)
141 colcount = 0 + nums[2]
142 if (!colcount) colcount = 80
147 mark = substr($0, 1, 2)
148 names = substr($0, 3)
153 cnt = split(names, name, " ")
159 sub(/^[ \t]+/, "", annotation)
160 sub(/^[^ \t]+[ \t]+/, "", annotation)
161 sub(/[ \t]+$/, "", annotation)
162 if (annotation != "") annotation = " " annotation
164 for (i = 2; i <= cnt; ++i) {
165 annotation = annotation ", " name[i]
167 annotation = " [" substr(annotation, 3) "]"
171 line = sprintf("%-39s%s", sprintf("%6d %s%s", NR, mark, name[1]), annotation)
173 line = sprintf("%6d %s%s%s", NR, mark, name[1], annotation)
174 printf "%.*s\n", cols() - 1, line
176 printf '%s' "Input the number: "
178 [ -n "$n" ] && [ "$n" = "${n%%[!0-9]*}" ] || die
"Bad input"
179 [ $n -ge 1 ] && [ $n -le $cnt ] || die
"Input out of range"
180 choice
="$(sed -n ${n}p <"$choices")" ||
:
181 case "$choice" in "* "*|
" "*) choice
="${choice#??}"; esac
182 choice
="${choice%%[ $tab]*}"
183 [ -n "$choice" ] || die
"Bad input"
185 git checkout
$iowoptval $forceval "$choice" --