- 网上的东西太乱太杂,只好静下来了,从最基本的地方,一点一点来。尽量确保自己写的每一行东西确实努力去弄懂了
安装
在Windows下需要的就是msysgit了,机器上现在安装的是
我们关心的主要是
D:\Program Files\Git\libexec\git-core\git-http-backend.exe
注意:
系统中当前apache版本信息:
这个没什么可说的。直接在官网下载安装即可。
创建仓库
选择一个目录 D:\ImportantData\git
git init --bare Test.git
然后期待通过
git http://127.0.0.1/git/Test.git
来访问这个软件仓库
httpd.conf 第一次配置
网上搜到的东西太多了,还是从最根本的看起:
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>
效果
现在我们可以通过http来clone仓库了:
git clone http://127.0.0.1/git/Test.git
fetch和pull也都没问题,但是我们不能push
upload 与 receive
http-backend提供了两个服务:
服务
|
允许你
|
默认
|
upload-pack
|
git fetch
git pull
git clone
|
开启
|
receive-pack
|
git push
|
关闭
|
要想能匿名push,我们需要开启recieve-pack服务,可通过修改仓库的config文件可以实现:
[http]
receivepack = true
当然,同样的方式,我们也可以关闭upload-pack服务。
这样一来,pull 和 push 都没问题了。第一阶段结束
httpd.conf 第二次配置
匿名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>
"POST /git/Test.git/git-upload-pack HTTP/1.1"
这样一来,只有通过验证的用户才可以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";
...
参考
|