Skip to content

Instantly share code, notes, and snippets.

@xanderificnl
Created April 22, 2023 00:35
Show Gist options
  • Save xanderificnl/f2f2975f730a5a88b7ada16c58873701 to your computer and use it in GitHub Desktop.
Save xanderificnl/f2f2975f730a5a88b7ada16c58873701 to your computer and use it in GitHub Desktop.
expose a git directory via HTTP
#!/usr/bin/env bash
#= Exposes the git repository under the current working directory over HTTP.
#= ​
#= Usage: $name [OPTION]... [PORT=3456]
#= ​
#= --help displays this text
#= ​
# Change this value if `git-http-backend` exists elsewhere.
HTTP_BACKEND="/usr/libexec/git/git-http-backend"
# On my systems the correct python executable is called python3 YMMV.
PYTHON="python3"
if test "$1" = "--help"
then
grep "^#= " "$0" | sed "s/#= //g; s|\$name|$(basename "$0")|g"
exit
fi
# git-http-backend variables
GIT_PROJECT_ROOT="$(pwd)"
GIT_HTTP_EXPORT_ALL=1
REPO_NAME=$(basename "$GIT_PROJECT_ROOT")
export GIT_PROJECT_ROOT
export GIT_HTTP_EXPORT_ALL
# Git HTTP server
PORT="${1:-3456}"
TMP="$(mktemp -d)"
CGI_BIN="$TMP/cgi-bin"
CGI_BACKEND="$CGI_BIN/repo.cgi"
mkdir "$CGI_BIN" || exit
ln -s "$HTTP_BACKEND" "$CGI_BACKEND" || exit
# Collect some data
echo
echo "Perhaps one of these will work:"
echo
for X in $(ip address | awk '/inet/ {print $2}' | sort -s)
do
CIDR=$(echo "$X" | cut -d/ -f2)
IP=$(echo "$X" | cut -d/ -f1)
test "$CIDR" -gt "32" && IP="[$IP]"
echo " git clone \"http://$IP:$PORT/cgi-bin/repo.cgi\" \"$REPO_NAME\""
done
echo
echo "Ctrl-C when you're done..."
echo
$PYTHON -m http.server --cgi --directory "$TMP" "$PORT" > /dev/null || ( \
echo
echo "!!!! failed to start server !!!!"
echo
)
# Clean up
unlink "$CGI_BACKEND" || exit
rm -r "$CGI_BIN" "$TMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment