forked from mackyle/topgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg-shell.sh
executable file
·96 lines (90 loc) · 2.03 KB
/
tg-shell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
# TopGit wayback machine shell command
# (C) 2017 Kyle J. McKay <[email protected]>
# All rights reserved
# GPLv2
USAGE="\
Usage: ${tgname:-tg} [...] -w [:]<tgtag> shell [--directory=<dirpath>] [-q] [--] [<arg>...]"
usage()
{
if [ "${1:-0}" != 0 ]; then
printf '%s\n' "$USAGE" >&2
else
printf '%s\n' "$USAGE"
fi
exit ${1:-0}
}
quote=
while [ $# -gt 0 ]; do case "$1" in
-h|--help|--directory|--directory=*)
usage
;;
--directory|--directory=*)
# only one is allowed and it should have been handled by tg.sh
# which means this is the second one and it's a usage error
usage 1
;;
--quote|-q)
quote=1
;;
--)
shift
break
;;
-?*)
echo "Unknown option: $1" >&2
usage 1
;;
*)
break
;;
esac; shift; done
[ -n "$wayback" ] || usage 1
# Everything's been handled by tg.sh except for verifying a TTY
# is present for an interactive shell
cmd=
redir=
theshell=@SHELL_PATH@
if [ $# -eq 0 ]; then
[ -z "$SHELL" ] || theshell="$SHELL"
test -t 0 || die "cannot use interactive wayback shell on non-TTY STDIN"
if test -t 1; then
test -t 2 || redir='2>&1'
elif test -t 2; then
redir='>&2'
else
die "cannot use interactive wayback shell on non-TTY STDOUT/STDERR"
fi
PS1='[../${PWD##*/}] wayback$ ' && export PS1
wbname="${wayback#:}"
[ -n "$wbname" ] || wbname='now?!'
eval 'info "going wayback to $wbname..."' "$redir"
else
if [ -z "$quote" ]; then
cmd='-c "$*"'
else
# attempt to "quote" the arguments and then glue them together
cmdstr=
for cmdword in "$@"; do
cmdworddq=1
case "$cmdword" in [A-Za-z_]*)
if test z"${cmdword%%[!A-Za-z_0-9]*}" = z"$cmdword"
then
cmdworddq=
else case "$cmdword" in *=*)
cmdvar="${cmdword%%=*}"
if test z"${cmdvar%%[!A-Za-z_0-9]*}" = z"$cmdvar"
then
v_quotearg cmdword "${cmdword#*=}"
cmdword="$cmdvar=$cmdword"
cmdworddq=
fi
esac; fi
esac
test z"$cmdworddq" = z || v_quotearg cmdword "$cmdword"
cmdstr="${cmdstr:+$cmdstr }$cmdword"
done
cmd='-c "$cmdstr"'
fi
fi
eval '"$theshell"' "$cmd" "$redir"