Gitweb

出自 ArchWiki

Gitweb 是 Git 自带的默认 Web 界面。

Gitweb 实际上原生支持 FCGI,因此您无需将其包装为 CGI 脚本。[1]

安装

要安装 Gitweb,您首先必须安装 git 和一个 web 服务器。现在,如果您想快速测试它,请参阅 git instaweb 的帮助。否则,如果您想要全面的设置,请继续阅读。

对于以下所有示例,您需要 安装 perl-cgi 软件包。(FS#45431

Web 服务器配置

Apache

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

Alias /gitweb "/usr/share/gitweb"
<Directory "/usr/share/gitweb">
   DirectoryIndex gitweb.cgi
   Options ExecCGI
   Require all granted
   <Files gitweb.cgi>
   SetHandler cgi-script
   </Files>
   SetEnv  GITWEB_CONFIG  /etc/gitweb.conf
</Directory>

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

# gitweb configuration
Include conf/extra/gitweb.conf

如果 Apache 拒绝显示 Gitweb,而是打印 perl 脚本的纯源代码,则很可能是 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

将以下内容添加到 /etc/lighttpd/lighttpd.conf

server.modules += ( "mod_alias", "mod_cgi", "mod_redirect", "mod_setenv" )
url.redirect += ( "^/gitweb$" => "/gitweb/" )
alias.url += ( "/gitweb/" => "/usr/share/gitweb/" )
$HTTP["url"] =~ "^/gitweb/" {
       setenv.add-environment = (
               "GITWEB_CONFIG" => "/etc/gitweb.conf",
               "PATH" => env.PATH
       )
       cgi.assign = ( ".cgi" => "" )
       server.indexfiles = ( "gitweb.cgi" )
}

然后重启 lighttpd.service 以应用这些更改。您可能还需要将 ".css" => "text/css" 添加到 mimetype.assign 行,以便 GitWeb 正确显示。

Nginx

将此位置附加到您的 nginx 配置(您可能需要更改位置)

/etc/nginx/nginx.conf
location /gitweb.cgi {
    include fastcgi_params;
    gzip off;
    fastcgi_param   SCRIPT_FILENAME  /usr/share/gitweb/gitweb.cgi;
    fastcgi_param   GITWEB_CONFIG  /etc/gitweb.conf;
    fastcgi_pass    unix:/run/fcgiwrap.sock;
}

location / {
    root /usr/share/gitweb;
    index gitweb.cgi;
}

如果您遵循 Nginx#CGI 实现,请尝试将 include fastcgi_params; 替换为 include fastcgi.conf;

最后,安装 fcgiwrap启动/启用 fcgiwrap.socket

Gitweb 配置

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

基本配置

打开(或创建,如果不存在)文件 /etc/gitweb.conf 并将以下内容放入其中

/etc/gitweb.conf
# The directories where your projects are. Must not end with a slash.
our $projectroot = "/path/to/your/repositories"; 

# Base URLs for links displayed in the web interface.
our @git_base_url_list = qw(git://<your_server> http://git@<your_server>);

要启用“blame”视图(显示源代码文件中每一行的作者),请添加以下行

$feature{'blame'}{'default'} = [1];

现在配置已完成,重启您的 Web 服务器。

添加仓库

要添加仓库,请转到您的仓库文件夹,像这样创建您的仓库

$ mkdir my_repository.git
$ git init --bare my_repository.git/
$ cd my_repository.git/
$ touch git-daemon-export-ok
$ echo "Short project's description" > description

接下来,打开 config 文件并添加此内容

config
[gitweb]
owner = Your Name

这将在 Gitweb 中填写“所有者”字段。这不是必需的。

这假设您希望将此仓库作为“中央”仓库存储,您将提交推送到该存储,因此 git-daemon-export-ok 和 --bare 在这里是为了尽量减少开销并允许在其上使用 git 守护程序。

这就是创建仓库的全部内容。您现在可以在 https://127.0.0.1/gitweb 上看到它(假设一切顺利)。您无需为新仓库重启 Apache,因为 Gitweb CGI 脚本只是读取您的仓库文件夹。

语法高亮

要启用 Gitweb 的语法高亮,您必须首先安装 highlight 软件包

安装 highlight 后,只需将此行添加到您的 gitweb.conf

$feature{'highlight'}{'default'} = [1];

保存文件,现在应该启用高亮显示。

参见