Let’s focus on the two most important scripts for our git server, the create and the delete script:
This is the create script, as you can see it’s a bit complex because it will do a few things for me:
- create the bare repository using the argument provided on the command line or asking for a project name
- check before creating the repo, if another repo with the same name exists or if a directory with files in it exists. That way I’ll make sure to only create a repo inside an empty directory.
- add a “custom-hooks” directory that will hold a link to the multimail.py script as well as my deploy.sh script
- create a post-receive script that uses pee from the moreutils project to run multiple scripts at the same hook.
- ensure sane permissions on the whole project directory.
- always assume no (the safest option) when asking the user about the action to take.
So after saving this script as “create” inside the git-shell-commands directory, we’ll give it executable permissions and we can move to the delete script.
# cat create-bare-repo.sh > /var/git/git-shell-commands/create # chown git:git -R /var/git/git-shell-commands # chmod 0755 /var/git/git-shell-commands/create
Let’s see the delete script:
This script is much simpler than the previous one, it’ll accept the name of the project as argument on the command line or will ask for it and will only delete it if it is a proper git repository, otherwise it will just exit with an error code.
I improved the way those scripts recognise a git repository from simply relying on the fact that there’s a HEAD file inside the directory they’re checking, which wasn’t the best option, to using git itself to check if the directory is a bare repository. Much better!!
Since we are here let’s modify the help command to make it show a short description of every available command.
The main thing I added is the support for a command line argument, now I’m able to run it by itself and display the usual output with a list of available commands, or followed by a command name to give a brief explanation like this:
git> help Hi git, Run 'help' for help, 'help <command>' for specific help on a command, run 'exit' to exit. Available commands: clear create delete list git> help create usage: create - create a git bare repository named PROJECT.git this command will setup the repo and send a mail for confirmation. git>
Pretty nice isn’t it?! Now it’s much more user friendly, and to show the description I used awk and cut to parse the comment at the top of every script I have in the
In the next page we’ll see the usual routine I follow when working with this new setup.