提交代码修改

软件工程有点像制作音乐,一段看起来简单、符合逻辑的代码修改,实际上需要多次修改和大量工作的投入迭代才能最终定版,合并到代码仓库。这份代码日常,通常来讲包含以下部分:

  • 修复编译错误
  • 代码抽象和方法重构,避免重复代码
  • 使用更好的算法设计,更快、性能更好
  • 处理各种异常情况,是程序更加健壮
  • 添加测试代码,避免异常回归或引入错误
  • 预发环境测试,验证修改结果和带来的改变
  • 整理代码,提升可读性
  • 改进修改提交消息,解释为什么要做这次提交

半成品的代码,最好不要放进代码仓库中,我们使用 Gerrit 来协助处理代码合入 Code base 前的协作。

Consider Queen’s six-minute long Bohemian Rhapsody, which took three weeks to record. Some segments were overdubbed 180 times!

接到开发任务,开始工作

项目列表中找到需要的代码仓库,项目权限可以找开发任务所属的项目负责人分配。

从项目仓库中,拉取最新的代码到本地:

git clone https://code.expound.cc/expound-wiki

在个人设置页面 #HTTPCredentials 可以生成 HTTP 密码;

提交本地代码修改

⚠️ 在你完成本地的代码修改工作后,在提交代码之前需要注意的事项:

1)确认当前项目的 git 用户配置是否和 Expound Review 系统一致, 在项目目录(只对该项目生效):

git config -l                             # 查看所有 git 配置信息
git config user.email "{user}@expound.cc" # 邮箱必须是工作邮箱,否则会拒绝此提交
git config user.name "{your username}"    # 可选配置

2)为了在 commit 消息中自动插入 Review 流程的 Change-ID, 需安装 git hooks:

# 从 Gerrit Server 上直接下载到项目目录
curl -Lo .git/hooks/commit-msg https://code.expound.cc/tools/hooks/commit-msg
# 确保 hook 是可以执行的
chmod u+x .git/hooks/commit-msg

3)提交本地修改:

git commit -a -m "You Commit Message"

推送代码修改到 Review 流程

直接向指定分支推送代码或者推送新分支都是不允许的,如果你想往 develop 分支推送代码:

git push origin HEAD:refs/for/develop  # 分支规范:refs/for/{branch}

推送成功后,在网页端可以邀请其他同事(专家、利益相关者、或团队成员)进行 Review,讨论代码问题:

...

remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote:   https://code.expound.cc/c/expound-wiki/+/2 修改介绍页面跳转链接 🔗 [NEW]
remote:
To https://code.expound.cc/expound-wiki
 * [new reference]   HEAD -> refs/for/develop

Gerrit Code Review 网页端功能使用说明

根据 Review 意见进行修改,再次提交

$ <rework>
$ git commit --amend # Rebase the commit if needed
$ git push origin HEAD:refs/for/develop

在满足 Code-Review Label 的投票规则后,该修改即可等待提交自动合并。

Rebase a Change locally

// update the remote tracking branches
$ git fetch

// fetch and checkout the change
// (checkout command copied from change screen)
$ git fetch https://gerrithost/myProject refs/changes/74/67374/2 && git checkout FETCH_HEAD

// do the rebase
$ git rebase origin/main

// resolve conflicts if needed and stage the conflict resolution
...
$ git add <path-of-file-with-conflicts-resolved>

// continue the rebase
$ git rebase --continue

// push the commit with the conflict resolution as new patch set
$ git push origin HEAD:refs/for/develop

同时开发多个功能,建议为每个功能在本地配置多个 feature 分支。