環境:git

Gerrit Server:172.16.206.133 es6

Client:172.16.206.129shell

 

一、在Gerrit服務器上建立用戶vim

Gerrit服務器使用的是HTTP認證類型,並用httpd作反向代理,建立用戶使用如下命令瀏覽器

#cd  /home/gerrit2/gerrit/etcbash

[root@localhost etc]# htpasswd -m passwords user2服務器

New password: dom

Re-type new password: yii

Adding password for user user2ssh

根據提示輸入密碼便可(密碼設置爲123456),這樣帳號就建立好了。建立完成,httpd和gerrit服務不須要重啓。

 

二、登陸gerrit用戶

用建立的user2帳號訪問gerrit,在瀏覽器地址欄輸入:

http://172.16.206.133:8080

輸入登陸的帳號密碼,帳號爲user2,密碼爲123456

wKioL1cxh72j3sxnAAH_7PqFtdg542.png

 

三、修改gerrit用戶配置

登陸後,在網頁右上角點擊本身的用戶名,在點擊「settings」。

wKioL1cxT6yh3itBAABV-Z3w2RA103.png

wKiom1cxTvyDOGa1AAIQnIKspGE990.pngsettings中就是用戶的配置相關信息,須要註冊郵箱和提交服務器上的公鑰

一、提交公鑰

注意:爲何要提交公鑰文件到gerrit服務器?若是不提交公鑰,則不能使用git push命令將本地代碼提交到gerrit服務器。公鑰是一個認證方式。

在客戶端172.16.206.129這臺服務器上建立公、私鑰對,具體命令以下:

[user2@host2 root]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user2/.ssh/id_rsa): Created directory '/home/user2/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user2/.ssh/id_rsa. Your public key has been saved in /home/user2/.ssh/id_rsa.pub. The key fingerprint is: fd:da:fa:c5:ec:6c:82:60:1f:39:15:bc:62:ec:51:15 user2@host2 The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | . .E. | | + | | . . o | | .= o | | So.= | | o =. o | | . o +. + | | oo.+. | | ooooo | +-----------------+
 

生成的密鑰文件以下:

[user2@host2 ~]$ ls .ssh/

id_rsa  id_rsa.pub

id_rsa.pub爲公鑰文件,將該文件中的內容,複製並粘貼到settings==>SSH Public Keys中,並點擊Add

wKioL1cxUgfDFUVqAAGq3q-4bWk295.png

測試SSH key是否提交成功,在client 172.16.206.129服務器上執行如下命令:

#ssh -p 29418  user2@172.16.206.133

29418爲gerrit上SSH服務器監聽的端口,user2爲登陸的gerrit帳號,注意這裏咱們使用哪一個帳號登陸gerrit就使用哪一個帳號測試,而且該帳號已經登陸了gerrit服務器,並且公鑰已經上傳。172.16.206.133爲gerrit服務器的IP

[user2@host2 .ssh]$ ssh -p 29418 user2@172.16.206.133 The authenticity of host '[172.16.206.133]:29418 ([172.16.206.133]:29418)' can't be established. RSA key fingerprint is 93:d3:47:c2:15:32:f0:41:d9:e6:7f:88:66:01:f3:9b. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[172.16.206.133]:29418' (RSA) to the list of known hosts. **** Welcome to Gerrit Code Review **** Hi user2, you have successfully connected over SSH. Unfortunately, interactive shells are disabled. To clone a hosted Git repository, use: git clone ssh://user2@172.16.206.133:29418/REPOSITORY_NAME.git Connection to 172.16.206.133 closed.
 

能夠看到鏈接成功,說明SSH Public Key上傳成功。

 

爲user2註冊郵箱

wKiom1cxU7GAHRFDAAGEtbEIY_0461.png

點擊Register後,登陸你剛纔註冊的郵箱,激活便可。

 

 

四、建立用戶組並建立一個項目

注意,只有管理員組的用戶才能建立group和Project

登陸Gerrit管理員帳號admin,建立一個新的組爲group2。並將user2帳號加入該組

wKioL1cxVyeQLKvEAAC6XxXWCvc918.png

wKioL1cxVyiieK8VAAGaaMumo9Q911.png

 

新建一個Project,名爲Project2

wKioL1cxWsKynVNDAAICK7gAolU605.png

wKioL1cxWsWjwjEwAAMaT5CjD8I516.png

 

五、gerrit代碼審覈流程

前面都是準備工做,接下來演示gerrit如何作代碼審覈的

a、client server( 172.16.206.129)的git初始配置

配置用戶名和郵箱

#git config --global user.name "user2"

#git config --global user.email "YOUR EMAIL-ADDRESS"

 

b、用帳戶user2登陸gerrit 

在Projects==> List中找到Projects2,而後點擊Clone with commit-msg hook, 在點擊ssh按鈕,

複製ssh下面項目地址:

git clone ssh://user2@172.16.206.133:29418/Project2 && scp -p -P 29418 user2@172.16.206.133:hooks/commit-msg Project2/.git/hooks/
 

直接將剛纔複製的命令,在client server的命令行運行

git clone ssh://user2@172.16.206.133:29418/Project2 && scp -p -P 29418 user2@172.16.206.133:hooks/commit-msg Project2/.git/hooks/
 

 

注意:步驟b必定選Clone with commit-msg hook下面的地址來clone,不然在客戶端使用git push

命令提交代碼時會報錯。若是不須要向gerrit服務器提交代碼,那麼你只需使用

git clone ssh://user2@172.16.206.133:29418/Project2

或者git clone http://user2@172.16.206.133:8081/a/Project2

 

 

c、建立一個文件,並提交到gerrit服務器端

[user2@host2 Project2]$ vim test.sh aaaaaaaaaaaaa bbbbbbbbbbbbb ccccccccccccc
 

用git add 和git commit命令將文件保存到git倉庫中

[user2@host2 Project2]$ git add test.sh [user2@host2 Project2]$ git commit -m "建立了test.sh文件" [master 16462e9] 建立了test.sh文件 1 files changed, 3 insertions(+), 0 deletions(-) create mode 100644 test.sh
 

 

d、用git push命令將代碼從本地傳到gerrit服務器對象項目下的master分支

[user2@host2 Project2]$ git push origin master:refs/for/master Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 341 bytes, done. Total 3 (delta 0), reused 0 (delta 0) remote: Processing changes: new: 1, refs: 1, done remote: remote: New Changes: remote: http://172.16.206.133:8081/5 建立了test.sh文件 remote: To ssh://admin@172.16.206.133:29418/Project2 * [new branch] master -> refs/for/master
 

origin:遠程倉庫的名稱,用git remote 能夠查看全部的遠程倉庫名稱

[user2@host2 Project2]$ git remote origin
 
master:refs/for/master 表示將代碼從本地的master分支傳到遠端的master分支,固定格式就是這樣
 

 

e、用user2帳號登陸Gerrit Web UI,邀請管理員admin來作code review。

注意:以前忘記截圖了,因此下圖不是原圖,可是基本上都同樣,只有Owner和Project不對而已。

Add:添加指定用戶來進行Code Review

Cherry Pick:切換分支,當執行push操做以後若是發現分支不對,須要切換,能夠在這裏點擊Cherry Pick按鈕進行分支切換操做。

Abandon:撤銷push操做,當push以後,若是發現代碼有問題,須要修改,能夠點擊這個按鈕,而後修改代碼,修改完成後,再次push,而後登錄Gerrit Web UI,會有一個Restore按鈕,這時候點擊Restore,就能夠從新申請Code Review。

Rebase和Follow-Up沒有找到相關資料wKioL1c6xyeAx7QTAAEVKOf0oBY681.png

 

 

 

 

 

e、看看Gerrit服務器上是否有code review的信息

登陸gerrit管理員帳號admin,在ALL下面點擊「Open」,看到「建立了test.sh」這條記錄。

這裏「建立了test.sh文件」就是git commit -m "建立了test.sh文件" 這條命令產生的

wKioL1cxd0KC9IBEAAJt4--8EoI491.png

點開這條記錄,看到如下信息

 

wKioL1cxd0biNku7AARvdB2TlY4241.png

 

咱們能夠點開test.sh文件看到裏面的內容

wKioL1cxeGuDJUuNAAEkJvYYuEs066.png

 

還能夠看到提交者的信息:

wKioL1cxeRrASYpkAAEtZeZW_es670.png

這裏顯示的用戶名和郵箱地址實際上是在client server上初始化git時,用如下命令配置的

#git config --global user.name "user2"

#git config --global user.email "YOUR EMAIL-ADDRESS"

 

點擊上上圖中的code review和replay進行代碼審覈

wKiom1cxebSB85xGAAEUOX7Xn3A497.png-2 表示不經過,+2表示代碼ok能夠經過審覈,目前尚未深究這個,我理解的就是這樣子的。

審覈後能夠看到審覈的日誌

wKioL1cxe1iyH_XTAAEcFP-mH0E976.png

 

以上就是gerrit代碼審覈的流程,gerrit倉庫是純粹的代碼倉庫,不存在work tree這個說法。因此在gerrit倉庫裏面執行git命令都會報錯:

[root@localhost Project2.git]# git status fatal: This operation must be run in a work tree
 

可是,若是用git clone將gerrit倉庫裏面的項目clone下來後,就自動變成了一個git倉庫。