Skip to content

Instantly share code, notes, and snippets.

@alaniwi
Created June 26, 2025 11:47
Show Gist options
  • Save alaniwi/77dd980d8189800c440bbb788b277fbd to your computer and use it in GitHub Desktop.
Save alaniwi/77dd980d8189800c440bbb788b277fbd to your computer and use it in GitHub Desktop.
git wrapper for JASMIN sci machines
#!/bin/bash
#============================
# A wrapper for git, for use on JASMIN. In most cases, it will just invoke
# git with the supplied command line arguments, but if you are on a physical
# sci machine and you are trying to talk to a non-http (i.e. ssh) repo URL,
# then it uses ssh to a sci VM in order to run the same command. This is
# because the networking arrangements don't permit this from the sci-ph
# machines.
#============================
script_args=("$@")
needs_remote() {
case ${script_args[0]} in
fetch|pull|push|clone) return 0;;
*) return 1;;
esac
}
is_172_private_network() {
gateway=$(ip route show default | awk '{print $3}')
[[ "$gateway" =~ ^172\.(1[6-9]|2[0-9]|3[0-1])\. ]]
}
is_http_url() {
if [ ${script_args[0]} = clone ]
then
url=${script_args[1]}
else
# LIMITATION: assumes that the remote is origin
# could be improved so that it parses the command args
url=$(git remote get-url origin)
fi
[[ "$url" =~ ^http ]] # includes https
}
get_sci_vm() {
# get hostname of a sci VM that is responding to ping
for host in sci-vm-0{1..5}
do
if ping -c1 -w1 $host > /dev/null
then
echo $host
return
fi
done
echo "no sci vm available" >& 2
exit 1
}
local_dir() {
case $(pwd) in
/tmp*|/var/tmp*) return 0;;
*) return 1;;
esac
}
if ! needs_remote || ! is_172_private_network || is_http_url
then
git "$script_args"
elif local_dir
then
echo "this might hang - consider using https URL" >&2
git "$script_args"
else
vm=$(get_sci_vm)
echo "using $vm" >&2
dir=$(pwd)
ssh -Ax $vm "cd $dir && git $@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment