- 网上的东西太乱太杂,只好静下来了,从最基本的地方,一点一点来。尽量确保自己写的每一行东西确实努力去弄懂了
安装
在Windows下需要的就是msysgit了,机器上现在安装的是
- Git-1.7.7-preview20111014.3xe
我们关心的主要是
D:\Program Files\Git\libexec\git-core\git-http-backend.exe
注意:
-
该程序需要 libiconv-2.dll 这个动态库,如果系统PATH路径中没有该文件,可以从Git安装目录下的bin目录中拷贝一个进来。
- 对我来说,当前系统PATH内有D:/MinGW/bin,该目录内有这个动态库。故不需要拷贝了
系统中当前apache版本信息:
-
Apache/2.2.15(Win32) DAV/2 SVN/1.6.6 mod_wsgi/3.3 Python/2.7
这个没什么可说的。直接在官网下载安装即可。
创建仓库
选择一个目录 D:\ImportantData\git
git init --bare Test.git
然后期待通过
git http://127.0.0.1/git/Test.git
来访问这个软件仓库
httpd.conf 第一次配置
网上搜到的东西太多了,还是从最根本的看起:
- 设置仓库的根目录
- 映射路径到我们的CGI程序:git-http-backend.exe
-
还有一个次要的 GIT_HTTP_EXPORT_ALL,如果不设置它的话,我们必须在每一个仓库中放置一个名为git-daemon-export-ok的文件才行
SetEnv GIT_PROJECT_ROOT D:/ImportantData/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /git/ "D:/Progra~1/Git/libexec/git-core/git-http-backend.exe/"
然后,设置路径的访问权限
- 我们的CGI程序的路径
- 我们仓库所在的路径
<Directory "D:/Program Files/Git/libexec/git-core/"> Allow From All </Directory> <Directory "D:/ImportantData/git"> Allow From All </Directory>
效果
现在我们可以通过http来clone仓库了:
git clone http://127.0.0.1/git/Test.git
fetch和pull也都没问题,但是我们不能push
upload 与 receive
http-backend提供了两个服务:
服务 |
允许你 |
默认 |
upload-pack |
git fetch |
开启 |
receive-pack |
git push |
关闭 |
要想能匿名push,我们需要开启recieve-pack服务,可通过修改仓库的config文件可以实现:
[http] receivepack = true
当然,同样的方式,我们也可以关闭upload-pack服务。
这样一来,pull 和 push 都没问题了。第一阶段结束
httpd.conf 第二次配置
匿名pull和push应该不是我们需要的,一般来说,应该是允许:
- 匿名用户可以pull
- 验证用户才可以push
和前面一样,基本设置不变
SetEnv GIT_PROJECT_ROOT D:/ImportantData/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAlias /git/ "D:/Progra~1/Git/libexec/git-core/git-http-backend.exe/" <Directory "D:/Program Files/Git/libexec/git-core/"> Allow From All </Directory> <Directory "D:/ImportantData/git"> Allow From All </Directory>
但我们对请求receive-pack的url路径进行限制
<LocationMatch "^/git/.*/git-receive-pack$"> AuthType Basic AuthName "Git repository" AuthUserFile D:/ImportantData/Repositories/htpasswd Require valid-user Allow From All </LocationMatch>
-
路径之所以这样,是因为我们git push时,http请求的格式大致是:
"POST /git/Test.git/git-upload-pack HTTP/1.1"
- 我们使用Basic的方式认证
-
密码文件放置在了D:/ImportantData/Repositories/htpasswd,和原来的SVN服务使用的相同。
-
密码文件可以使用 htpasswd 创建和修改
-
这样一来,只有通过验证的用户才可以push了(clone仓库时可以直接指定用户名):
git clone http://dbzhang800@127.0.0.1/git/Test.git
httpd.conf 第三次配置
接前面的第二配置,如果也要禁用匿名用户的clone等操作怎么办? 和push类似,只需要通过Location控制下面的HTTP请求路径即可:
"POST /git/Test.git/git-upload-pack HTTP/1.1"
但是,这个还阻止不了pull、fetch操作,因为它们HTTP头部是
"GET /git/Test.git/info/refs?service=git-upload-pack HTTP/1.1"
所以也要一块处理。
但是,实际上,我们可能只有部分私密仓库才需要这么设置。这时,我们可以直接控制某个仓库的路径就行了
- 比如,要控制我们以/git/Test开头的仓库路径:
<Location /git/Test> AuthType Basic AuthName "Git repository" AuthUserFile D:/ImportantData/Repositories/htpasswd Require valid-user </Location>
httpd.conf 第四次配置
前面我们一直使用的
ScriptAlias /git/ "D:/Progra~1/Git/libexec/git-core/git-http-backend.exe/"
还可以用下面的东西来替换(而保持功能不变):
ScriptAliasMatch \ "(?x)^/git/(.*/(HEAD | \ info/refs | \ objects/(info/[^/]+ | \ [0-9a-f]{2}/[0-9a-f]{38} | \ pack/pack-[0-9a-f]{40}\.(pack|idx)) | \ git-(upload|receive)-pack))$" \ "D:/Program Files/Git/libexec/git-core/git-http-backend.exe/$1"
这后这么复杂有什么好处么?
好处就是,这使得 git-http-backend.exe 这个东西只处理这些匹配的路径。而不会处理其他的,比如像
http://127.0.0.1/git/goodbyeGit
这样的路径。这样的路径可以进一步交由其他CGI程序处理。比如gitweb.cgi
httpd.conf 第五次配置
配置gitweb
在前面的基础上
- 添加上gitweb.cgi这个脚本
- 并设置其所在目录的权限
ScriptAliasMatch \ "(?x)^/git/(.*/(HEAD | \ info/refs | \ objects/(info/[^/]+ | \ [0-9a-f]{2}/[0-9a-f]{38} | \ pack/pack-[0-9a-f]{40}\.(pack|idx)) | \ git-(upload|receive)-pack))$" \ "D:/Program Files/Git/libexec/git-core/git-http-backend.exe/$1" ScriptAlias /git/ "D:/Progra~1/Git/share/gitweb/gitweb.cgi/" <Directory "D:/Program Files/Git/share/gitweb/"> Options +ExecCGI Allow From All </Directory>
由于在Windows下,还需要将该cgi脚本第一行改成我们Perl程序所在路径,比如:
这样就差不多了,而至于gitweb如何配置,额,我还不清楚。只是注意到可以直接修改这个 gitweb.cgi 文件中的:
our $GIT = "git"; our $projectroot = "D:/ImportantData/git"; ...