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 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
72 usage
1 "Unknown option: $1"
78 [ $# -ne 0 ] ||
[ -n "$dashdash" ] ||
set -- "next" # deprecated "next" alias
79 [ $# -gt 0 ] || usage
1
82 n|next|push|child
) shift;;
83 p|prev|previous|pop|parent|..
) reverse
=1; shift;;
88 choices
="$(get_temp choices)"
91 if [ -n "$dashdash" ]; then
92 [ $# -eq 1 ] || usage
1 "goto mode requires exactly one pattern"
94 [ -n "$pattern" ] || usage
1 "goto mode requires a non-empty pattern"
97 tg
--no-pager info
"$pattern" "${branch:-HEAD}" >"$choices" ||
exit
101 [ -z "$branch" ] || usage
1 "--branch not allowed in goto <pattern> mode"
102 tg
--no-pager summary
--list |
grep -e "$pattern" >"$choices" ||
:
103 no_branch_found
="No topic branch matches grep pattern '$pattern'"
107 [ $# -gt 0 ] ||
set -- "1"
108 [ $# -eq 1 ] || usage
1 "next/previos permits no more than one argument"
111 [ -z "$branch" ] || usage
1 "--branch not allowed in --all mode"
112 navigate_deps
-t -s=-1 ${reverse:+-r} >"$choices"
113 no_branch_found
="No TopGit branches found at all!"
116 [ "$1" = "${1%%[!0-9]*}" ] || usage
1 "invalid next/previous step count"
117 v_verify_topgit_branch branch
"${branch:-HEAD}"
118 navigate_deps
-t -s="$1" ${reverse:+-r} "$branch" >"$choices" ||
exit
120 [ "$steps" != 1 ] || pl
=
121 [ -z "$reverse" ] || dir
="previous"
122 no_branch_found
="No $dir TopGit branch(es) found $steps$pl away"
125 usage
1 "invalid next/previous movement; must be --all or positive number"
130 cnt
=$
(( $
(wc -l <"$choices") ))
131 [ $cnt -gt 0 ] || die
"$no_branch_found"
133 if [ $cnt -eq 1 ]; then
134 read -r choice
<"$choices" ||
:
135 choice
="${choice%%[ $tab]*}"
136 [ -n "$choice" ] || die
"$no_branch_found"
138 echo "Please select one of the following topic branches:"
139 awk -v "desc=$desc" <"$choices" '
140 BEGIN { colcount = 0 }
142 if (colcount) return colcount
143 sizer = "exec stty size 0>&2 2>/dev/null"
148 if (split(info, nums, " ") >= 2 && nums[2] ~ /^[1-9][0-9]*$/)
149 colcount = 0 + nums[2]
150 if (!colcount) colcount = 80
155 mark = substr($0, 1, 2)
156 names = substr($0, 3)
161 cnt = split(names, name, " ")
167 sub(/^[ \t]+/, "", annotation)
168 sub(/^[^ \t]+[ \t]+/, "", annotation)
169 sub(/[ \t]+$/, "", annotation)
170 if (annotation != "") annotation = " " annotation
172 for (i = 2; i <= cnt; ++i) {
173 annotation = annotation ", " name[i]
175 annotation = " [" substr(annotation, 3) "]"
179 line = sprintf("%-39s%s", sprintf("%6d %s%s", NR, mark, name[1]), annotation)
181 line = sprintf("%6d %s%s%s", NR, mark, name[1], annotation)
182 printf "%.*s\n", cols() - 1, line
184 printf '%s' "Input the number: "
186 [ -n "$n" ] && [ "$n" = "${n%%[!0-9]*}" ] || die
"Bad input"
187 [ $n -ge 1 ] && [ $n -le $cnt ] || die
"Input out of range"
188 choice
="$(sed -n ${n}p <"$choices")" ||
:
189 case "$choice" in "* "*|
" "*) choice
="${choice#??}"; esac
190 choice
="${choice%%[ $tab]*}"
191 [ -n "$choice" ] || die
"Bad input"
193 git checkout
$quietval $iowoptval $mergeval $forceval "$choice" --