How to make a branch review with Gerrit

試著忘記壹切 提交于 2021-02-05 08:31:10

问题


I'm choosing a free tool to enforce code-review, I'm thinking about gerrit. The idea is to review the code of a branch with multiple changes (commits). How can I do a branch review with gerrit? The goal is do analyse all branch changes in one gerrit change.

Thanks


回答1:


If you want to review a branch with multiple commits Gerrit isn't an appropriate tool for that. Reviews in Gerrit are performed in every commit individually. Github or Bitbucket are tools suitable for branch review.

Answered your question, a suggestion: IMHO you should try to work with Gerrit, reviewing every commit individually, because it's a very nice workflow. I really like (and prefer) Gerrit strategy.




回答2:


I created a git alias which facilitates the traditional git workflow (with multiple commits on a "working" branch) along with pushing them to Gerrit automatically with the same Change-Id in the commit message. It can be added to .gitconfig file:

...
[alias]
    gerrit = "!git reset --soft $(git rev-list master..HEAD | tail -1) &&  \
               git commit --amend --no-edit &&                             \
              (git push origin HEAD:refs/for/master; git reset HEAD@{2})"

Assumptions:

  • you have created a branch for your topic/fix change and placed at least one commit;
  • the name of the trunk branch in your project is master (can be set in the alias);

Usage: git gerrit


This works as follows:

git reset --soft $(git rev-list master..HEAD | tail -1)

git rev-list master..HEAD | tail -1 obtains SHA-1 of the first commit we created on the branch (after forking from master), I'll call it a "base" commit. Then we feed the SHA-1 to: git reset --soft which moves the branch pointer to this commit keeping all changes from later commits as staged. Then:

git commit --amend --no-edit

we squash staged changes into the base commit with a single --amend. The original commit message will be preserved and so will be Change-Id.

git push origin HEAD:refs/for/master

This is the point of everything - we push to Gerrit...

git reset HEAD@{2}

and finally we restore the original shape of our branch utilizing reflog history, HEAD@{2} means a revision where we was two steps ago (before first reset and commit --amend).

Advantages:

  • you maintain your own granularity of commits on the working branch, you can edit them with git rebase -i as much as you like for logical order and readability (don't remove Change-Id from the "base" commit message);
  • at the same time with a single command you upload newest changes to Gerrit making it accept them as another patchset of the same Change without SPAMMING your Change List with multiple related Changes;

Nevertheless I also recommend keeping a classical remote branch as a backup and synchronize it. If one doesn't like the reflog usage in the alias go ahead and replace it with git rev-parse @{u} which is a tip of the remote reference. The same trick can be used for a branch shared with colleagues (but we must refrain from rewriting history and push -f).



来源:https://stackoverflow.com/questions/54220722/how-to-make-a-branch-review-with-gerrit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!