diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2018-10-18 18:42:00 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2018-10-19 19:49:55 +0300 |
commit | fd4b8ec3d690fa7341159b0166b382dd43c6c967 (patch) | |
tree | 611f28b71b6e31f2f04b64f7b0db095b8af98d30 /bpkg-rep/utility.bash.in | |
parent | 81a57bde7ad17f6be6b3c604b2c63c623117d0ab (diff) |
Add implementation
Diffstat (limited to 'bpkg-rep/utility.bash.in')
-rw-r--r-- | bpkg-rep/utility.bash.in | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/bpkg-rep/utility.bash.in b/bpkg-rep/utility.bash.in new file mode 100644 index 0000000..bcf25d8 --- /dev/null +++ b/bpkg-rep/utility.bash.in @@ -0,0 +1,87 @@ +# file : bpkg-rep/utility.bash.in +# copyright : Copyright (c) 2014-2018 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +# Utility functions useful for implementing bpkg repository utilities. + +if [ "$bpkg_rep_utility" ]; then + return 0 +else + bpkg_rep_utility=true +fi + +# Diagnostics. +# +function info () { echo "$*" 1>&2; } +function error () { info "$*"; exit 1; } + +# Trace a command line, quoting empty arguments as well as those that contain +# spaces. +# +function trace_cmd () # <cmd> <arg>... +{ + local s="+" + while [ $# -gt 0 ]; do + if [ -z "$1" -o -z "${1##* *}" ]; then + s="$s '$1'" + else + s="$s $1" + fi + + shift + done + + info "$s" +} + +# Trace the current function name and arguments. +# +function trace_func () # <args>... +{ + trace_cmd "${FUNCNAME[1]}" "$@" +} + +# Trace and run a command. +# +function run () # <cmd> <arg>... +{ + trace_cmd "$@" + "$@" +} + +# Return lower-case URL scheme or empty string if the argument doesn't look +# like a URL. +# +function url_scheme () # <url> +{ + sed -n -re 's%^(.*)://.*$%\L\1%p' <<<"$1" +} + +# Check that the git repository properly responds to the probing request +# before the timeout (in seconds). Noop for protocols other than HTTP(S). +# +function check_git_connectivity () # <repo-url> <timeout> +{ + local url="$1" + local tmo="$2" + + local s + s="$(url_scheme "$url")" + + if [ "$s" == "http" -o "$s" == "https" ]; then + local u q + + u="$(sed -n -re 's%^([^?]*).*$%\1%p' <<<"$url")" # Strips query part. + q="$(sed -n -re 's%^[^?]*(.*)$%\1%p' <<<"$url")" # Query part. + + if [ -z "$q" ]; then + u="$u/info/refs?service=git-upload-pack" + else + u="$u/info/refs$q&service=git-upload-pack" + fi + + # Here we limit the time for the whole operation. + # + curl -S -s --max-time "$tmo" "$u" >/dev/null + fi +} |