tg-annihilate.sh: autostash and support --stash and --no-stash
[topgit/pro.git] / tg-checkout.sh
blobb03dec6c4a7fc3fd4485aaa92ebc1f338ed8f435
1 #!/bin/sh
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>
5 # All rights reserved.
6 # GPLv2
8 ## Parse options
10 USAGE="\
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>]"
15 usage()
17 [ -z "$2" ] || printf '%s\n' "$2" >&2
18 if [ "${1:-0}" != 0 ]; then
19 printf '%s\n' "$USAGE" >&2
20 else
21 printf '%s\n' "$USAGE"
23 exit ${1:-0}
26 # Parse options
28 branch= # -b value
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
36 -h|--help)
37 usage
39 -f|--force)
40 forceval="-f"
42 -m|--merge)
43 mergeval="-m"
45 -q|--quiet)
46 quietval="-q"
48 --ignore-other-worktrees|--iow)
49 iowoptval="$iowopt"
51 --branch=*)
52 branch="${1#--branch=}"
53 [ -n "$branch" ] || usage 1 "--branch= requires a branch name"
55 --branch|-b)
56 [ $# -ge 2 ] || usage 1 "$1 requires an argument"
57 branch="$2"
58 [ -n "$branch" ] || usage 1 "$1 requires a branch name"
59 shift
61 --series|--series=*)
62 dashdash=1
63 break
65 --|goto)
66 [ "$1" != "goto" ] || [ "$2" != "--" ] || shift
67 shift
68 dashdash=1
69 break
71 -?*)
72 usage 1 "Unknown option: $1"
75 break
77 esac; shift; done
78 [ $# -ne 0 ] || [ -n "$dashdash" ] || set -- "next" # deprecated "next" alias
79 [ $# -gt 0 ] || usage 1
80 [ -n "$dashdash" ] ||
81 case "$1" in
82 n|next|push|child) shift;;
83 p|prev|previous|pop|parent|..) reverse=1; shift;;
84 -*) usage 1;;
85 *) dashdash=1;;
86 esac
88 choices="$(get_temp choices)"
89 desc=
91 if [ -n "$dashdash" ]; then
92 [ $# -eq 1 ] || usage 1 "goto mode requires exactly one pattern"
93 pattern="$1"
94 [ -n "$pattern" ] || usage 1 "goto mode requires a non-empty pattern"
95 case "$pattern" in
96 --series|--series=*)
97 tg --no-pager info "$pattern" "${branch:-HEAD}" >"$choices" || exit
98 desc=1
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'"
105 esac
106 else
107 [ $# -gt 0 ] || set -- "1"
108 [ $# -eq 1 ] || usage 1 "next/previos permits no more than one argument"
109 case "$1" in
110 -a|--all)
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!"
115 [1-9]*)
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
119 pl="s" dir="next"
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"
127 esac
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"
137 else
138 echo "Please select one of the following topic branches:"
139 awk -v "desc=$desc" <"$choices" '
140 BEGIN { colcount = 0 }
141 function cols() {
142 if (colcount) return colcount
143 sizer = "exec stty size 0>&2 2>/dev/null"
144 info = ""
145 sizer | getline info
146 close sizer
147 colcount = 0
148 if (split(info, nums, " ") >= 2 && nums[2] ~ /^[1-9][0-9]*$/)
149 colcount = 0 + nums[2]
150 if (!colcount) colcount = 80
151 return colcount
154 if ($0 ~ /^[* ] /) {
155 mark = substr($0, 1, 2)
156 names = substr($0, 3)
157 } else {
158 mark = ""
159 names = $0
161 cnt = split(names, name, " ")
162 if (!cnt) next
163 annotation = ""
164 if (cnt > 1) {
165 if (desc) {
166 annotation = names
167 sub(/^[ \t]+/, "", annotation)
168 sub(/^[^ \t]+[ \t]+/, "", annotation)
169 sub(/[ \t]+$/, "", annotation)
170 if (annotation != "") annotation = " " annotation
171 } else {
172 for (i = 2; i <= cnt; ++i) {
173 annotation = annotation ", " name[i]
175 annotation = " [" substr(annotation, 3) "]"
178 if (desc)
179 line = sprintf("%-39s%s", sprintf("%6d %s%s", NR, mark, name[1]), annotation)
180 else
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: "
185 read -r n
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" --