系統環境:Ubuntu12.04
服務器上安裝git+gitolite+gitweb
root@server: 為搭建git服務器終端,假設ID為 192.168.199.117 或域名為: http://www.radisson.tech:8081 root@adminGit: 為管理員終端, 假設ID為 192.168.199.119 root@client: 為客戶終端, 假設ID為 192.168.199.35
建立git以及adminGit賬戶:
git用戶存放Gerrit Sever以及Gitolite相關文件:
root@server: apt-get install git root@server: apt-get install gitolite root@server: sudo adduser git
創建管理員SSH-KEY&安裝:
假設管理員使用使用的是另外一台PC生成,生成SSH-KEY(adminGit.pub)並將adminGit.pub上傳到遠端GIT服務器:
root@adminGit: ssh-keygen -t rsa -C "xxx@163.com" //用戶目錄的.ssh文件夾生產id_rsa & id_rsa.pub,注意要指定郵箱 root@adminGit: mv id_rsa.pub adminGit.pub //更名id_rsa.pub為adminGit@pub root@adminGit: scp ./adminGit.pub adminGit@192.168.199.xxx:adminGit.pub //.ssh目錄執行,將ssh-key上傳到GIT服務器
root@adminGit: git config --global user.name "adminGit"
root@adminGit: git config --global user.email "adminGit@xxx.com"
通過SSH-KEY認證的用戶,在用戶目錄的“~/.gitconfig”記錄了配置信息。
GIT服務器端(git用戶)安裝管理員SSH-KEY:
root@server: cp -f /home/git/adminGit.pub ./ //管理員的ssh-key root@server: gl-setup ./adminGit.pub //gitolite安裝管理員權限
//輸出如下:
creating gitolite-admin...
Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/
creating testing...
Initialized empty Git repository in /home/git/repositories/testing.git/
[master (root-commit) b2f63aa] start
2 files changed, 6 insertions(+)
create mode 100644 conf/gitolite.conf
create mode 100644 keydir/adminGit.pub
git HOME目錄如下:
注意“.gitolite”&“repositories”目錄,“.gitolite”是做GIT服務器權限配置(SSH-KEY/倉庫/用戶權限配置),一般不直接修改。
如果做權限配置,需要管理員adminGit用戶先檢出“/home/git/repositories/gitolite-admin.git”倉庫,修改倉庫的權限並提交后,git服務器端“/home/git/.gitolite”目錄權限文件會跟隨同步修改。
-rw------- 1 git git 590 Nov 22 22:26 .bash_history -rw-r--r-- 1 git git 220 Nov 20 19:31 .bash_logout -rw-r--r-- 1 git git 3486 Nov 20 19:31 .bashrc drwx------ 2 git git 4096 Nov 20 19:33 .cache/ drwx------ 8 git git 4096 Dec 4 01:17 .gitolite/ -rw-r--r-- 1 git git 4217 Dec 4 01:17 .gitolite.rc -rw-r--r-- 1 git git 675 Nov 20 19:31 .profile drwx------ 5 git git 4096 Dec 4 03:43 repositories/ drwx------ 2 git git 4096 Dec 4 03:43 .ssh/ -rw------- 1 git git 4976 Dec 4 04:06 .viminfo
權限修改Gitolite方式:
1、管理員客戶端檢出GIT服務器端管理權限代碼,用於管理gitolite&添加用戶:
root@adminGit: git clone git@192.168.199.117:gitolite-admin.git //檢出
2、添加新用戶:
管理員添加新用戶步驟(test1用戶):
//將新用戶test1的公鑰復制到管理員端檢出庫gitolite-admin/keydir目錄 root@adminGit: git add keydir //執行 git add 命令,將公鑰添加入版本庫 root@adminGit: git status root@adminGit: git commit -m “add user: xxx” //執行 git commit,完成提交 root@adminGit git push //執行 git push,同步到服務器,才真正完成新用戶的添加
此時GIT服務器端添加的內容如下:
root@server: vim /home/git/.ssh/authorized_keys
# gitolite start command="/usr/share/gitolite/gl-auth-command adminGit",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLVlymtEwqHEQ6g4f7BZYumnAknNsRp+j0yD6Z9cIrs14CDozZVB60PiZgrNP+copeqo9eE6zqUiAsnpESmm4DkvUPGDzIKU3j60kmPuB6rzaVoR3hguDKWp8lOzQ1wBSjOq+eK840TH1qVoutCQ2E2KeatmcK/xPpTf4q2PExjqRajpj98BZ4Qgw5YDi8uRXklbkqMv9V7uy3kTMow06VFtjZEuZpsaCaJKoQEMUhvqO2qoCkvWo43xqdS/mKCMqRlB0cFwWFyox/Er/IK/muovK5/V/gdRIQyjjf97bahNAOuRg6LHgVZlKCFkaVB9h024XHG+tDzjs/VVR3UkLZ adminGit@shkk command="/usr/share/gitolite/gl-auth-command test1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrwht4SPGcYv7nxioVDAaHnApSb1L5TNlXY9Nni+Hv9g0Bn095rRKMMHecr+IIuFQaOyuCjNdJysYDaClsPL3eA4DffZOIfanEX7ANA0CL3vPvu7Vl//vuc6waP3JXBxTspqVUIl63aShHK/FwALdIRplqKqJ4tZiayJ/ugMnwvmpqJCmK8+n1xodcHIWaEQHSB5S6yyxlV7FwDJQ7pAFIFVu6l5yLB1R9nCw0+iRBxoLK9S+BGd3nzEbvX6BRZakIfoSa77Z5zy4JpoHB+A34xXw/pQyoZI6zVy0J3lYT2E/PZ1ihoChxfNSj/UVmynovc7zwtGy3h0q3NIcdlnS9 test1@ubuntu # gitolite end
/home/git/.gitolite/keydir 增加了“test1.pub”文件
完成用戶添加。
3、遠程建倉gittest.git&配置test1用戶權限:
root@adminGit: vim ./gitolite-admin/conf //管理員客戶端,遠程建倉,及倉庫用戶權限修改 repo gitolite-admin RW+ = adminGit repo repo/gittest RW+ = test1 repo testing RW+ = @all root@adminGit: git add ./conf/ root@adminGit: git commit -m "create remote repository & setting test1 user" root@adminGit: git push //輸出信息如下: Counting objects: 7, done. Delta compression using up to 16 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 391 bytes, done. Total 4 (delta 0), reused 0 (delta 0) remote: creating repo/gittest... remote: Initialized empty Git repository in /home/git/repositories/repo/gittest.git/ To git@192.168.199.117:gitolite-admin.git f9bbaa4..90ba89d master -> master
管理員提交后,GIT服務器端會在相應目錄遠程建倉(/home/git/repositories/repo/gittest.git/)& 添加test1用戶對於gittest1.git倉庫的權限。
4、用戶test1客戶端測試檢出gittest.git倉庫:
root@client: git clone git@192.168.199.117:repo/gittest.git //檢出成功,輸出如下: Cloning into 'gittest'... warning: You appear to have cloned an empty repository.
安裝Gerrit:
root@server: java -jar gerrit-2.10.war init -d ./gerritSever/ *** Gerrit Code Review 2.10 *** Create '/home/git/gerritSever' [Y/n]? *** Git Repositories *** Location of Git repositories [git]: /home/git/repo *** SQL Database *** Database server type [h2]: *** Index *** Type [LUCENE/?]: *** User Authentication *** Authentication method [OPENID/?]: http Get username from custom HTTP header [y/N]? SSO logout URL : *** Review Labels *** Install Verified label [y/N]? *** Email Delivery *** SMTP server hostname [localhost]: SMTP server port [(default)]: SMTP encryption [NONE/?]: SMTP username : *** Container Process *** Run as [git]: Java runtime [/usr/lib/jvm/java-7-openjdk-i386/jre]: Copy gerrit-2.10.war to /home/git/./gerritSever/bin/gerrit.war [Y/n]? Copying gerrit-2.10.war to /home/git/./gerritSever/bin/gerrit.war *** SSH Daemon *** Listen on address [*]: Listen on port [29418]: Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v149 If available, Gerrit can take advantage of features in the library, but will also function without it. Download and install it now [Y/n]? Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar ... !! FAIL !! error: http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar: 302 Found Please download: http://www.bouncycastle.org/download/bcpkix-jdk15on-149.jar and save as: /home/git/./gerritSever/lib/bcpkix-jdk15on-149.jar Press enter to continue Continue without this library [Y/n]? Generating SSH host key ... rsa(simple)... done *** HTTP Daemon *** Behind reverse proxy [y/N]? y Proxy uses SSL (https://) [y/N]? n Subdirectory on proxy server [/]: Listen on address [*]: Listen on port [8081]: Canonical URL [http://ubuntu/]: http://192.168.199.117:8081/ #或者填寫正確的外網域名:http://www.xxx.com:8081 *** Plugins *** Install plugin commit-message-length-validator version v2.10 [y/N]? Install plugin download-commands version v2.10 [y/N]? Install plugin replication version v2.10 [y/N]? Install plugin reviewnotes version v2.10 [y/N]? Install plugin singleusergroup version v2.10 [y/N]? Initialized /home/git/gerritSever Executing /home/git/./gerritSever/bin/gerrit.sh start Starting Gerrit Code Review: OK Waiting for server on 192.168.199.117:8081 ... OK Opening http://192.168.199.117:8081/#/admin/projects/ ...FAILED Open Gerrit with a JavaScript capable browser: http://192.168.199.117:8081/#/admin/projects/ git@ubuntu:~$
配置Gerrit服務器:/home/git/gerritSever/etc/gerrit.config
root@server: vim /home/git/gerritSever/etc/gerrit.config
[gerrit] basePath = /home/git/repo canonicalWebUrl = http://192.168.199.117:8081/ #外網域名 http://www.xxx.com:8081 [database] type = h2 database = db/ReviewDB [index] type = LUCENE [auth] type = HTTP [sendemail] smtpServer = localhost [container] user = git javaHome = /usr/lib/jvm/java-7-openjdk-i386/jre [sshd] listenAddress = *:29418 [httpd] listenUrl = proxy-http://*:8091/ [cache] directory = cache
此處注意:由於apache2監聽的端口與反向代理鏈接的Gerrit端口是同一台服務器,所以端口不能設置為一致:
這里設置apache2監聽的端口為:8081,轉接到Gerrit的端口為8091,即所有http://192.168.199.117:8081/收到的信息,都會反向代理到GerritServer:http://127.0.0.1:8091/
反向代理Apache2配置:
1、配置監聽端口:
root@server: vim /etc/apache2/ports.conf NameVirtualHost *:80 NameVirtualHost *:8081 Listen 80 Listen 8081
2、配置反向代理:
root@server: cd /etc/apache2/sites-available/ root@server: touch gitProxy //啟用目錄&建立軟鏈接 root@server: cd /etc/apache2/sites-enabled/ root@server: sudo ln -s ../sites-available/gitProxy gitProxy
gitProxy配置如下:
<VirtualHost *:8081> #Apache2監聽的端口 ServerName 192.168.199.117 #Apache2Sever IP ProxyRequests Off #正向代理為OFF,即為反向代理 ProxyVia Off ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy> <Location "/login/"> AuthType Basic AuthName "Gerrit Code Review" Require valid-user AuthUserFile /home/git/gerritSever/passwords #web認證的用戶名&密碼 </Location> AllowEncodedSlashes On ProxyPass / http://127.0.0.1:8091/ nocanon #反向代理的GerritServer IP </VirtualHost>
3、需要WEB訪問的話,使用htpasswd,htpasswd命令是Apache的Web服務器內置工具,用於創建和更新儲存用戶名、域和用戶基本認證的密碼文件。
root@server: cd /home/git/gerritSever root@server: touch passwords root@server: htpasswd -b passwords git 密碼 //增加一個WEB認證的用戶名和密碼
4、配置“httpd.conf”
root@server: sudo vim /etc/apache2/httpd.conf //添加如下 ServerName localhost
5、開啟反向代理
root@server$ cd /etc/apache2/mods-enabled
root@server$ sudo ln -s ../mods-available/proxy.load root@server$ sudo ln -s ../mods-available/proxy.conf root@server$ sudo ln -s ../mods-available/proxy_http.load root@server$ sudo ln -s ../mods-available/proxy_balancer.conf root@server$ sudo ln -s ../mods-available/proxy_balancer.load root@server$ sudo ln -s ../mods-available/rewrite.load root@server$ sudo ln -s ../mods-available/ssl.conf root@server$ sudo ln -s ../mods-available/ssl.load root@server$ sudo /etc/init.d/apache2 restart
6、驗證反向代理,IE瀏覽器:http://192.168.199.117:8081/
這里要使用htpasswd認證的用戶名和密碼登陸。

安裝GitWeb:
root@server: sudo apt-get install gitweb
//重啟Aapache2&Gerrit服務器 root@server: sudo service apache2 root@server: /home/git/gerritSever/bin/gerrit.sh restart
REPO工具集安裝:
$ git clone http://gerrit.googlesource.com/git-repo git-repo.git //或者 $ git clone http://review.mfunz.com/git-repo git-repo.git $ vim git-repo.git/repo REPO_URL = 'ssh://192.168.199.119:29418/git-repo' REPO_REV = 'master' $ cp -f ./git-repo.git/repo /bin //注意:repo的REPO_VER為git-repo倉庫的分支 //git-repo建倉 ssh -p 29418 git@192.168.199.119 gerrit create-project -n git-repo rm -rf .git git init git add ./ git commit -m "git-repo" git push ssh://git@192.168.199.119:29418/git-repo HEAD:refs/heads/master git clone ssh://git@192.168.199.119:29418/git-repo git tag -a -m "master" master git push origin –tags
配置項目:
環境搭建完成后,開始配置具體項目。
1、增加一個該項目的權限組,編輯該權限組的權限:
git clone ssh://git@192.168.199.119:29418/All-Projects //先拉一份All-Projects下來 mv All-Projects All-3553 //將All-Projects 改名為 All-3553 rm -rf .git //刪除原來的.git倉庫 ssh -p 29418 git@192.168.199.119 gerrit create-project -n All-3553 //新建遠程倉庫All-3553 //在All-3553根目錄下: git init git add ./ git commit -m "init All-3553" git push ssh://git@192.168.199.119:29418/All-3553 HEAD:refs/heads/master //將本地內容推送到遠程倉庫All-3553
2、為各賬號配置權限
其中,需要做代碼審核的,必須提交到:refs/for/dev
例如:git push origin dev:refs/for/dev
3、創建manifests倉庫,並建立dev分支
ssh -p 29418 git@192.168.199.119 gerrit create-project -n msd3553/manifests //新建manifests的目錄,拷貝default.xml拷貝進來: git init git add ./ git commit -m "init manifests" git push ssh://git@192.168.199.119:29418/msd3553/manifests HEAD:refs/heads/master git clone ssh://git@192.168.199.119:29418/chakra3/manifests //克隆&建dev分支 git checkout -b dev vim default.xml //revision為master git add ./default.xml git push ssh://git@192.168.199.119:29418/chakra3/manifests HEAD:refs/heads/dev
4、根據清單文件批量建倉
#!/bin/bash SERVER_IP="192.168.199.119" SERVER_PORT="29418" REMOTE_FETCH="ssh://${SERVER_IP}:${SERVER_PORT}/" REMOTE_REVIEW="http://${SERVER_IP}" PROJECT_NAME="testGit" BRANCH="dev" SRC_SOURCE=`pwd` #根據目錄下的.git創建project.list #刪除目錄下的.git&.gitignore #在空目錄下創建.gitkeep,避免建倉時空目錄不能提交 function createProjectList(){ find ./ -name ".git" > project.list sed -ri "s/^..(.*)..git$/\1/" project.list find ./ -name ".git" | xargs rm -rf {} find ./ -name ".gitignore" | xargs rm -rf {} touch .gitkeep find ./ -type d -empty | xargs -i cp ./.gitkeep {} rm -f ./.gitkeep } #創建manifests.xml清單文件 function createManifestsXml(){ echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > manifest.xml echo "<manifest>" >> manifest.xml echo " <remote fetch=\"${REMOTE_FETCH}\" name=\"origin\" review=\"${REMOTE_REVIEW}\"/>" >> manifest.xml echo " <default remote=\"origin\" revision=\"${BRANCH}\" sync-j=\"4\" sync-c=\"true\"/>" >> manifest.xml echo "" >> manifest.xml GIT_PROJECTS=`cat project.list | xargs` for git_path in ${GIT_PROJECTS} do if [ ${git_path} == "build" ] then echo " <project name=\"${PROJECT_NAME}/${git_path}\" path=\"${git_path}\" >" >> manifest.xml echo " <copyfile dest=\"Makefile\" src=\"core/root.mk\"/>" >> manifest.xml echo " </project>" >> manifest.xml elif [ ${git_path} == "vendor/intel/support" ] then echo " <project name=\"${PROJECT_NAME}/${git_path}\" path=\"${git_path}\" >" >> manifest.xml echo " <copyfile dest=\"device/intel/Android.mk\" src=\"x86_only_Android.mk\"/>" >> manifest.xml echo " <copyfile dest=\"platform/vendor/intel/Android.mk\" src=\"x86_only_Android.mk\"/>" >> manifest.xml echo " </project>" >> manifest.xml else echo " <project path=\"${git_path}\" name=\"${git_path}\" />" >> manifest.xml fi done echo "" >> manifest.xml echo "</manifest>" >> manifest.xml } #遠程建倉並提交SRC function createRepo() { GIT_PROJECTS=`cat project.list | xargs` for repoName in ${GIT_PROJECTS} do #批量建倉 echo "Init Empty Repo:${PROJECT_NAME}/${repoName}" ssh -p $SERVER_PORT git@$SERVER_IP gerrit create-project -n ${PROJECT_NAME}/${repoName} done while read git_name do if [ -d ${SRC_SOURCE}/${git_name} ] then echo "${SRC_SOURCE}/${git_name}" cd ${SRC_SOURCE}/${git_name} git init git add ./ git commit -m "Init Repo" git push ssh://git@${SERVER_IP}:${SERVER_PORT}/${PROJECT_NAME}/${git_name} HEAD:refs/heads/master fi done < project.list } createProjectList createManifestsXml createRepo
5、repo pull 遠程代碼:注意:參數--no-repo-verify為不驗證簽名
repo init -u ssh://git@192.168.199.119:29418/msd3553/manifests.git --no-repo-verify
檢查manifests的revision為master,先檢出master分支:
$ vim .repo/manifest.xml <?xml version="1.0" encoding="UTF-8"?> <manifest> <remote name="origin" fetch="ssh://192.168.199.119:29418/msd3553" review="http://192.168.199.119:8089/" /> <default revision="master" remote="origin" sync-j="4" sync-c="true" /> <project path="boot" name="boot" /> <project path="code" name="code" /> <project path="BUILD_RECORD" name="BUILD_RECORD" /> <project path="CUS_FILE" name="CUS_FILE" /> <project path="MSTAR_ORIGINAL" name="MSTAR_ORIGINAL" /> </manifest>
repo根據manifests同步代碼,由於腳本批量建立了master分支,注意檢查manifests的revision為master,否則repo sync會出錯:
repo sync
6、根據manifests批量建立遠程開發分支dev:
repo start dev --all //獲取的遠程master,在本地建立了dev分支 repo branch repo forall -p -c git push origin dev:dev //將本地倉庫的dev分支提交為遠程倉庫的dev分支,一般要加上-p參數,打印出倉庫的路徑
7、介紹repo檢出&檢入:
1、要注意manifests文件:revision="master"/“dev”,需要檢出的是哪一個分支,下面檢出master:
repo init -u ssh://xxx@192.168.199.119:29418/msd3553/manifests.git --no-repo-verify repo sync //要在sync之前檢查manifests repo start master --all //目前已獲取到遠程倉庫分支為master,本地分支master的代碼 repo branch
2、dev分支檢出:
repo init -u ssh://xxx@192.168.199.119:29418/msd3553/manifests.git --no-repo-verify //注意:manifets需要修改檢出賬號 fetch="ssh://xxx@192.168.199.119:29418/msd3553" repo sync repo start dev --all //遠程倉庫分支為dev,本地分支為dev的代碼 repo branch
3、檢入:
$ git add ./xxx $ git commit -m "xxx" --no-verify $ git push origin dev:refs/for/dev
4、repo分支切換
$ cd .repo/manifests $ ls //查看xxx.xml文件 $ cd .. //回到上級目錄 $ ln -s manifest.xml manifests/QK_CM298_EXER.xml //更換manifest.xml的軟連接 $ cd .. $ repo sync //同步代碼 $ repo start xxx--all//建立本地分支
$ repo checkout //切換分支
8、沙箱操作:
//創建sandbox分支 git push ssh://xxx@192.168.199.119:29418/msd3553/子倉庫 HEAD:refs/heads/sandbox/個人賬號/分支目錄 //獲取沙箱 git clone -b sandbox/個人賬號/分支目錄 ssh://xxx@192.168.199.119:29418/msd3553/字倉庫
repo常用:
git status -->查看當前git倉的狀態,是否有文件被更新、修改等 git log -->查看當前git倉的所有提交log repo sync . -->同步當前git倉,如果當前倉工作區的某個文件有未提交的修改,則以當 前文件為准,不會強制與服務器同步該文件。該命令與在工程根目錄下執 行:repo sync + git倉路徑 效果一樣,注意 repo sync 后面帶一個'.' git branch + 分支名稱 -->新建一個本地分支 git branch -a -->查看所有分支,包括本地分支和遠程分支,不加-a,只查看本地分支 git checkout + 本地分支名稱 -->切換到某個本地分支上 git branch -D + 本地分支名稱 -->刪除某個本地分支 git add . -->提交當前git倉下的所有修改 git commit -am "修改記錄" -->將修改提交至緩沖區,並為修改添加修改記錄 repo upload . -->將修改提交至gerrit服務器評審
下載新分支
repo forall -c git checkout -b 本地分支名稱(自定義) 服務器分支名稱
切換到另一個分支
repo forall -c git checkout your_branch
注意事項:
1、因為第一個登陸gerrit的賬號為管理員,所以必須要用git賬號先登陸。登陸后,先把git的git.pub內容增加到ssh key中。在GitWeb登陸的個人賬戶中,要設置郵箱,WEB不能直接設置的話,要使用SSH終端進行設置。
如Git用戶的郵箱設置:注意該郵箱要和用git建立該用戶時登記的郵箱一致(可以查看該用戶登陸目錄的.gitconfig文件,不一致repo報錯:“Enter passphrase for key '/home/kk/.ssh/id_rsa':”)
ssh -p 29418 192.168.199.119 gerrit set-account --add-email git@kitking.com git

2、多個客戶端可以使用同一個“私鑰”&“公鑰”,即將公鑰&私鑰內容保持一致即可
使用時注意:私鑰名字 “id_rsa”不能改變,公鑰的名字可以改變。
3、終端登陸的名稱&遠端SSH名稱要一致,否則報紅色箭頭錯誤。“Permission denied (publickey).”

即下面橙色標記處需要一致。
jiangzhaowei@ubuntu:~/ss$ repo init -u ssh://jiangzhaowei@192.168.199.119:29418/chakra3/manifests.git --no-repo-verify
4、代碼的權限配置
41 git clone ssh://git@192.168.199.119:29418/All-Projects
43 mv All-Projects All-3553
45 cd All-3553/
51 rm -rf .git/
54 ssh -p 29418 git@192.168.199.119 gerrit create-project -n All-3553
56 git init
57 git add ./
58 git commit -m "init 3553"
59 git push ssh://git@192.168.199.119:29418/All-3553 HEAD:refs/heads/master
5、查看配置參數:記錄在 /etc/gitconfig以及~/.gitconfig以及.git/config文件都可以看到配置的定義。
命令行查看如下:
$ git config --list
//配置信息如下:
user.name=jiangzhaowei user.email=jiangzhaowei@kitking.com color.ui=auto core.repositoryformatversion=0 core.filemode=true remote.origin.url=ssh://192.168.199.119:29418/msd3553/code remote.origin.review=http://192.168.199.119:8089/ remote.origin.projectname=code remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.dev.remote=origin branch.dev.merge=dev
$ git remote -v
6、提交空格符的處理
$ git commit -m "modify file"
ERROR: Your commit has trailing white space, please fix it.
You can use the follow command to do the check. git diff --cached --check --no-ext-diff $ find ./ -regex ".*\.c|.*\.h" | xargs sed -ri 's/ +$//g' //迭代目錄的源文件、頭文件,處理空格結尾的行
7、文件權限檢查
repo forall -c git config core.fileMode false git config --global core.fileMode false
安裝相關軟件
$
sudo
apt-get dist-upgrade
$
sudo
apt-get
install
mysql-client-5.5 mysql-server-5.5 git-core apache2.2-bin apache2.2-common apache2-utils
|
配置Mysql
$
sudo
apt-get
install
mysql-client-5.5 mysql-server-5.5
1.創建數據庫表和帳戶,必須執行下面命令進去mysql
#mysql -u root -p
2.在mysql中建立一個以項目名稱命名的用戶密碼為secret(以project為例)
mysql>CREATE USER
'project'
@
'localhost'
IDENTIFIED BY
'secret'
;
注意:可能會有這個錯誤的:
mysql> CREATE USER
'project'
@
'localhost'
IDENTIFIED BY
'secret'
;
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s)
for
this operation
是因為直接mysql登陸進去的,請用mysql -u root -p 登錄!!!
3.在mysql中建立一個名為reviewdb_[項目名]庫
mysql>CREATE DATABASE reviewdb_project;
4.將所有權限給剛才建立的新的庫
mysql>GRANT ALL ON reviewdb_project.* TO
'project'
@
'localhost'
;
5.刷新權限表
mysql>FLUSH PRIVILEGES;
|
安裝Gerrit
.創建一個以項目名稱命名的新用戶,以project為例(自己可以根據實際的項目名來命名)
$
sudo
adduser project
以新用戶身份操作
$
sudo
su
- project
2.創建安裝目錄,以項目名命令。(建議直接拷貝使用其它項目的gerrit安裝目錄)
$
mkdir
-p review_site_project
3.下載gerrit
http:
//code
.google.com
/p/gerrit/
4.安裝並配置初始化gerrit
java -jar gerrit-2.9.1.war init -d review_site_project/
Create
'/home/gerrit2/review-site'
[Y
/n
]?
*** Git Repositories
***
Location of Git repositories [git]:
*** SQL Database
***
Database server
type
[H2/?]:
*** User Authentication
***
Authentication method [OPENID/?]: HTTP
//
注意這個不要按回車跳過去了,否則不是HTTP認證,是需要用google的OPENID
Get username from custom HTTP header [y
/N
]? :
SSO
logout
URL :
*** Email Delivery
***
SMTP server
hostname
[localhost]: smtp.company.com
//
自己改成自己的郵箱smtp
SMTP server port [(default)]:
SMTP encryption [NONE/?]:
SMTP username [?]: yourname@company.com
//
自己改成自己的郵箱
yourname@xxxxx.com's password :
confirm password :
*** Container Process
***
Run as [project]:
Java runtime [
/usr/lib/jvm/java-6-sun-1
.6.0.24
/jre
]:
Copy gerrit.war to
/home/gerrit2/review-site/bin/gerrit
.war [Y
/n
]?
Copying gerrit.war to
/home/gerrit2/review-site/bin/gerrit
.war
*** SSH Daemon
***
Listen on address [*]:
Listen on port [29418]:
//
如果同一個用戶下建多個gerrit,請不用都公用29418這個端口號
Gerrit Code Review is not shipped with Bouncy Castle Crypto v144
If available, Gerrit can take advantage of features
in
the library, but will also
function
without it.
Download and
install
it now [Y
/n
]?
Downloading http:
//www
.bouncycastle.org
/download/bcprov-jdk16-144
.jar ... OK
Checksum bcprov-jdk16-144.jar OK
Generating SSH host key ... rsa... dsa...
done
*** HTTP Daemon
***
Behind reverse proxy [y
/N
]? y
Proxy uses SSL (https:
//
) [y
/N
]? n
Subdirectory on proxy server [/]:
Listen on address [*]:
Listen on port [8081]:
//
切記,此端口號一定不要和apache2占用相同的端口號,
//
否則就會Starting Gerrit Code Review:Failed
Canonical URL [http:
//
*/]:http:
//1
.2.3.4:8080/
//
填自己的ip和端口號,勿完全抄
Initialized
/home/gerrit2/review-site
Executing
/home/gerrit2/review-site/bin/gerrit
.sh start
Starting Gerrit Code Review: OK
|
其他參考:
- 搭建最好在一個獨立的用戶上面,創建一個名為Gerrit的用戶進行環境變量的配置;
- 安裝Oracle JDK,如果已經安裝請跳過,如果沒有請自行安裝,如果系統中已經安裝OpenJDK請使用sudo update-alternatives --config 進行切換;
- 在Gerrit用戶先面配置JAVA_HOME、 JRE_HOME、CLASSPATH、PATH的相關變量;
- 從官網下載:http://code.google.com/p/gerrit/
- 安裝:java -jar gerrit_xxx.war init -d review_site
- 安裝過程中這里的鑒權方式要用HTTP:Authentication method [OPENID/?]: HTTP //注意這個不要按回車跳過去了,否則不是HTTP認證,是需要用google的OPENID
- 代理 Behind reverse proxy [y/N]? y //使用反向代理的話必須選擇y來配置,默認是N
- subdirectory on proxy server [/]: //可以選擇直接跳過,那樣你的gerrit最后的URL是http://10.1.42.85
- 建立一個代碼存放的目錄 mkdir /home/gerrit/GerritResource
到上面位置Gerrit的安裝已經完成。然后進行Gerrit配置文件的修改:
- vim review_site/etc/gerrit.config
[gerrit]
basePath =
/home/gerrit/GerritResource
-->上面創建空目錄
canonicalWebUrl = http:
//1
.2.3.4:8081/
[database]
type
= H2
database = db
/ReviewDB
[auth]
type
= HTTP
[sendemail]
enable
=
true
smtpServer =
smtpUser =
smtpPass =
from =
[container]
user = gerrit
javaHome =
/opt/jdk1
.7.0_38
/jre
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = proxy-http:
//1
.2.3.4:8081/
[cache]
directory = cache
|
配置Apache
$
sudo
apt-get
install
apache2.2-bin apache2.2-common apache2-utils apache2-mpm-worker
1.創建編輯apache配置文件,添加如下內容(下面以Ubuntu系統為例請注意不同操作系統apache配置文件的區別)也可配置 apache2.conf httpd.conf,我們這里配置的是
/etc/apache2/sites-available/gerrit
$
sudo
vi
/etc/apache2/sites-available/gerrit
NameVirtualHost 1.2.3.4:80
//
填自己的ip和端口號,勿完全抄
//
填自己的ip和端口號,勿完全抄
ServerName 1.2.3.4
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
Order deny,allow
Allow from all
<</code>/Proxy
>
/login/
>
AuthType Basic
AuthName
"Gerrit Code Review"
Require valid-user
AuthUserFile
/etc/apache2/passwords
//
指定http登錄認證的的paassword文件所在位置,放哪都行,根據后來
//sudo
htpasswd -cb
/etc/apache2/passwords
gerrit1 gerrit1
<</code>/Location
>
AllowEncodedSlashes On
ProxyPass / http:
//127
.0.0.1:8081/
ProxyPassReverse / http:
//127
.0.0.1:8081/
<</code>/VirtualHost
>
保存退出
$
cd
/etc/apache2/sites-enabled/
$
sudo
ln
..
/sites-available/gerrit
.
2.創建
passwd
文件,添加gerrit登錄用戶,-c參數為創建,僅限第一次添加用戶時使用
$
sudo
htpasswd -cb
/etc/apache2/passwords
scm scm
3.繼續配置apache2
$
sudo
vi
/etc/apache2/http
.conf
//
在文件中加入以下內容
ServerName localhost
$
cd
/etc/apache2/mods-enabled
$
sudo
ln
-s
/etc/apache2/mods-available/proxy
.conf proxy.conf
$
sudo
ln
-s
/etc/apache2/mods-available/proxy
.load proxy.load
$
sudo
ln
-s
/etc/apache2/mods-available/proxy_http
.load proxy_http.load
/etc/apache2
$
sudo
vi
ports.conf
NameVirtualHost *:80
Listen 80
Listen 8080
//
不要給gerrit!
Listen 8000
//
不要給gerrit!
添加apache2的tcp端口號,切記不要把gerrit的,即Listen on port [8081]: 不要輸入上面配置給apache2的端口號,新手切記!!!
否則gerrit 起不來的
搭建多個gerrit 時Listen on port [8081]:可以用8082 8083 8084 等
|
其他參考:
創建httpd.conf文件(當然,需要root權限的),錄入以下內容:
ServerName localhost
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
Order deny,allow
Allow from all
<</code>/Proxy
>
/login/
>
AuthType Basic
AuthName
"Gerrit Code Review"
AuthBasicProvider
file
AuthUserFile
/home/username/review_site/etc/passwords
Require valid-user
<</code>/Location
>
ProxyPass / http:
//1
.2.3.4:8081/
<</code>/VirtualHost
>
|
這個配置文件的意思是,當訪問任意(*)時,進行代理,代理到/login/目錄,並根據AuthUserFile請求用戶名及密碼,最終代理為http://1.2.3.4:8081/
這里有兩個重點需要說一下:
- 關於review_site/etc/passwords
- 打開終端,進入review_site/etc目錄下,執行:
- $: touch passwords
- $: htpasswd passwords gerrit
- 會請求你輸入此用戶(gerrit)的密碼,確認無誤后將相關信息寫入passwords文件中。這個用戶名及密碼就是你稍后訪問本地搭建的gerrit服務器所需要的。
- ProxyPass / http://10.1.42.85:8081/
- 自此應與gerrit.config中字段[httpd]的配置對應相同。
配置完后,保存該文件。打開apache2.conf
Include httpd.conf
通過ln -s 命令將proxy.conf proxy.load proxy_http.load從available中link到enable中即可。
啟動服務
1.啟動apache
sudo
/etc/init
.d
/apache2
restart
2.啟動gerrit服務
$review_site_project
/bin/gerrit
.sh start
|
登錄Gerrit
在瀏覽器地址欄中輸入http:
//1
.2.3.4:8080/
在顯示的登錄框中輸入用戶名/密碼,
即之前配置passwords
$
sudo
htpasswd -cb
/etc/apache2/passwords
gerrit1 gerrit1
Note: 請注意第一個登入的用戶,Gerrit會自動將其定義為管理員
|
Gerrit 配置及建庫流程
建庫
1.創建只用於權限管理的庫,可以使用如下命令(請注意端口號以實際為准)
ssh
-p 29418 scm@1.2.3.4 gerrit create-project -p All-Projects -n project --empty-commit --permissions-only
2.創建代碼庫,以manifest為例命令如下:
ssh
-p 29418 scm@1.2.3.4 gerrit create-project -p project -n project
/manifest
--empty-commit
批量建庫,可以通過整理庫列表,使用如下腳本來完成
for
i
in
`
cat
project-list`;
do
ssh
-p 29418 scm@1.2.3.4 gerrit create-project -p project -n projec
|
