在一台centos上搭建git,并把源码从svn上迁移到git,保留其中的历史记录和用户信息
1、centos上安装 git,gitweb,搭建http服务
过程不再详细说明,大致上是:
yum install httpd
yum install git
yum install git-svn
yum install gitweb
安装 git-svn是为了从svn导出。 安装gitweb是为了方便web方式查看git项目库,如果不需要可以不安装。
2、配置httpd
增加:/etc/httpd/conf.d/git.conf文件,内容如下:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.localhost
#DocumentRoot "/home/gituser/repos"
#ServerName localhost
#ServerAlias localhost
SetEnv GIT_PROJECT_ROOT /home/gituser/repos
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
Alias /gitweb /var/www/git
ErrorLog "logs/git-error.log"
CustomLog "logs/git-access.log" common
<Directory "/home/gituser/repos/*">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory "/home/gituser/users/*">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory "/usr/libexec/git-core*">
Options ExecCGI Indexes
Order allow,deny
Allow from all
Require all granted
</Directory> <Directory /var/www/git>
#Options ExecCGI FollowSymLinks SymLinksIfOwnerMatch
Options ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex gitweb.cgi
</Directory>
<Location /git>
AuthType Basic
AuthName "Git Access"
AuthUserFile /home/gituser/users/htpasswd
Require valid-user
</Location>
</VirtualHost>
其中,
- home/gituser/repos为 git仓库目录,可以根据需要修改
- 如果不使用 gitweb,可去掉:
Alias /gitweb /var/www/git
- 访问/git目录需要输入用户名密码。如果不需要,可以不配置最后一个的 Location。如果配置了,需要生成 htpasswd文件,并放入到配置文件指定的目录中。生成密码文件方法:
htpasswd -c htpasswd username
(第一个用户,有 "-c" 生成文件,后续的不需要再有 -c参数了,否则已有文件会被覆盖)
3、修改 gitweb配置文件
/etc/gitweb.conf文件,增加一行 指向仓库根目录的路径:
$projectroot = "/home/gituser/repos"
4、目录及权限的说明
需要保证:/home/gituser/repos 对 httpd的服务进程可读写, /home/gituser/users/htpasswd文件需要对httpd的服务进程刻读。为简单起见,我创建了一个用户 gituser,组为 gituser。httpd的服务运行用户为 apache, 组为 apache。只要把 apache这个用户加入到 gituser组中:执行
sudo usermod -a -G gituser apache
然后把 仓库的目录改为组可读写
chmod -R g+w /home/gituser/repos
5、现在,已经部署成功。检查一下
初始化一个库:
cd /home/gituser/repos
mkdir test.git
cd test.git
git init --bare
切换到另一目录,获取该库:
cd /home/gituser/work
git clone http://localhost/git/test.git
应该能够成功。
本人安装过程中出现了500错误,检查 /var/log/httpd/git-error.log,发现是 httpd不能访问 /home/gituser/users/htpasswd导致。需要把 SELinux的权限关闭:
setenforce 0
6、在其他机器上用浏览器 打开 http://192.168.0.100/gitweb,可以看到项目
7、接下来要导入已有svn项目内容。先准备用户账号映射
首先,获取svn中的账号信息:
svn log --xml | grep author | sort -u | perl -pe 's/.*>(.*?)<.*/$1 = /'
保存为文本后,编辑修改成下面的格式:
user1_in_svn = username1_in_git <user1@mail.com>
user2_in_svn = username2_in_git <user2@mail.com>
其中等号前面的为 在svn中的账号, 等号后面的为要转换出来的git账号
8、导库:
先创建一个空的库
cd /home/gituser/repos
mkdir projectname
git init --bare
切换到工作目录
cd /home/gituser/work
git svn clone https://192.168.0.101/svn/projectname --authors-file=/home/gituser/users/svn-users.txt --no-metadata projectname
其中 192.168.0.101为你现在的svn访问地址,改成需要的地址。 svn-users.text 为上一步的用户账号映射文件
可以使用 git log来查看本地的库
接下来推送的服务器上:
cd projectname
git remote add projectname http://localhost/git/projectname
git push origin --all
至此,库已经成功导入,历史信息均已经保留。