forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp-svn-url
executable file
·87 lines (73 loc) · 2.21 KB
/
cp-svn-url
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
#!/bin/bash
# @Function
# copy the svn remote url of current svn directory.
#
# @Usage
# $ ./cp-svn-url
# $ ./cp-svn-url /path/to/svn/work/dir
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/vcs.md#-cp-svn-url
# @author ivanzhangwb (ivanzhangwb at gmail dot com)
#
# NOTE about Bash Traps and Pitfalls:
#
# 1. DO NOT combine var declaration and assignment which value supplied by subshell!
# for example: readonly var1=$(echo value1)
# local var1=$(echo value1)
#
# declaration make exit code of assignment to be always 0,
# aka. the exit code of command in subshell is discarded.
# tested on bash 3.2.57/4.2.46
# NOTE: DO NOT declare var PROG as readonly, because its value is supplied by subshell.
PROG="$(basename "$0")"
readonly PROG_VERSION='2.5.0-dev'
usage() {
cat <<EOF
Usage: ${PROG} [DIR]
Copy the svn remote url of local svn directory
DIR is local svn directory, default is current directory.
Example:
${PROG}
${PROG} /path/to/svn/work/dir
Options:
-h, --help display this help and exit
-V, --version display version information and exit
EOF
# shellcheck disable=SC2086
exit $1
}
progVersion() {
echo "$PROG $PROG_VERSION"
exit
}
################################################################################
# parse options
################################################################################
for a; do
[[ "-h" == "$a" || "--help" == "$a" ]] && usage
done
for a; do
[[ "-V" == "$a" || "--version" == "$a" ]] && progVersion
done
################################################################################
# biz logic
################################################################################
[ $# -gt 1 ] && { echo At most 1 local directory is need! ; usage 1; }
readonly dir="${1:-.}"
# NOTE: DO NOT declare var url as readonly, because its value is supplied by subshell.
url="$(svn info "${dir}" | awk '/^URL: /{print $2}')"
if [ -z "${url}" ]; then
echo "Fail to get svn url!" 1>&2
exit 1
fi
copy() {
case "$(uname)" in
Darwin*)
pbcopy ;;
CYGWIN*|MINGW*)
clip ;;
*)
xsel -b ;;
esac
}
echo -n "${url}" | copy && echo "${url} copied!"