2 # TopGit - A different patch queue manager
3 # Copyright (C) 2013 Per Cederqvist <ceder@lysator.liu.se>
4 # Copyright (C) 2015,2017,2018,2021 Kyle J. McKay <mackyle@gmail.com>
11 Usage: ${tgname:-tg} [...] checkout [<opt>...] [-b <branch>] (next | prev) [<steps>]
12 Or: ${tgname:-tg} [...] checkout [<opt>...] (next | prev) -a
13 Or: ${tgname:-tg} [...] checkout [<opt>...] [goto] [--] <pattern> | --series[=<head>]
15 --iow pass '--ignore-other-worktrees' to git >= v2.5.0
16 --force / -f pass '--force' option to git
17 --merge / -m pass '--merge' option to git
18 --quiet / -q pass '--quiet' option to git
19 --branch <branch> start at branch <branch> instead of HEAD
20 -b <branch> alias for --branch <branch>
21 --all / -a step as many times as possible"
25 [ -z "$2" ] ||
printf '%s\n' "$2" >&2
26 if [ "${1:-0}" != 0 ]; then
27 printf '%s\n' "$USAGE" >&2
29 printf '%s\n' "$USAGE"
37 iowoptval
= # "" or "$iowopt"
38 forceval
= # "" or "-f"
39 mergeval
= # "" or "-m"
40 quietval
= # "" or "-q"
41 dashdash
= # "" or "1" if [goto] "--" [--series[=<head>]] seen
43 while [ $# -gt 0 ]; do case "$1" in
56 --ignore-other-worktrees|
--iow)
60 branch
="${1#--branch=}"
61 [ -n "$branch" ] || usage
1 "--branch= requires a branch name"
64 [ $# -ge 2 ] || usage
1 "$1 requires an argument"
66 [ -n "$branch" ] || usage
1 "$1 requires a branch name"
74 [ "$1" != "goto" ] ||
[ "$2" != "--" ] ||
shift
83 usage
1 "Unknown option: $1"
89 [ $# -ne 0 ] ||
[ -n "$dashdash" ] ||
{
90 # deprecated "next" alias
91 warn
"support for \"tg checkout\" with no argument will soon be removed"
92 warn
"please switch to equivalent \"tg checkout +\" to avoid breakage"
95 [ $# -gt 0 ] || usage
1
97 [ -n "$dashdash" ] ||
{
98 case "$1" in +[0-9]*|
-[0-9]*)
101 set -- "${arg%%[0-9]*}" "${arg#?}" "$@"
105 +|n|next|push|child
) shift;;
106 -|p|prev|previous|pop|parent|..
) reverse
=1; shift;;
112 choices
="$(get_temp choices)"
115 if [ -n "$dashdash" ]; then
116 [ $# -eq 1 ] || usage
1 "goto mode requires exactly one pattern"
118 [ -n "$pattern" ] || usage
1 "goto mode requires a non-empty pattern"
121 tg
--no-pager info
"$pattern" "${branch:-HEAD}" >"$choices" ||
exit
125 [ -z "$branch" ] || usage
1 "--branch not allowed in goto <pattern> mode"
126 tg
--no-pager summary
--list |
grep -e "$pattern" >"$choices" ||
:
127 no_branch_found
="No topic branch matches grep pattern '$pattern'"
131 [ $# -gt 0 ] ||
set -- "1"
132 [ $# -eq 1 ] || usage
1 "next/previous permits no more than one argument"
135 [ -z "$branch" ] || usage
1 "--branch not allowed in --all mode"
136 navigate_deps
-t -s=-1 ${reverse:+-r} >"$choices"
137 no_branch_found
="No TopGit branches found at all!"
140 [ "$1" = "${1%%[!0-9]*}" ] || usage
1 "invalid next/previous step count"
141 v_verify_topgit_branch branch
"${branch:-HEAD}"
142 navigate_deps
-t -s="$1" ${reverse:+-r} ${pinstep:+-k} "$branch" >"$choices" ||
exit
145 [ -z "$reverse" ] || dir
="previous"
146 no_branch_found
="No $dir TopGit branch(es) found $1 step$pl away"
149 usage
1 "invalid next/previous movement; must be --all or positive number"
154 cnt
=$
(( $
(wc -l <"$choices") ))
155 [ $cnt -gt 0 ] || die
"$no_branch_found"
157 if [ $cnt -eq 1 ]; then
158 read -r choice
<"$choices" ||
:
159 choice
="${choice%%[ $tab]*}"
160 [ -n "$choice" ] || die
"$no_branch_found"
162 echo "Please select one of the following topic branches:"
163 awk -v "desc=$desc" <"$choices" '
164 BEGIN { colcount = 0 }
166 if (colcount) return colcount
167 sizer = "exec stty size 0>&2 2>/dev/null"
172 if (split(info, nums, " ") >= 2 && nums[2] ~ /^[1-9][0-9]*$/)
173 colcount = 0 + nums[2]
174 if (!colcount) colcount = 80
179 mark = substr($0, 1, 2)
180 names = substr($0, 3)
185 cnt = split(names, name, " ")
191 sub(/^[ \t]+/, "", annotation)
192 sub(/^[^ \t]+[ \t]+/, "", annotation)
193 sub(/[ \t]+$/, "", annotation)
194 if (annotation != "") annotation = " " annotation
196 for (i = 2; i <= cnt; ++i) {
197 annotation = annotation ", " name[i]
199 annotation = " [" substr(annotation, 3) "]"
203 line = sprintf("%-39s%s", sprintf("%6d %s%s", NR, mark, name[1]), annotation)
205 line = sprintf("%6d %s%s%s", NR, mark, name[1], annotation)
206 printf "%.*s\n", cols() - 1, line
208 printf '%s' "Input the number: "
210 [ -n "$n" ] && [ "$n" = "${n%%[!0-9]*}" ] || die
"Bad input"
211 [ $n -ge 1 ] && [ $n -le $cnt ] || die
"Input out of range"
212 choice
="$(sed -n ${n}p <"$choices")" ||
:
213 case "$choice" in "* "*|
" "*) choice
="${choice#??}"; esac
214 choice
="${choice%%[ $tab]*}"
215 [ -n "$choice" ] || die
"Bad input"
217 git checkout
$quietval $iowoptval $mergeval $forceval "$choice" --