2 # TopGit - A different patch queue manager
3 # Copyright (C) 2013 Per Cederqvist <ceder@lysator.liu.se>
4 # Copyright (C) 2015,2017,2018 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 mergeval
= # "" or "-m"
32 quietval
= # "" or "-q"
33 dashdash
= # "" or "1" if [goto] "--" [--series[=<head>]] seen
35 while [ $# -gt 0 ]; do case "$1" in
48 --ignore-other-worktrees|
--iow)
52 branch
="${1#--branch=}"
53 [ -n "$branch" ] || usage
1 "--branch= requires a branch name"
56 [ $# -ge 2 ] || usage
1 "$1 requires an argument"
58 [ -n "$branch" ] || usage
1 "$1 requires a branch name"
66 [ "$1" != "goto" ] ||
[ "$2" != "--" ] ||
shift
75 usage
1 "Unknown option: $1"
81 [ $# -ne 0 ] ||
[ -n "$dashdash" ] ||
{
82 # deprecated "next" alias
83 warn
"support for \"tg checkout\" with no argument will soon be removed"
84 warn
"please switch to equivalent \"tg checkout +\" to avoid breakage"
87 [ $# -gt 0 ] || usage
1
89 [ -n "$dashdash" ] ||
{
90 case "$1" in +[0-9]*|
-[0-9]*)
93 set -- "${arg%%[0-9]*}" "${arg#?}" "$@"
97 +|n|next|push|child
) shift;;
98 -|p|prev|previous|pop|parent|..
) reverse
=1; shift;;
104 choices
="$(get_temp choices)"
107 if [ -n "$dashdash" ]; then
108 [ $# -eq 1 ] || usage
1 "goto mode requires exactly one pattern"
110 [ -n "$pattern" ] || usage
1 "goto mode requires a non-empty pattern"
113 tg
--no-pager info
"$pattern" "${branch:-HEAD}" >"$choices" ||
exit
117 [ -z "$branch" ] || usage
1 "--branch not allowed in goto <pattern> mode"
118 tg
--no-pager summary
--list |
grep -e "$pattern" >"$choices" ||
:
119 no_branch_found
="No topic branch matches grep pattern '$pattern'"
123 [ $# -gt 0 ] ||
set -- "1"
124 [ $# -eq 1 ] || usage
1 "next/previous permits no more than one argument"
127 [ -z "$branch" ] || usage
1 "--branch not allowed in --all mode"
128 navigate_deps
-t -s=-1 ${reverse:+-r} >"$choices"
129 no_branch_found
="No TopGit branches found at all!"
132 [ "$1" = "${1%%[!0-9]*}" ] || usage
1 "invalid next/previous step count"
133 v_verify_topgit_branch branch
"${branch:-HEAD}"
134 navigate_deps
-t -s="$1" ${reverse:+-r} ${pinstep:+-k} "$branch" >"$choices" ||
exit
137 [ -z "$reverse" ] || dir
="previous"
138 no_branch_found
="No $dir TopGit branch(es) found $1 step$pl away"
141 usage
1 "invalid next/previous movement; must be --all or positive number"
146 cnt
=$
(( $
(wc -l <"$choices") ))
147 [ $cnt -gt 0 ] || die
"$no_branch_found"
149 if [ $cnt -eq 1 ]; then
150 read -r choice
<"$choices" ||
:
151 choice
="${choice%%[ $tab]*}"
152 [ -n "$choice" ] || die
"$no_branch_found"
154 echo "Please select one of the following topic branches:"
155 awk -v "desc=$desc" <"$choices" '
156 BEGIN { colcount = 0 }
158 if (colcount) return colcount
159 sizer = "exec stty size 0>&2 2>/dev/null"
164 if (split(info, nums, " ") >= 2 && nums[2] ~ /^[1-9][0-9]*$/)
165 colcount = 0 + nums[2]
166 if (!colcount) colcount = 80
171 mark = substr($0, 1, 2)
172 names = substr($0, 3)
177 cnt = split(names, name, " ")
183 sub(/^[ \t]+/, "", annotation)
184 sub(/^[^ \t]+[ \t]+/, "", annotation)
185 sub(/[ \t]+$/, "", annotation)
186 if (annotation != "") annotation = " " annotation
188 for (i = 2; i <= cnt; ++i) {
189 annotation = annotation ", " name[i]
191 annotation = " [" substr(annotation, 3) "]"
195 line = sprintf("%-39s%s", sprintf("%6d %s%s", NR, mark, name[1]), annotation)
197 line = sprintf("%6d %s%s%s", NR, mark, name[1], annotation)
198 printf "%.*s\n", cols() - 1, line
200 printf '%s' "Input the number: "
202 [ -n "$n" ] && [ "$n" = "${n%%[!0-9]*}" ] || die
"Bad input"
203 [ $n -ge 1 ] && [ $n -le $cnt ] || die
"Input out of range"
204 choice
="$(sed -n ${n}p <"$choices")" ||
:
205 case "$choice" in "* "*|
" "*) choice
="${choice#??}"; esac
206 choice
="${choice%%[ $tab]*}"
207 [ -n "$choice" ] || die
"Bad input"
209 git checkout
$quietval $iowoptval $mergeval $forceval "$choice" --