评审协作
当我们对代码质量有较高要求的情况下,再使用git push直接将代码推入远程仓库的方法就难以满足我们的需求了。为此iCode为大家提供了人工评审和预提交测试的功能,下面介绍下开启方法和效果。
开启方法
进入代码库“提交规则”设置页,勾选“提交准入检查”。push 代码的时候需要以 refs/for/分支 的形式使用准入检查功能(详细介绍见下文)
- 机器检查>持续集成:ipipe的change流水线执行成功才能入库
- 人工评分>代码评审:人工评审通过,拿到+2分后才能入库
- 人工评分>禁止自评:人工评审通过,拿到+2分后才能入库,且代码的提交人不能评审自己的代码。
开启上述规则的情况下,再使用git push [目标仓库] [目标分支]就不好使了。那么我们怎么提交代码呢?下面就来介绍下原理和操作方法
原理
每一个本地commit被合入远端任意分支(例如:master)前都需要经过评审
本地commit先被推送到远端master的暂存区进行逐行评审
评审通过后自动合入远端master
评审驳回后发起人替换新版commit并丢弃旧版(git commit –amend),再次发起评审
操作步骤
过程解释
以开发master分支为例,假设我们要基于最新的master分支继续开发代码。首先进入本地的workspace,执行下述命令,
1➜ icode git:(master) git checkout master
2Already on 'master'
3Your branch is up-to-date with 'origin/master'.
4➜ icode git:(master) git fetch
5➜ icode git:(master) git merge origin/master
6Already up-to-date.
步骤 | 命令 |
---|---|
① 远程仓库的master分支最新一次提交是C1,基于C1拉取本地分支featureX【可选】拉本地分支featureX是可选操作,当同时需要开发多个不同的功能时拉不同的本地分支更有意义。否则直接用本地的master分支即可 | |
②在本地修改代码,提交到本地仓库C2 | 修改代码git add <文件名>git commit -m “some comments for C2” |
③发起Change Request(push),实时触发编码规范检查和预提交流水线构建任务,可以在此时通知其他同事来评审代码 | git push origin featureX:refs/for/master |
④所有检查通过的情况下,点击”合入“按钮成功,代码真正提交到远程仓库的master分支 合入成功后,持续集成任务会被触发,可以在这个环节做更多的自动化验证和手工测试 |
![]() ![]() |
当任意一项检查不通过需要修改代码的情况下(编译失败/人工平时不通过/合入冲突……),重新回到开发环境使用相同的提交步骤。修改代码>add>commit提交为C3>push到master | 修改代码 git add <文件名> git commit -m “some comments for C3” git push origin featureX:refs/for/master |
举例
本地开发和提交
我要向appdemo/icode的master分支提交代码,我在本地拉出feature-abc分支作为开发分支
1➜ $ (master) git checkout -b feature-abc
2Switched to a new branch 'feature-abc'
3➜ $ (feature-abc) echo cat >pets
4➜ $ (feature-abc) ✗ git add pets
5➜ $ (feature-abc) ✗ git commit -m'get some pets'
6[feature-abc c00fc83] get some pets
7 1 file changed, 1 insertion(+)
8 create mode 100644 pets
9➜ $ (feature-abc) git push origin feature-abc:refs/for/master
10Enumerating objects: 5, done.
11Counting objects: 100% (5/5), done.
12Delta compression using up to 4 threads.
13Compressing objects: 100% (2/2), done.
14Writing objects: 100% (3/3), 365 bytes | 365.00 KiB/s, done.
15Total 3 (delta 0), reused 0 (delta 0)
16remote:
17remote: Processing changes: new: 1, refs: 1, done
18remote:
19remote: New Changes:
20remote: http://xly.bce.baidu.com/review/changes/170 get some pets
21remote:
22To https://xly.bce.baidu.com/git/xxxx/Baidu/demo
23 * [new branch] feature-abc -> refs/for/master
(警告) 上述以master分支为例,以此类推,向其他分支提交代码的方法是一样的,在git push origin HEAD:refs/for/<目标分支>时,目标分支不同,决定了代码最终会被合并到哪个远程分支上。
Owner评审:通过或驳回
代码评审入口
评审详情页
查看上传的commit详情、代码diff,代码逐行评审,添加评论
- +2:通过,可以提交
- +1:我觉得OK,还需要其他人+2
- 0:无意见
- -1:最好不要提交
- -2:不能提交
合入master
点“评审详情页”右上角的“合入”按钮
【Optional】替换评审中的commit
注意:飘红的为关键命令
1➜ $ (feature-abc) echo cat >>pets
2➜ $ (feature-abc) ✗ git commit --amend
3 [feature-abc 2307014] get some pets
4 Date: Thu Jan 17 18:47:13 2019 +0800
5 2 files changed, 2 insertions(+), 1 deletion(-)
6 create mode 100644 test1.txt
7➜ $ (feature-abc) git push origin feature-abc:refs/for/master
8 Enumerating objects: 6, done.
9 Counting objects: 100% (6/6), done.
10 Delta compression using up to 4 threads.
11 Compressing objects: 100% (2/2), done.
12 Writing objects: 100% (4/4), 412 bytes | 412.00 KiB/s, done.
13 Total 4 (delta 0), reused 0 (delta 0)
14 remote:
15 remote: Processing changes: updated: 1, refs: 1, done
16 remote:
17 remote: Updated Changes:
18 remote: http://xly.bce.baidu.com/review/changes/170 get some pets
19 remote:
20 To https://xly.bce.baidu.com/git/xxxx/Baidu/demo
21 * [new branch] feature-abc -> refs/for/master
更新Patch Set
##【Optional】git-rebase远端master
当发生以下情况,你需要将远端的master的代码合并到本地分支,
- 远端master提交了一个commit,你的本地分支依赖这个commit的代码
- 远程master提交了一个commit,与你的本地分支合并冲突,你需要手动解决冲突 这两种场景都使用git-rebase来解决,
命令
1➜ $ (feature-abc) git fetch origin master:master
2From https://xly.bce.baidu.com/git/xxxx/Baidu/demo
3 2897d6d..e047cf5 master -> origin/master
1➜ $ (feature-abc) git rebase origin/master
2First, rewinding head to replay your work on top of it...
3Applying: get some pets
4Using index info to reconstruct a base tree...
5Falling back to patching base and 3-way merge...
6Auto-merging pets
7CONFLICT (add/add): Merge conflict in pets
8error: Failed to merge in the changes.
9Patch failed at 0001 get some pets
10The copy of the patch that failed is found in: .git/rebase-apply/patch
11When you have resolved this problem, run "git rebase --continue".
12If you prefer to skip this patch, run "git rebase --skip" instead.
13To check out the original branch and stop rebasing, run "git rebase --abort".
14➜ $ (36d6b4c) ✗ git status
15rebase in progress; onto 36d6b4c
16You are currently rebasing branch 'feature-abc' on '36d6b4c'.
17 (fix conflicts and then run "git rebase --continue")
18 (use "git rebase --skip" to skip this patch)
19 (use "git rebase --abort" to check out the original branch)
20Unmerged paths:
21 (use "git reset HEAD <file>..." to unstage)
22 (use "git add <file>..." to mark resolution)
23 both added: pets
24no changes added to commit (use "git add" and/or "git commit -a")
25➜ $ (36d6b4c) ✗ vi pets
26➜ $ (36d6b4c) ✗ git add pets
27➜ $ (36d6b4c) ✗ git rebase --continue
28Applying: get some pets
29➜ $ (feature-abc) git push origin feature-abc:refs/for/master
30 Enumerating objects: 6, done.
31 Counting objects: 100% (6/6), done.
32 Delta compression using up to 4 threads.
33 Compressing objects: 100% (2/2), done.
34 Writing objects: 100% (4/4), 412 bytes | 412.00 KiB/s, done.
35 Total 4 (delta 0), reused 0 (delta 0)
36 remote:
37 remote: Processing changes: updated: 1, refs: 1, done
38 remote:
39 remote: Updated Changes:
40 remote: http://xly.bce.baidu.com/review/changes/170 get some pets
41 remote:
42 To https://xly.bce.baidu.com/git/xxxx/Baidu/demo
43 * [new branch] feature-abc -> refs/for/master</file></file>
提交规则设置
提交规则
配置提交规则直接影响代码的提交方式和提交要求。
- 分支:配置项对哪个分支生效。为空表示目前尚不支持按照分支维度配置,只能按照代码库维度配置
- 禁止删除:禁止删除已经入库的提交代码,禁止使用git push -f修改远程仓库的提交历史
- 提交准入检查:不能直接使用git push命令更新远程分支,必须经过评审检查环节
- 机器检查>持续集成:强制检查持续集成任务执行成功
- 人工评分>代码评审:强制要求人工评审通过
- 人工评分>同行评审:强制要求代码的提交人不能给自己评审通过,必须找其他同事评审通过
- 提交方式>自动提交:当不勾选”人工评分“的情况下,检查通过会自动”合入“代码到远程仓库的目标分支
- 提交方式>合入策略:当别人在我执行合入前已经更新了远程仓库对应分支的代码时,系统如何将我的代码合入远程仓库
默认评审人
认评审人是一个便捷用户操作的功能,当我执行类似git push origin xxx:refs/for/xxx的命令发起评审时,默认评审人会在iCode的我的评审页中看到我的评审单。
可以按照分支+文件/目录的维度来配置