stagit.md (2445B)
1 # Static git server 2 3 Stagit allows us to display git repositories running on a previously installed git server. 4 5 We give the user git the home: `/srv/git` and store the repos as `/srv/git/repos/my_repo`. 6 The web server has its root at `/var/www/git/`. 7 8 ## Helper-script for executing stagit and stagit-index 9 10 First, we create a helper script that scans the repos/folders inside `/srv/git/repos` and generates a website *if a file* `git-daemon-export-ok` *exists*. 11 Finally, all these public repos are listed in `/var/www/git/index.html`. 12 13 Create `/usr/local/bin/update-stagit.sh` with 14 15 ```bash 16 #!/bin/sh 17 18 set -eu 19 20 REPO_ROOT="/srv/git/repos" 21 HTML_ROOT="/var/www/git" 22 23 mkdir -p "$HTML_ROOT" 24 25 INDEX_LIST="$(mktemp)" # creates some temporary file to list repos 26 27 # For each repo with a git-daemon-export-ok file, we run stagit 28 for REPO_PATH in "$REPO_ROOT"/*.git; do 29 30 REPO_NAME="$(basename "$REPO_PATH" .git)" 31 REPO_HTML="$HTML_ROOT/$REPO_NAME" 32 33 if [ -f "$REPO_PATH/git-daemon-export-ok" ]; then 34 echo "Generating HTML for $REPO_NAME..." 35 mkdir -p "$REPO_HTML" 36 cd "$REPO_HTML" 37 stagit "$REPO_PATH" 38 echo "$REPO_PATH" >> "$INDEX_LIST" 39 40 else 41 # Delete the folder if it was created earlier 42 rm -rf "$REPO_HTML" 43 fi 44 45 done 46 47 # All such repos are then included in the index.html 48 if [ -s "$INDEX_LIST" ]; then 49 echo "Regenerating git index..." 50 cd "$HTML_ROOT" 51 stagit-index $(cat "$INDEX_LIST") > index.html 52 else 53 echo "No repos with git-daemon-export-ok file found" 54 rm -f "$HTML_ROOT/index.html" 55 fi 56 57 rm -f "$INDEX_LIST" 58 echo "done" 59 ``` 60 61 and make it executable: 62 63 ```console 64 # chmod +x /usr/local/bin/update-git.sh 65 ``` 66 67 ## Post-receive hook 68 69 We now set up a post-receive hook that creates the folder structure for new repos. 70 71 Inside `/srv/git/template/hooks/post-receive`: 72 73 ```bash 74 #!/bin/sh 75 /usr/local/bin/update-stagit.sh 76 ``` 77 78 Make it executable as well with `chmod +x ...`. 79 80 ## Set in default template 81 82 Instruct git to use it as a template: 83 84 ```console 85 # git config --system init.templateDir /srv/git/template 86 ``` 87 88 Finally, we give all users reading rights to all subfolders of the repo folders: 89 90 ```console 91 # chmod -R a+rX /srv/git/repos 92 # chmod -R a+rX /var/www/git 93 ``` 94 95 ## Usage 96 97 New repos can now be initiated on the server with 98 99 ```console 100 $ git init --bare my_repo.git 101 ``` 102 103 The website and the repo endpoint will be updated upon the first push (and future ones). 104 Make sure to create a `git-daemon-export-ok` file to make the repo public.