使用gitolite自建立git server

服务器环境如下:

➜  ~ neofetch
  `.::///+:/-.        --///+//-:``    pi@raspi2b
 `+oooooooooooo:   `+oooooooooooo:    ----------
  /oooo++//ooooo:  ooooo+//+ooooo.    OS: Raspbian GNU/Linux 11 (bullseye) armv
  `+ooooooo:-:oo-  +o+::/ooooooo:     Host: Raspberry Pi 2 Model B Rev 1.1
   `:oooooooo+``    `.oooooooo+-      Kernel: 6.1.21-v7+
     `:++ooo/.        :+ooo+/.`       Uptime: 1 hour, 53 mins
        ...`  `.----.` ``..           Packages: 624 (dpkg)
     .::::-``:::::::::.`-:::-`        Shell: zsh 5.8
    -:::-`   .:::::::-`  `-:::-       Terminal: /dev/pts/1
   `::.  `.--.`  `` `.---.``.::`      CPU: BCM2835 (4) @ 1.000GHz
       .::::::::`  -::::::::` `       Memory: 70MiB / 921MiB
 .::` .:::::::::- `::::::::::``::.
-:::` ::::::::::.  ::::::::::.`:::-
::::  -::::::::.   `-::::::::  ::::
-::-   .-:::-.``....``.-::-.   -::-
 .. ``       .::::::::.     `..`..
   -:::-`   -::::::::::`  .:::::`
   :::::::` -::::::::::` :::::::.
   .:::::::  -::::::::. ::::::::
    `-:::::`   ..--.`   ::::::.
      `...`  `...--..`  `...`
            .::::::::::
             `.-::::-`

服务器添加git用户并设置密码

sudo useradd -m git
➜  ~ sudo passwd git
New password:
Retype new password:
passwd: password updated successfully

本地公钥上传到服务器

$ scp ~/.ssh/id_ed25519.pub pi@raspi2b:
pi@raspi2b's password:
id_ed25519.pub                                100%   96     6.9KB/s   00:00

设置公钥,将文件名改为自定义的用户名

➜  ~ sudo cp id_ed25519.pub /home/git/alair.pub
➜  ~ chown -R git:git /home/git/*
➜  ~ sudo chown -R git:git /home/git/*
➜  ~ ll /home/git
total 8.0K
-rw-r--r-- 1 git git   96 Jun 10 16:11 alair.pub
➜  ~

安装gitolite

➜  ~ su - git
Password:
git@raspi2b:~ $ pwd
/home/git
git@raspi2b:~ $ git clone https://github.com/sitaramc/gitolite
Cloning into 'gitolite'...
remote: Enumerating objects: 9728, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 9728 (delta 5), reused 13 (delta 4), pack-reused 9706
Receiving objects: 100% (9728/9728), 3.04 MiB | 26.00 KiB/s, done.
Resolving deltas: 100% (6031/6031), done.
git@raspi2b:~ $ ./gitolite/src/gitolite setup -pk ./alair.pub
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /home/git/repositories/testing.git/
WARNING: /home/git/.ssh missing; creating a new one
    (this is normal on a brand new install)
WARNING: /home/git/.ssh/authorized_keys missing; creating a new one
    (this is normal on a brand new install)
git@raspi2b:~ $

安装完成后,我们可以看到默认系统建立了两个repo。

git@raspi2b:~/repositories $ ls
gitolite-admin.git  testing.git

我们试着把testing.git pull下来操作下。

$ git clone git@raspi2b:testing.git
Cloning into 'testing'...
warning: You appear to have cloned an empty repository.
alair@op36 MINGW64 /d/gits
$ cd testing/
alair@op36 MINGW64 /d/gits/testing (master)
$ ls
alair@op36 MINGW64 /d/gits/testing (master)
$ touch testfile
alair@op36 MINGW64 /d/gits/testing (master)
$ git add .
alair@op36 MINGW64 /d/gits/testing (master)
$ git commit -m "update"
[master (root-commit) e7ac441] update
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testfile
alair@op36 MINGW64 /d/gits/testing (master)
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 199 bytes | 199.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To raspi2b:testing.git
 * [new branch]      master -> master
alair@op36 MINGW64 /d/gits/testing (master)
$

管理添加新的repo和用户

gitolite通过一个名为gitolite-admin.git来进行管理,我们可以把他pull下来看下

alair@op36 MINGW64 /d/gits
$ git clone git@raspi2b:gitolite-admin.git
Cloning into 'gitolite-admin'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.

$ ls -Rl gitolite-admin/
gitolite-admin/:
total 0
drwxr-xr-x 1 alair 197121 0 Jun 10 23:21 conf/
drwxr-xr-x 1 alair 197121 0 Jun 10 23:21 keydir/

gitolite-admin/conf:
total 1
-rw-r--r-- 1 alair 197121 82 Jun 10 23:21 gitolite.conf

gitolite-admin/keydir:
total 1
-rw-r--r-- 1 alair 197121 97 Jun 10 23:21 alair.pub

从上可以看到,gitolite-admin.git包含两个目录,confkeydir,其中conf保存着gitolite.conf配置文件,而keydir保存着上传的公钥文件。

其中keydir中按自定义名保存着公钥文件,如果需要新增用户就在此目录新增用户公钥文件即可。如下,新增一个名为foo的用户。

cp ~/.ssh/foo.pub gitolite-admin/keydir/foo.pub

gitolite-admin/conf/gitolite.conf文件为用户权限配置,如下:

$ cat gitolite-admin/conf/gitolite.conf
repo gitolite-admin
    RW+     =   alair

repo testing
    RW+     =   @all

以上表示repo gitolite-adminalair用户独有,而repo testing的操作权限为所有用户。

我们需要添加新的REPO时,直接在gitolite.conf中新增一行即可,如下:

repo foo
    RW+     =   foo

然后新的gitolite-admin PUSH到服务器即可完成配置。