Pro Git - Pro Git 4.6 服务器上的 Git 网页界面 GitWeb
From Pro Git
Pro Git
professional version control
To help translate the book, please fork the book at GitHub and push your contributions.
网页界面 GitWeb
如今我们的项目已经有了读写和只读的连接方式,也许应该再架设一个简单的网页界面使其更加可视化。为此,Git 自带了一个叫做 GitWeb 的 CGI 脚本。你可以在类似 http://git.kernel.org 这样的站点找到 GitWeb 的应用实例(见图 4-1)。

Figure 4-1. 基于网页的 GitWeb 用户界面
如果想知道项目的 GitWeb 长什么样,Git 自带了一个命令,可以在类似 lighttpd 或 webrick 这样轻量级的服务器程序上打开一个临时的实例。在 Linux 主机上通常都安装了 lighttpd ,这时就可以在项目目录里输入 git instaweb 来运行它。如果使用的是 Mac ,Leopard 预装了 Ruby,所以 webrick 应该是最好的选择。使用 lighttpd 以外的程序来启用 git instaweb, 可以通过它的 --httpd 选项来实现。
$ git instaweb --httpd=webrick
[2009-02-21 10:02:21] INFO WEBrick 1.3.1
[2009-02-21 10:02:21] INFO ruby 1.8.6 (2008-03-03) [universal-darwin9.0]
这会在 1234 端口开启一个 HTTPD 服务,随之在浏览器中显示该页。简单的很。需要关闭服务的时候,只要使用相同命令的 --stop 选项就好了:
$ git instaweb --httpd=webrick --stop
如果需要为团队或者某个开源项目长期的运行 web 界面,那么 CGI 脚本就要由正常的网页服务来运行。一些 Linux 发行版可以通过 apt 或 yum 安装一个叫做 gitweb 的软件包,不妨首先尝试一下。我们将快速的介绍一下手动安装 GitWeb 的流程。首先,你需要 Git 的源码,其中带有 GitWeb,并能生成 CGI 脚本:
$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git/
$ make GITWEB_PROJECTROOT="/opt/git" \
prefix=/usr gitweb/gitweb.cgi
$ sudo cp -Rf gitweb /var/www/
注意通过指定 GITWEB_PROJECTROOT 变量告诉编译命令 Git 仓库的位置。然后,让 Apache 来提供脚本的 CGI,为此添加一个 VirtualHost:
<VirtualHost *:80>
ServerName gitserver
DocumentRoot /var/www/gitweb
<Directory /var/www/gitweb>
Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi
</Directory>
</VirtualHost>
不难想象,GitWeb 可以使用任何兼容 CGI 的网页服务来运行;如果偏向使用其他的(译注:这里指Apache 以外的服务),配置也不会很麻烦。现在,通过 http://gitserver 就可以在线访问仓库了,在 http://git.server 上还可以通过 HTTP 克隆和获取仓库的内容。 Again, GitWeb can be served with any CGI capable web server; if you prefer to use something else, it shouldn’t be difficult to set up. At this point, you should be able to visit http://gitserver/ to view your repositories online, and you can use http://git.gitserver to clone and fetch your repositories over HTTP.