Skip to content

Commit 0470a0c

Browse files
author
Mark Stosberg
committed
Add some explicit documentation on how gitreceive works
For people intending to maintain this in production, it's important to understand the details of how this works in case something goes wrong. To that end, I've added some explicit comments in the source code, and explained the key role that SSH "forced command" feature plays in the README.md file.
1 parent a4e72c2 commit 0470a0c

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ The repo contents are streamed into `STDIN` as an uncompressed archive (tar file
5858

5959
#### Create a user by uploading a public key from your laptop
6060

61-
We just pipe it into the `gitreceive upload-key` command via SSH:
61+
We just pipe our local SSH key into the `gitreceive upload-key` command via SSH:
6262

6363
$ cat ~/.ssh/id_rsa.pub | ssh you@yourserver.com "sudo gitreceive upload-key <username>"
6464

6565
The `username` argument is just an arbitrary name associated with the key, mostly
6666
for use in your system for auth, etc.
6767

68+
`gitreceive upload-key` will authorize this key for use on the `$GITUSER`
69+
account on the server, and use the SSH "forced commands" syntax in the remote
70+
`.ssh/authorized_keys` file, causing the internal `gitreceive run` command to
71+
be called when this key is used with the remote git account. This allows us to
72+
intercept the `git` requests and set up a `pre-receive` hook to run on the
73+
repo, which triggers the custom receiver script.
74+
6875
#### Add a remote to a local repository
6976

7077
$ git remote add demo git@yourserver.com:example.git

‎gitreceive‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ EOF
2929
echo "Created receiver script in $GITHOME for user '$GITUSER'."
3030
;;
3131

32-
upload-key) # gitreceive upload-key <username>
32+
upload-key) # sudo gitreceive upload-key <username>
3333
KEY=$(cat)
3434
FINGERPRINT=$(ssh-keygen -lf /dev/stdin <<< $(echo $KEY) | awk '{print $2}')
3535
AUTHORIZED_KEYS=$GITHOME/.ssh/authorized_keys
36+
# When this key is used, use the ssh 'forced command' feature to have 'gitreceive run' to run instead.
3637
KEY_PREFIX="command=\"$SELF run $2 $FINGERPRINT\",no-agent-forwarding,no-pty,no-user-rc,no-X11-forwarding,no-port-forwarding"
3738
echo "$KEY_PREFIX $KEY" >> $AUTHORIZED_KEYS
3839
echo $FINGERPRINT
@@ -42,6 +43,7 @@ EOF
4243
run)
4344
export RECEIVE_USER=$2
4445
export RECEIVE_FINGERPRINT=$3
46+
# ssh provides the original requested command in $SSH_ORIGINAL_COMMAND
4547
eval $(echo $SSH_ORIGINAL_COMMAND | awk '{print "export RECEIVE_REPO="$2}')
4648
REPO_PATH="$GITHOME/$RECEIVE_REPO"
4749
if [ ! -d $REPO_PATH ]; then

0 commit comments

Comments
 (0)