From 4a890dad70bed4fa158f0607ee44fa5f02502b9c Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 5 Nov 2020 19:31:38 +0100 Subject: Added chat to utils list --- blog/git_server.html | 78 ++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'blog/git_server.html') diff --git a/blog/git_server.html b/blog/git_server.html index 010e6de..5a9423c 100644 --- a/blog/git_server.html +++ b/blog/git_server.html @@ -14,21 +14,21 @@

How to make your own git server/website

Basic ssh server

Every repository on the server will be owned by a git user.

-
useradd -m git
+
useradd -m git

Create a new directory to store the repositories owned by the git user.

-
mkdir /srv/git
-chown git:git /srv/git
+
mkdir /srv/git
+chown git:git /srv/git

Login as the git user so the new repositories will be owned by him.

-
su git
-cd /srv/git
+
su git
+cd /srv/git

Creating a repository

They will be stored as bare, meaning we will only store the .git folder not the actual files (called the workspace) to save space.
It’s a convention to to suffix a bare repository with the .git extension.

-
mkdir repo.git
-cd repo.git
-git init --bare
+
mkdir repo.git
+cd repo.git
+git init --bare

Or clone a distant one:

-
git clone --bare <location>
+
git clone --bare <location>

Look at the content of a bare repository and the .git directory in a regular one to convince yourself that they’re the same.

@@ -38,26 +38,26 @@ It’s a convention to to suffix a bare repository with the .git ex Follow the steps and it will create id_rsa (private key) and id_rsa.pub (public key) in ~/.ssh.
On your server you append your public key to /home/git/.ssh/authorized_keys

At this point you should be able to login as the git user via ssh

-
ssh git@<host>
+
ssh git@<host>

You can clone from your server.

-
git clone git@<hostname>:/srv/git/<reponame>.git
+
git clone git@<hostname>:/srv/git/<reponame>.git

Better server interaction with git-shell

Permitting the git user to have a regular shell can be too permissive, we would like to restrict him to a few repository actions, like creation/deletion, importing (clone), listing.

-
echo $(which git-shell) >> /etc/shells`  # Register the git-shell as a valid shell
-chsh -s $(which git-shell) git           # Change the shell of the git user
+
echo $(which git-shell) >> /etc/shells`  # Register the git-shell as a valid shell
+chsh -s $(which git-shell) git           # Change the shell of the git user

If you try to ssh as the git user, you will be greeted with something along the line of:

fatal: Interactive git shell is not enabled.
 hint: ~/git-shell-commands should exist and have read and execute access.
 Connection to <host> closed.

As suggested by the hint we have to create the directory /home/git/git-shell-commands and put the commands (executable) available to the git user.

-
#!/bin/sh
-[ $# -ne 1 ] &&
-    echo "Usage: $0 repository" && exit 1
-repo_path="/srv/git/$1.git"
-[ -d "$repo_path" ] &&
-    echo "$0: Error: $repo_path already exist" && exit 2
-mkdir "$repo_path"
-git -C "$repo_path" init --bare
+
#!/bin/sh
+[ $# -ne 1 ] &&
+    echo "Usage: $0 repository" && exit 1
+repo_path="/srv/git/$1.git"
+[ -d "$repo_path" ] &&
+    echo "$0: Error: $repo_path already exist" && exit 2
+mkdir "$repo_path"
+git -C "$repo_path" init --bare

This script create a new repository in /srv/git.
Put it under git-shell-commands/create and make it executable then try to ssh as the git user once again.
You will be prompted with git>, you can only execute the create <repository> and exit command.

@@ -68,7 +68,7 @@ If you add a help script, it will be ran at the beginning of the co

Allow anyone to clone with git-daemon

Cloning with ssh is fine but only the people with ssh access can do it, we would like anyone to clone.
git-daemon does precisely that, after running it you will be able to run git clone git://<host>/<repository>

-
git daemon --reuseaddr --base-path=/srv/git/ /srv/git/
+
git daemon --reuseaddr --base-path=/srv/git/ /srv/git/

Follow the instruction of this tutorial if you want to know how to make it a service

Public/private repository

You may want to introduce a public/private distinction for your repositories.
@@ -118,24 +118,24 @@ make install

Git hooks are scripts located in <repository>/.git/hooks that will be run on a certain action.
The hook we’re interested in is post-receive, it will be ran after someone pushes to the repository.
We can use it to regenerate the repository’s pages and the website’s index.

-
#!/bin/sh
-
-# Insert repo_name variable here
-# <REPO_NAME> -- replace with repo_name=name
-
-[ -z "$repo_name" ] && exit 1
-[ ! -d "/srv/git/public/$repo_name.git" ] && exit
-
-repo_web_path="/var/www/git/$repo_name"
-mkdir -p "$repo_web_path"
-cd "$repo_web_path" || exit 1
-stagit "/srv/git/$repo_name.git"
-stagit-index /srv/git/public/* > /var/www/git/index.html
+
#!/bin/sh
+
+# Insert repo_name variable here
+# <REPO_NAME> -- replace with repo_name=name
+
+[ -z "$repo_name" ] && exit 1
+[ ! -d "/srv/git/public/$repo_name.git" ] && exit
+
+repo_web_path="/var/www/git/$repo_name"
+mkdir -p "$repo_web_path"
+cd "$repo_web_path" || exit 1
+stagit "/srv/git/$repo_name.git"
+stagit-index /srv/git/public/* > /var/www/git/index.html

This is a template for the post-receive hook. Every time you publish a repository you can change his post-receive hook.

-
post_receive_path="<repository>/hooks/post-receive"
-sed '/REPO_NAME/ c repo_name='"$repo" < post-receive.template > "$post_receive_path"
-chmod +x "$post_receive_path"
-"$post_receive_path"
+
post_receive_path="<repository>/hooks/post-receive"
+sed '/REPO_NAME/ c repo_name='"$repo" < post-receive.template > "$post_receive_path"
+chmod +x "$post_receive_path"
+"$post_receive_path"

Add this code to your publish script

-- cgit