cgit

来自 ArchWiki

cgit 旨在为 git 版本控制系统创建一个快速的 Web 界面,使用内置缓存来减少 git 服务器的压力。

安装

安装 cgit 软件包。

要使用 cgit,必须在系统上安装和配置 web 服务器

Web 服务器配置

Apache

将以下内容添加到 /etc/httpd/conf/httpd.conf 的末尾

ScriptAlias /cgit "/usr/lib/cgit/cgit.cgi/"
Alias /cgit-css "/usr/share/webapps/cgit/"
<Directory "/usr/share/webapps/cgit/">
   AllowOverride None
   Options None
   Require all granted
</Directory>
<Directory "/usr/lib/cgit/">
   AllowOverride None
   Options ExecCGI FollowSymlinks
   Require all granted
</Directory>

或者,也可以将其添加到单独的文件中,例如 etc/httpd/conf/extra/cgit.conf,然后将以下内容添加到 httpd.conf 的末尾

# cgit configuration
Include conf/extra/cgit.conf

这允许您通过 your.server.com/cgit 访问 cgit。确保 Apache 配置为允许 CGI 执行,方法是在 httpd.conf 中取消注释以下内容

<IfModule !mpm_prefork_module>
   LoadModule cgid_module modules/mod_cgid.so
</IfModule>
<IfModule mpm_prefork_module>
   LoadModule cgi_module modules/mod_cgi.so
</IfModule>

然后 重启 httpd.service 以应用这些更改。有关使用 Apache 执行 CGI 的更多详细信息,请参阅 https://httpd.apache.ac.cn/docs/2.4/howto/cgi.html

Lighttpd

以下配置将允许您通过 http://your.server.com/git 或 http://your.server.com/cgit 访问 cgit。cgit url 并不完美(例如,您将在所有仓库的 url 中看到“cgit.cgi”),但可以工作。

创建文件 /etc/lighttpd/conf.d/cgit.conf

server.modules += ( "mod_cgi", "mod_alias" )

$HTTP["url"] =~ "^/cgit" {
    server.document-root = "/usr/share/webapps/"
    server.indexfiles = ("cgit.cgi")
    cgi.assign = ("cgit.cgi" => "")
    mimetype.assign = ( ".css" => "text/css" )
}

alias.url += (
    "/git" => "/usr/share/webapps/cgit/cgit.cgi",
)
$HTTP["url"] =~ "^/git" {
    cgi.assign = ( "" => "" )
}

并将此文件包含在 /etc/lighttpd/lighttpd.conf

include "conf.d/cgit.conf"

然后 重启 lighttpd.service

Lighttpd 子域名

此 Lighttpd 替代配置将在子域名(如 git.example.com)上提供 cgit,并具有可选的 SSL 支持,以及创建美观永久链接的重写规则

server.modules += ( "mod_cgi", "mod_rewrite" )

#$SERVER["socket"] == ":443" {
$SERVER["socket"] == ":80" {
    #ssl.engine                    = "enable"
    #ssl.pemfile                   = "/etc/lighttpd/ssl/git.example.com.pem"

    server.name          = "git.example.com"
    server.document-root = "/usr/share/webapps/cgit/"

    index-file.names     = ( "cgit.cgi" )
    cgi.assign           = ( "cgit.cgi" => "" )
    mimetype.assign      = ( ".css" => "text/css" )
    url.rewrite-once     = (
        "^/cgit/cgit.css"   => "/cgit.css",
        "^/cgit/cgit.png"   => "/cgit.png",
        "^/([^?/]+/[^?]*)?(?:\?(.*))?$"   => "/cgit.cgi?url=$1&$2",
    )
}

Nginx

使用 fcgiwrap

以下配置使用 fcgiwrap,将在子域名(如 git.example.com)上提供 cgit。

启动启用 fcgiwrap.socket。然后,配置 Nginx

/etc/nginx/nginx.conf
worker_processes          1;
 
events {
  worker_connections      1024;
}
 
http {
  include                 mime.types;
  default_type            application/octet-stream;
  sendfile                on;
  keepalive_timeout       65;
  gzip                    on;
 
  # Cgit
  server {
    listen                80;
    server_name           git.example.com;
    root                  /usr/share/webapps/cgit;
    try_files             $uri @cgit;

    # Configure HTTP transport
    location ~ /.+/(info/refs|git-upload-pack) {
        include             fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME     /usr/lib/git-core/git-http-backend;
        fastcgi_param       PATH_INFO           $uri;
        fastcgi_param       GIT_HTTP_EXPORT_ALL 1;
        fastcgi_param       GIT_PROJECT_ROOT    /srv/git;
        fastcgi_param       HOME                /srv/git;
        fastcgi_pass        unix:/run/fcgiwrap.sock;
    }

    location @cgit {
      include             fastcgi_params;
      fastcgi_param       SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;
      fastcgi_param       PATH_INFO       $uri;
      fastcgi_param       QUERY_STRING    $args;
      fastcgi_param       HTTP_HOST       $server_name;
      fastcgi_pass        unix:/run/fcgiwrap.sock;
    }
  }
}

使用 uwsgi

以下示例将使用 uwsgi 的原生 cgi 插件设置 cgit。

首先,安装 uwsgiuwsgi-plugin-cgi

将以下服务器块添加到您的配置中

/etc/nginx/nginx.conf
server {
  listen 80;
  server_name git.example.com;
  root /usr/share/webapps/cgit;

  # Serve static files with nginx
  location ~* ^.+(cgit.(css|png)|favicon.ico|robots.txt) {
    root /usr/share/webapps/cgit;
    expires 30d;
  }
  location / {
    try_files $uri @cgit;
  }
  location @cgit {
    gzip off;
    include uwsgi_params;
    uwsgi_modifier1 9;
    uwsgi_pass unix:/run/uwsgi/cgit.sock;
  }
} 

为 cgit 添加 uwsgi 配置。

/etc/uwsgi/cgit.ini
[uwsgi]
master = true
plugins = cgi
socket = /run/uwsgi/%n.sock
uid = http
gid = http
procname-master = uwsgi cgit
processes = 1
threads = 2
cgi = /usr/lib/cgit/cgit.cgi

启用启动 相应的 socket uwsgi@cgit.socket

Caddy

以下配置使用 fcgiwrap,将在子域名(如 git.example.com)上提供 cgit。

启动启用 fcgiwrap.socket。然后,配置 Caddy

/etc/caddy/conf.d/cgit
git.example.com

@assets path /cgit.css /cgit.js /cgit.png /favicon.ico /robots.txt
handle @assets {
	root * /usr/share/webapps/cgit
	file_server
}

reverse_proxy unix//run/fcgiwrap.sock {
	transport fastcgi {
		env SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi
	}
}

h2o

软件包 h2o-gitAUR 有其自己的 CGI 包装器 fastcgi-cgi,它通过以下配置支持 cgit。

/etc/h2o/h2o.conf
  "git.domain.tld:443":
    listen:
      port: 443
      ssl:
        ...
    paths:
      /cgit.css:
        file.file: /usr/share/webapps/cgit/cgit.css
        file.send-compressed: ON
      /favicon.ico:
        file.file: /usr/share/webapps/cgit/favicon.ico
        file.send-compressed: ON
      /robots.txt:
        file.file: /usr/share/webapps/cgit/robots.txt
      /cgit.png:
        file.file: /usr/share/webapps/cgit/cgit.png
      /:
        fastcgi.spawn: /usr/share/h2o/fastcgi-cgi
        setenv:
          SCRIPT_FILENAME: /usr/lib/cgit/cgit.cgi
        compress: ON

cgit 配置

有关所有配置选项的列表,请参阅 cgitrc(5)

基本配置

在开始添加仓库之前,您首先必须在 /etc/cgitrc 创建基本的 cgit 配置文件。

#
# cgit config
#

# The defaults
#css=/cgit.css
#logo=/cgit.png

# Following lines work with the above Apache config
#css=/cgit-css/cgit.css
#logo=/cgit-css/cgit.png

# Following lines work with the above Lighttpd config
#css=/cgit/cgit.css
#logo=/cgit/cgit.png

# Allow http transport git clone
#enable-http-clone=0


# if you do not want that webcrawler (like google) index your site
robots=noindex, nofollow

# if cgit messes up links, use a virtual-root. For example, cgit.example.org/ has this value:
virtual-root=/

添加仓库

现在您可以添加您的仓库

#
# List of repositories.
# This list could be kept in a different file (e.g. '/etc/cgitrepos')
# and included like this:
#   include=/etc/cgitrepos
#

repo.url=MyRepo
repo.path=/srv/git/MyRepo.git
repo.desc=This is my git repository

# For a non-bare repository (repository with the working tree)
repo.url=MyOtherRepo
repo.path=/srv/git/MyOtherRepo/.git
repo.desc=That's my other git repository

或者,也可以配置 cgit 以自动搜索仓库

scan-path=/srv/git/

如果您使用上述方法,请将描述添加到 .git/description 文件,并添加以下行以显示作者

.git/config
[gitweb]
        owner = John Cena <john@riseup.net>

如果您来自 gitweb 并希望保留描述和所有者信息,请使用

enable-git-config=1

语法高亮

cgit 在查看 blobs 时支持语法高亮。要启用语法高亮,您有多种选择。

使用 python-pygments

安装 python-pygments 并在 /etc/cgitrc 中添加过滤器

source-filter=/usr/lib/cgit/filters/syntax-highlighting.py

要更改着色样式,请修改传递给 syntax-highlighting.py 文件中 HtmlFormatterstyle 参数。例如,要将着色样式更改为“tango”

 formatter = HtmlFormatter(encoding='utf-8', style='tango')

要获取所有可用着色样式的列表,请执行

 $ python
 >>> from pygments.styles import get_all_styles
 >>> list(get_all_styles())
 ['manni', 'igor', 'xcode', 'vim', 'autumn', 'vs', 'rrt', 'native', 'perldoc', 'borland', 'tango', 'emacs', 'friendly', 'monokai', 'paraiso-dark', 'colorful', 'murphy', 'bw', 'pastie', 'paraiso-light', 'trac', 'default', 'fruity']

使用 highlight

安装 highlight 软件包。

/usr/lib/cgit/filters/syntax-highlighting.sh 复制到 /usr/lib/cgit/filters/syntax-highlighting-edited.sh。然后,在复制的文件中,注释掉版本 2 并注释掉版本 3。您可能需要将 --inline-css 添加到 highlight 的选项中,以获得更丰富多彩的输出,而无需编辑 cgit 的 css 文件。

 # This is for version 2
 #exec highlight --force -f -I -X -S "$EXTENSION" 2>/dev/null
 
 # This is for version 3
 exec highlight --force --inline-css -f -I -O xhtml -S "$EXTENSION" 2>/dev/null

/etc/cgitrc 中启用过滤器

source-filter=/usr/lib/cgit/filters/syntax-highlighting-edited.sh
注意: 直接编辑 /usr/lib/cgit/filters/syntax-highlighting.sh 将在 cgit 更新后丢失所有修改。

格式化 about 页面

cgit 可以使用 MarkdownreStructuredTextman page 语法、文本文件和 html 文件显示格式化的 about 页面。

脚本 /usr/lib/cgit/filters/about-formatting.sh/etc/cgitrc 中的 about-filter 或 repo.about-filter 一起使用。例如

about-filter=/usr/lib/cgit/filters/about-formatting.sh
readme=:README.md

about-formatting.sh 的工作方式是根据输入文件的扩展名,在 html-converters 子目录中运行特定的脚本。上述配置允许 Markdown 格式,只要 README.md 位于仓库默认分支的根目录中,并且该脚本被赋予了可执行权限。

为了获得更轻量级的 Markdown 转换器,安装 md4c 并创建一个新的过滤器 /usr/lib/cgit/filters/md2html.sh,它运行 md2html --github

集成

Gitosis

如果要与 gitosis 集成,您将必须运行两个命令来授予 Apache 权限以查看文件夹。

# chgrp http /srv/gitosis
# chmod a+rx /srv/gitosis

Gitolite

如果您添加了由 gitolite 管理的仓库,则必须更改权限,以便 Web 服务器可以访问文件。

  • http 用户添加到 gitolite
    • 以 root 用户身份运行 usermod -aG gitolite http
  • 更改未来仓库的权限
  • 更改 gitolite 主目录和现有仓库的权限。运行以下两个命令
    • 以 root 用户身份运行 chmod g+rX /var/lib/gitolite
    • 以 root 用户身份运行 chmod -R g+rX /var/lib/gitolite/repositories
  • 如果您将 scan-pathproject-list 结合使用,请更改项目列表的权限。运行以下命令
    • 以 root 用户身份运行 chmod g+r /var/lib/gitolite/projects.list

故障排除

以下故障排除子章节主要概述了由 cgit 配置文件中设置顺序引起的问题。为了安全起见,请尝试确保在 scan-path 之前定义所有全局设置。

snapshots 未正确显示

如果您已启用 scan-path 以及 snapshots,则 cgitrc 中的顺序很重要。根据 cgit 邮件列表snapshots 应在 scan-path 之前指定

snapshots=tar.gz tar.bz2 zip
scan-path=/path/to/your/repositories

readme 文件未找到,about 标签未显示

如果您添加了一个或多个 readme 条目,这些条目定义了 cgit 在填充 about 标签时将搜索的文件,并且您确定它们与仓库中的 README 文件匹配,但 about 标签未显示,请确保在 scan-path 之前指定 readme 条目。

source-filter 未正常工作

如果您已启用 scan-path,同样,cgitrc 中的顺序很重要。source-filter 应在 scan-path 之前指定,否则它将不起作用。

参见