关于 Mercurial 的简介和基本操作,请参见小G的随笔《Mercurial(Hg)基本操作》。
我不再赘述 Mercurial 的各种让人振奋的特性,上一篇中我们提到了使用第三方版本库 bitbucket 来进行私有代码托管,但是小G在使用过程发现,由于天朝的网络问题,经常会使通信中断而导致同步操作失败。所以,这一篇,小G来和大家分享一下怎么搭建自己的 Mercurial 版本控制服务器。
1.环境和所需的工具
小G的服务器是 Windows Server 2008 R2 x64 版本, 对于软件小G一向是“喜新厌旧”的(^O^),所以下面所列出的这些工具,都是目前为止的最新版本:
|
2.操作步骤
1) 先安装 Python 2.7.3,接着安装 Mercurial 2.4.2 Python 2.7 package,再安装 TortoiseHg 2.6.2 with Mercurial ,最后安装 URL Rewrite 组件;也可以不按这个顺序安装,但是 Python 2.7.3 一定要在 Mercurial 2.4.2 Python 2.7 package 之前安装(推荐大家全部使用各工具软件的 x86 版本,因为 x64 位版本的 Python 小G在配置时报错了)。
2) 新建一个目录用于存放 Mercurial 站点,小G的目录是:E:\SkyDrive\Development\Mercurial\hgweb;
3) 在 IIS 中新建一个网站,名称自定义,小G的网站名是 hgweb,物理路径指向上一步的 E:\SkyDrive\Development\Mercurial\hgweb;
4) 在 IIS 中选择刚才新建的网站,右侧的功能视图中选择 “处理程序映射”,如下图:
双击“处理程序映射”,在右侧的动作列表中选择“编辑脚本映射”,按下图配置:
其中“请求路径”指定要处理的文件类型,“可执行文件”指示处理 cgi 文件的应用及其参数。
cgi 是个“古老”但很NB的东东,IIS 5.1 下的配置,请参见《基于windows IIS的C语言CGI WEB服务器环境搭建》。
5) 创建 hgweb.cgi 默认文档
选择刚才新建的 hgweb 站点,在右侧的功能视图中选择默认文档,双击打开:
在右侧的动作列表中选择“添加”,新增一个 hgweb.cgi 的文档项,并将此文档上移置顶:
6) 创建 hgweb.cgi 文件
在 Mercurial 的站点目录(E:\SkyDrive\Development\Mercurial\hgweb)下,新建一个 hgweb.cgi 文件(文件名可自定义),填入以下内容:
|
#!C:/Python27/python.exe # # An example FastCGI script for use with flup, edit as necessary # Path to repo or hgweb config to serve (see 'hg help hgweb') config = "E:/SkyDrive/Development/Mercurial/hgweb/hgweb.config" # Uncomment and adjust if Mercurial is not installed system-wide # (consult "installed modules" path from 'hg debuginstall'): import sys; sys.path.insert(0, "C:\\Program Files (x86)\\Mercurial") # Uncomment to send python tracebacks to the browser if an error occurs: import cgitb; cgitb.enable() from mercurial import demandimport; demandimport.enable() from mercurial.hgweb import hgweb, wsgicgi application = hgweb(config) wsgicgi.launch(application) |
说明:注意第一行,一定要用#!,否则会被当成注释,后面是 Python 2.7 的安装路径;上面第二个灰色区域是 Mercurial 的安装路径,大家请根据自己的具体情况做调整。
7) 创建 hgweb.config 文件
在 Mercurial 的站点目录(E:\SkyDrive\Development\Mercurial\hgweb)下新建一个 hgweb.config 文件,填入以下内容:
| [paths] /Repositories/ = E:/SkyDrive/Development/Mercurial/Repositories/* [web] descend = True baseurl = / |
其中阴影部分等号左侧类似于一个版本库分组,右侧是版本库的物理路径;
这时你其实已经可以使用匿名用户访问版本库,输入网址应该可以看到如下界面:
小G在这里绑定了子域名,大家如果没有域名绑定时可直接输入IP地址进行测试;
8) 启用 Url Rewrite
包含 hgweb.cgi 的请求路径是不是有点复杂?下面小G告诉大家如何去除这个复杂的东东。
在新建 HgWeb 站点时,IIS 会自动创建一个 Web.config 配置文件,用编辑器(小G比较喜欢用 Notepad++)打开这个配置文件,并在 System.webServer 的 handlers 结点下添加一个 rewrite 配置节,最终的文件内容如下:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<add value="hgweb.cgi" />
</files>
</defaultDocument>
<handlers accessPolicy="Read, Script">
<remove name="CGI-exe" />
<add name="CGIHandler" path="*.cgi" verb="*" modules="CgiModule" scriptProcessor="c:\Python27\python.exe -u "%s" "%s"" resourceType="Unspecified" requireAccess="Script" />
<add name="CGI-exe" path="*.exe" verb="*" modules="CgiModule" scriptProcessor="C:\Python27\python.exe" resourceType="File" requireAccess="Execute" allowPathInfo="true" />
</handlers>
<rewrite>
<rules>
<clear />
<rule name="hgweb.cgi" enabled="true" patternSyntax="Wildcard">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="hgweb.cgi/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
<appSettings>
</appSettings>
</configuration>