From: Nicolas Boisselier Date: Tue, 6 Sep 2016 14:57:40 +0000 (+0100) Subject: functions X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=c52a7f57dc2c188bc5c5f9c13fb34e4ed72483c0;p=nb.git functions --- diff --git a/bin/nb-update b/bin/nb-update index 870225f9..271aa4e2 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -16,56 +16,9 @@ declare CLEANUP=0 ##################################################################### # Git Connection check # -url2nc() { - perl -e ' -for (@ARGV) { -@_ = m,://([^/:]+)(?::(\d+))?, and print join(" ",$_[0],$_[1] ? $_[1] : (/^https/?443:80))."\n", and next; -@_ = m,^(?:[^@]+@)?([^:/]+):[^/].*?(?:\.git)?$, and print "$1 22\n" and next; -@_ = m,([^@:/]+):[^/].*?(?:\.git)?$, and print "$1 22\n" and next; -} -' $@ - return 0 -} #[ "$1" = "TEST" ] && url2nc nico@zaza.com:re.git && exit #[ "$1" = "TEST" ] && url2nc zaza.com:re.git && exit #[ "$1" = "TEST" ] && url2nc https://zaza.com/re.git && exit -_git_conn_check=" " -git_conn_check() { - #local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//') - local dest=$(git config --local --get remote.origin.url) - local port='' - - if [ -z "$dest" ]; then - logger -t $NAME -s "Can't get url from \`git config --het remote.origin.url\` in `pwd`" - return 1 - fi - #[ "$1" = "TEST" ] && - dest=$(url2nc "$dest") - #dest="$dest 22" - - local ok - case "$_git_conn_check" in - *" $dest=0 "*) ok=0 ;; - *" $dest=1 "*) ok=1 ;; - *) - ok=0 - case "$dest" in - *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;; - *) nc -z -w 5 $dest && ok=1 ;; - esac -#echo "$ok: git=$(git config --local --get remote.origin.url) | dest=$dest | url2nc="$(url2nc $(git config --local --get remote.origin.url)) - _git_conn_check="${_git_conn_check}$dest=$ok " - ;; - esac - - #if ! timeout 5 ssh BatchMode=yes -o ConnectTimeout=5 $dest true; then - if [ "$ok" != "1" ]; then - logger -t $NAME -s "Can't connect to $dest" - return 1 - fi - - return 0 -} #cd $NB_ROOT || exit #git_conn_check || git_conn_check || exit #exec echo BYE diff --git a/etc/profile.d/functions b/etc/profile.d/functions index aaf5ac82..d11f3e03 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -576,3 +576,9 @@ is_prompt() { [[ $- == *i* ]] && return 0 return 1 } + +url2nc() { + local in="cat"; + [ -n "$@" ] && in="echo $@"; + $in | perl -MURI::Split -ne 'BEGIN{%port=("ssh"=>22,"https"=>443,"http"=>80);}; chomp($_); m,^\w+://, or $_="ssh://$_"; s,:([a-zAZ]),/$1,; @_=URI::Split::uri_split($_) or next; $_=$_[1]; s/^[^@]+@//; s/:/ / or $_.=" ".$port{$_[0]||"ssh"}; print "$_\n"' +} diff --git a/etc/profile.d/git.sh b/etc/profile.d/git.sh index bfe51395..f6f8f162 100644 --- a/etc/profile.d/git.sh +++ b/etc/profile.d/git.sh @@ -1,7 +1,7 @@ #-------------------------------------------------------------------------------- # Git # -GIT_REPO='git@git.nbdom.net' +[ -z "GIT_REPO" ] && GIT_REPO='git@git.nbdom.net' alias git_grep="git grep --color=auto" alias git_ls_tree="git ls-tree --full-tree -r HEAD" @@ -121,3 +121,41 @@ git_list() { local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;') ssh $repo list | perl -ne '/(\S+).git/ and print "$1\n"' } + +_git_conn_check=" " +git_conn_check() { + #local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//') + local dest=$(git config --local --get remote.origin.url) + local port='' + + if [ -z "$dest" ]; then + logger -t $NAME -s "Can't get url from \`git config --het remote.origin.url\` in `pwd`" + return 1 + fi + #[ "$1" = "TEST" ] && + dest=$(url2nc "$dest") + #dest="$dest 22" + + local ok + case "$_git_conn_check" in + *" $dest=0 "*) ok=0 ;; + *" $dest=1 "*) ok=1 ;; + *) + ok=0 + case "$dest" in + *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;; + *) nc -z -w 5 $dest && ok=1 ;; + esac +#echo "$ok: git=$(git config --local --get remote.origin.url) | dest=$dest | url2nc="$(url2nc $(git config --local --get remote.origin.url)) + _git_conn_check="${_git_conn_check}$dest=$ok " + ;; + esac + + #if ! timeout 5 ssh BatchMode=yes -o ConnectTimeout=5 $dest true; then + if [ "$ok" != "1" ]; then + logger -t $NAME -s "Can't connect to $dest" + return 1 + fi + + return 0 +}