Skip to content

Instantly share code, notes, and snippets.

@tisseurdetoile
Last active August 29, 2015 14:19
Show Gist options
  • Save tisseurdetoile/fad3b78b359153bd36c9 to your computer and use it in GitHub Desktop.
Save tisseurdetoile/fad3b78b359153bd36c9 to your computer and use it in GitHub Desktop.
git-shell-commands example
# git-shell-commands
#!/bin/bash
userconn=$(whoami)
# If the user is not root
if [ "$userconn" != "gitadmin" ]
then
# Dislpay a notice and stop
echo "Sorry, only gitadmin can use this command."
exit 1
fi
# Read in the SSH key
echo "Input the key to be added:"
read key
# Place the key in a temporary file (it's hard to get ssh-keygen
# to read from stdin; <<< works for bash, but is non-posix)
keyfile=$(tempfile) &&\
echo "$key" > $keyfile
# Generate a fingerprint
fingerprint=$(ssh-keygen -lf $keyfile)
# Check for errors
if [ $(echo "$fingerprint" | egrep -c '(R|D)SA') -eq 0 ]
then
# Display the fingerprint error and clean up
echo "Error: $fingerprint"
rm $keyfile
exit 1
fi
# Add the key to the authorised keys file and clean up
mkdir -p .ssh &&\
echo -n "no-agent-forwarding,no-port-forwarding,no-X11-forwarding " >> .ssh/authorized_keys &&\
cat $keyfile >> .ssh/authorized_keys
rm $keyfile
# Display the fingerprint for reference
echo "Success! Added a key with the following fingerprint:"
echo $fingerprint
#!/bin/sh
# Read in the SSH key
echo "Input the key to be added:"
read key
# Place the key in a temporary file (it's hard to get ssh-keygen
# to read from stdin; <<< works for bash, but is non-posix)
keyfile=$(tempfile) &&\
echo "$key" > $keyfile
# Generate a fingerprint
fingerprint=$(ssh-keygen -lf $keyfile)
# Check for errors
if [ $(echo "$fingerprint" | egrep -c '(R|D)SA') -eq 0 ]
then
# Display the fingerprint error and clean up
echo "Error: $fingerprint"
rm $keyfile
exit 1
fi
# Add the key to the authorised keys file and clean up
mkdir -p .ssh &&\
echo -n "no-agent-forwarding,no-port-forwarding,no-X11-forwarding " >> .ssh/authorized_keys &&\
cat $keyfile >> .ssh/authorized_keys
rm $keyfile
# Display the fingerprint for reference
echo "Success! Added a key with the following fingerprint:"
echo $fingerprint
#!/bin/bash
userconn=$(whoami)
# If the user is not an admin
if [ "$userconn" != "gitadmin" ]
then
# Dislpay a notice and stop
echo "Sorry, only gitadmin can use this command."
exit 1
fi
# If no project name is given
if [ $# -eq 0 ]
then
# Display usage and stop
echo "Usage: create <project.git>"
exit 1
fi
# Set the project name, adding .git if necessary
project=$(echo "$*" | sed 's/\.git$\|$/.git/i')
# Create and initialise the project
mkdir "/opt/git/$project" && \
cd "/opt/git/$project" && \
git --bare init && \
chown -R gitadmin:gitusers "/opt/git/$project" && \
chmod -R 0775 "/opt/git/$project"
#!/bin/sh
print_if_bare_repo='
if "$(git --git-dir="$1" rev-parse --is-bare-repository)" = true
then
printf "%s\n" "${1#./}"
fi
'
find /opt/git/ -type d -name "*.git" -exec sh -c "$print_if_bare_repo" -- \{} \; -prune 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment