1---2title: "极简自建个人 git"3date: 2023-05-21T12:54:37+08:004draft: true5---67## 简述8手工创建 git bare repo ,系统 ssh + git-daemon + cgit ,git-hooks 走 systemd-run 做 CI910## 远程访问11### SSH12简单的使用系统本身的 ssh daemo1314如果系统已经带了 git 用户,使用原有的就行了,如果系统不带:1516```17# 新建 git 用户18# 使用 git-shell 作为默认 shell19# 家目录放在 /srv/git20useradd -d /srv/git -m -s /usr/bin/git-shell git21```2223把你的公钥放在 /srv/git/.ssh/authorized_keys 中,如果有必要,也可以给 git 用户初始化密码2425> 这种方式有一个缺点,不能用 `ssh://git.example.tld/repo.git` 的 remote url 访问。26>27> 必须用 `ssh://git.example.tld/~/repo.git` 指明家目录下的 repo 。28>29> 或者 `git@git.example.tld:repo.git`3031### git 协议32安装 `git-daemon` ,如果是 debian ,包名是 `git-daemon-run`33```34apt install git-daemon-run35```3637创建一个 systemd-service 文件3839```40# /etc/systemd/system/git-daemon.service4142[Unit]43Description=Start Git Daemon4445[Service]46ExecStart=/usr/bin/git daemon --reuseaddr --base-path=/srv/git/ /srv/git/4748Restart=always49RestartSec=500ms5051StandardOutput=syslog52StandardError=syslog53SyslogIdentifier=git-daemon5455User=git56Group=git5758[Install]59WantedBy=multi-user.target60```6162```63# 启动64systemctl enable --now git-daemon65```66### Web67我选用的是 cgit ,需要安装 cgit 和 fastcgi68```69# debian70apt-get install -y cgit fcgiwrap71```7273```74# /etc/cgitrc 配置文件7576# cgit config77# see cgitrc(5) for details7879css=/cgit.css80logo=/avatar.jpg81virtual-root=/8283clone-url=git://git.lin.moe/$CGIT_REPO_URL https://git.lin.moe/$CGIT_REPO_URL8485readme=:README86readme=:README.txt87readme=:README.html88readme=:README.htm8990about-filter=/usr/lib/cgit/filters/about-formatting.sh9192repository-sort=age9394enable-index-owner=195enable-http-clone=196enable-blame=197enable-commit-graph=198enable-log-filecount=199enable-log-linecount=1100enable-index-owner=0101enable-index-links=0102branch-sort=age103max-stats=quarter104root-title=Lin's cgit105root-desc=106footer=107# root-readme=108snapshots=tar.gz zip109110enable-git-config=1111112mimetype.gif=image/gif113mimetype.html=text/html114mimetype.jpg=image/jpeg115mimetype.jpeg=image/jpeg116mimetype.pdf=application/pdf117mimetype.png=image/png118mimetype.svg=image/svg+xml119120scan-path=/srv/git121```122123使用 nginx 作为 web 服务器124```125# /etc/nginx/site-enabled/cgit126127server {128 listen 443 http2 ssl;129 listen [::]:443 http2 ssl;130 server_name git.example.tld;131132 ssl_certificate /path/to/certificate;133 ssl_certificate_key /path/to/private_key;134135 root /usr/share/cgit;136137 try_files $uri @cgit;138139 location ~ /.+/(info/refs|git-upload-pack) {140 include fastcgi_params;141 fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;142 fastcgi_param PATH_INFO $uri;143 fastcgi_param GIT_HTTP_EXPORT_ALL 1;144 fastcgi_param GIT_PROJECT_ROOT /srv/git;145 fastcgi_param HOME /srv/git;146 fastcgi_pass unix:/run/fcgiwrap.socket;147 }148149 location @cgit {150 include fastcgi_params;151 fastcgi_param SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;152 fastcgi_param PATH_INFO $uri;153 fastcgi_param QUERY_STRING $args;154 fastcgi_param HTTP_HOST $server_name;155156 # 使 go get [module] 可用157 sub_filter '</head>'158 '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';159 sub_filter_once on;160161 fastcgi_pass unix:/run/fcgiwrap.socket;162 }163}164```165166## git shell command167在 git 用户168169## CI170CI 要极简风,有点麻烦。我用 git server-side hooks + systemd-run + go-task 来管理。